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;
}