Thread: Directories

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    Directories

    Hi

    I want to make a programm similar to "dir", except that it does not diplay the files found, but work with it. Let's call my prog dir2. Finding files like "dir2 *.sys" is easy. I use FindFirstFile() and FindNextFile() from windows.h. However, I don't manage to do a query like "dir2 *.sys /s" that should scan also all subdirectories. I thought about maybe wraping my curent code (see below) in a while which searches all files (*) and then proofs if it is a directory with GetFileAttributes(). If it is a directory it cals itself recoursivly. that however seems a little bit to complicated? Isn't there an Api function for that? Any easy way?

    Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include <windows.h>
    
    #define	LINELENGTH	1000
    #define PATTERN		"COMCOP "
    
    int scanfile(char *);
    
    int main(int argc, char *argv[])
    {
    	WIN32_FIND_DATA FindFileData;
    	HANDLE			hFind;
    	DWORD			dwAttrs; 
    	
    	if(argc<2 || argc>3) {
    		printf("Bitte Dateinamen angeben.\n");
    		return 1;
    	}
    
      
    	hFind = FindFirstFile(argv[1], &FindFileData);
    	if (hFind == INVALID_HANDLE_VALUE) {
    		printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());
    		return 1;
    		} 
    	else {
    		/* Ist die Gefundene Datei ein Ordner? Nein -> Verarbeiten, Ja -> Tu nichts*/
    		dwAttrs = GetFileAttributes(FindFileData.cFileName); 
            if (!(dwAttrs & FILE_ATTRIBUTE_DIRECTORY)) { 
    			printf ("Bearbeite %s\n", FindFileData.cFileName);
    			if(scanfile(FindFileData.cFileName)!=0) {
    				printf("Fehler beim bearbeiten von: %s\n",argv[1]);
    				return 1;
    			}
    		}
    	}
    
    	while(FindNextFile(hFind,&FindFileData)) {
    		/* Ist die Gefundene Datei ein Ordner? Nein -> Verarbeiten, Ja -> Tu nichts*/
    		dwAttrs = GetFileAttributes(FindFileData.cFileName); 
            if (!(dwAttrs & FILE_ATTRIBUTE_DIRECTORY)) { 
    			printf ("Bearbeite %s\n", FindFileData.cFileName);
    			if(scanfile(FindFileData.cFileName)!=0) {
    				printf("Fehler beim bearbeiten von: %s\n",argv[1]);
    				return 1;
    			}
    		}
    	} 
    	
    	if (GetLastError() == ERROR_NO_MORE_FILES) { 
    		printf("Verarbeitung erfolgreich beendet.\n"); 
    	} 
    	else { 
    		printf("Konnte nicht alle Dateine verarbeiten.\n"); 
    		return 1;
    	}
    	FindClose(hFind);
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's a couple of examples here. OK, they're not in "Windows code", but the principle remains the same. Recursion is a good way to go, it's not complicated once you get your head round it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing 2 directories
    By Opel_Corsa in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2007, 11:30 PM
  2. VC6 directories
    By Magos in forum Tech Board
    Replies: 0
    Last Post: 03-11-2005, 05:52 PM
  3. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM
  4. mkdir and multiple directories
    By Idle in forum C Programming
    Replies: 4
    Last Post: 06-28-2003, 04:56 AM
  5. Working with directories...
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 11:45 AM