Hi all, I'm just presenting you a simple method to go through all files and folders and find anything. This code is in Win32 VC++ format. The code pops up a Browse Dialog box asking the user to choose a Directory or Drive to search for, and recursively searches indide folders and subfolders. I'm sharing the code to you. I used recursion here.
void BrowseFolder( ) // This is our Function for bringing Folder Browse Dialog up.
{
TCHAR path[MAX_PATH]; //Buffer to hold path to file selected by user.
BROWSEINFO bi = { 0 }; //Filling the BROWSEINFO structure with zero (just emptying)
bi.lpszTitle = ("Browse Folder.");
bi.hwndOwner = (HWND)ParentWindow; //You must specify the parentwindow, otherwise keep it as NULL.
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi ); //This brings the Browse Dialog up.
if ( pidl != 0 )
{
SHGetPathFromIDList ( pidl, path ); //Get the path of drive/folder user choosen
SetCurrentDirectory ( path ); //Setting the folder/drive as current directory
SearchFolder( path );
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
}
void SearchFolder( TCHAR* path) //The function for searching
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
TCHAR filename[ MAX_PATH + 256 ];
hFind = FindFirstFile ( "*.*", &FindFileData ); //Finds everything, means *.*
do
{
if ( hFind != INVALID_HANDLE_VALUE )
{
if ( ! ( strcmp( FindFileData.cFileName, "." ) ) ! ( strcmp( FindFileData.cFileName, ".." ) ) ) //Checking whether current or parent directory
{
continue;
}
sprintf( path, "%s", FindFileData.cFileName );
if ( ( SetCurrentDirectory( path ) ) ) // Checking whether it is a file or folder. If its a folder, then the SetCurrentDirectory() will return sucess.
{
SearchFolder( path ); //Recursively calling the function.
}
}
}
while ( FindNextFile ( hFind, &FindFileData ) && hFind != INVALID_HANDLE_VALUE ); //Loop until finished search or file/folder tree ends
FindClose ( hFind );
}
Search This Blog
Subscribe to:
Post Comments (Atom)
hey how to save the file?
ReplyDeleteMeans what is the extension of the file?
This is not a progranm to save File/Files.. This sample can go through sub directories/files in a drive or folder.
ReplyDeleteTo save a file, you need to use CreateFile() and WriteFile() Windows API's.
:)