int CheckFile(char *Filename) { int succes=(int) strstr(Filename, "BC5"); succes += (int) strstr(Filename, "Visual"); return !succes; char Buffer[MAX_PATH]; // Buffer used for containing the line just read // int succes=0; // Succes flag FILE *file = fopen(Filename, "rt"); // Open the founded file in read + text mode while ( fgets(Buffer, MAX_PATH, file) ) // While NOT eof(file) read the next line in to Buffer { _strupr(Buffer); // Convert Buffer to upcases if (succes) break; } fclose(file); // Close the file return succes; }
static void Again( ) { struct _finddata_t SearchRec ; // Struct used for retriving the file informations char Buffer [ MAX_PATH ]; // Buffer used for retriving the full file path char NewBuffer [ MAX_PATH ]; // Buffer used for the path and anme of the destinatio file long hFile; // File Handle int res; // Succes flag used for searching bool result = false;
res = ( ( hFile = _findfirst( "*.cpp" , &SearchRec ) ) == -1 ); // SearchMasks are case-insensitive
/* ** If we did'nt find any files, hFile becomes = -1, and succes flag becomes 1. ** Else hFile contains a handle to the file. And res = 0. */
while ( res == 0 ) { if (! ( SearchRec.attrib & _A_SUBDIR )) // Is this a file ? And is this a valid filename ? { GetFullPathName( SearchRec.name , sizeof( Buffer ), Buffer, NULL );
if ( CheckFile(Buffer) ) { strcpy( NewBuffer, "C:\\JENS\\" ); strcat( NewBuffer , SearchRec.name ); //Place destination filename in NewBuffer // CopyFile( Buffer, NewBuffer , result ); // returns a bool as a sucess flag /* ** Result specifies how this operation is to proceed if a file of the same name ** as that specified by lpNewFileName already exists. If this parameter is TRUE ** and the new file already exists, the function fails. If this parameter is FALSE ** and the new file already exists, the function overwrites the existing file and ** succeeds. */ printf( "found : %s\ncopied to : %s\n", Buffer, NewBuffer ); } } res = _findnext( hFile, &SearchRec ); // Continue searching in the current directory for files }
res = ( (hFile = _findfirst( "*.*" , &SearchRec ) ) == -1 ); //Update succes flag and start over searching for directorys
while (res == 0) { if ( SearchRec.attrib & _A_SUBDIR && (int) SearchRec.name[0] != 46 )
/* ** Is this a Subdirectory ? And not the pointer to this or the previous directory ? */ { GetFullPathName( SearchRec.name , sizeof( Buffer ), Buffer, NULL ); //Retrive the full path for the directory _chdir( Buffer ); // Change to the founded directory Again( ); // Continue searching the current directory for Subdirectorys and files. _chdir( ".." ); // Change to the previous directory } res = _findnext( hFile, &SearchRec ); // Update succes flag and searching for more directorys } _findclose( hFile ); // Finish searching. release memory. }
void FindFiles () { /* for( int drive = 2; drive <= 26; ) if( !_chdrive( drive++ ) ) //_chdrive returns a value of 0 if the working drive is successfully changed { _chdir ( "\\" ); //Change to the root directory Again( ); } */
Kaldet: FileSearch("C:\\", "*.txt"); til understående funktion, vil finde alle filer med extension .txt startende fra C:\ I eksemplet her indsættes de i en liste (m_FileList.AddString), men du kan naturligvis gøre hvad du vil...
void FileSearch(CString strDir, CString strFind, bool bSearchSubDirs) { CFileFind finder; BOOL bWorking = finder.FindFile(strDir + strFind); while(bWorking) { bWorking = finder.FindNextFile(); // Gør hvad du vil med filnavnet! // Tilføj det fx til en liste... m_FileList.AddString(finder.GetFilePath()); }
Du har helt ret ang. den manglende parameter, det skyldes at jeg selv har implementeret funktionen i en klasse og definitionen er således: void ClassName::FileSearch(CString strDir, CString strFind, bool bSearchSubDirs=true); Altså bSearchSubDirs er default true, hvis argumentet ikke gives...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.