Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created March 19, 2012 19:36
Show Gist options
  • Save timpulver/2125489 to your computer and use it in GitHub Desktop.
Save timpulver/2125489 to your computer and use it in GitHub Desktop.
Searches for a file (CPP, Win)
void CFile::search(fstream &FS) {
char savepath[MAX_PATH]; // zur Zwischenspeicherung des Pfades
int len;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
strcpy(savepath, path); // Sicherung des Pfades
strcat(path, "\\*");
hFind = FindFirstFile(path, &FindFileData);
if(hFind == INVALID_HANDLE_VALUE) {
cout << "Datei nicht vorhanden oder anderer Fehler..." << endl << "GetLastError: " << GetLastError () << endl;
}
else {
do {
int result = strcmp(FindFileData.cFileName, ".");
int result2 = strcmp(FindFileData.cFileName, "..");
if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && result != 0 && result2 !=0) {
len=strlen(path);
strcpy(&path[len-2], "\0");
strcat(path, FindFileData.cFileName);
strcat(path, "\\");
search(FS);
strcpy(path, savepath);
strcat(path, "\\*");
}
else {
char file_end[MAX_PATH];
strcpy(file_end,FindFileData.cFileName);
len=strlen(file_end);
strcpy(&file_end[len-4], "\0");
strcat(file_end, type);
if((strcmp(file_end, FindFileData.cFileName)) == 0) {
cout << savepath << FindFileData.cFileName << endl;
}
}
} while(FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
}// end search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment