How to find all files from a folder

29 10 2013

Sometimes we may need to find out all files belongs to a folder. How we will do it?


The FindFirstFile function opens a search handle and returns information about the first file with a name that matches the specified pattern. After the search handle is established, use the FindNextFile function to search for other files that match the same pattern.

void FindFilesFromFolder(CString p_strFolderPath, CStringArray* p_pFiles)
{
    HANDLE hFile = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA   FindFileData;
    CString strTemp;

    strTemp.Format(_T("%s\\%s"), p_strFolderPath, _T("\\*.*"));
    hFile = FindFirstFile(strTemp, &FindFileData);
    CString strFilePath;

    if (INVALID_HANDLE_VALUE != hFile) 
    {
        do
        {
            //Skip directories
            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
                continue;

            strFilePath.Format(_T("%s\\%s"), p_strFolderPath, FindFileData.cFileName);
            p_pFiles->Add(strFilePath);
            strFilePath.Empty();            
        } while(FindNextFile(hFile, &FindFileData));

        FindClose(hFile);
    }    
}


Please give the exact file extension incase if you need to find out only files which has specific extension.





How to read filenames from a folder

6 01 2010

 

 

How can we read the names of all files in a folder with a specific extension?

 

 

Windows has following APIs FindFirstFile and FindNextFile to get the handle of all files in a folder and thus it can extract the relevant information of a file to a WIN32_FIND_DATA structure.

 

 

bool FindFilesFromFolder()
{
   HANDLE            hFile;
   WIN32_FIND_DATA   FindFileData;
   std::vector fileList;

   char chFolderpath[_MAX_PATH];
   CString strExtension   = _T("*.txt");

   strcpy(chFolderpath, _T("D:\\san\\"));
   strcat(chFolderpath, strExtension);

   hFile = FindFirstFile(chFolderpath, &FindFileData);

   if (hFile == INVALID_HANDLE_VALUE) {

      AfxMessageBox(_T("Inavlid file handle."));
      return false;
   }

   CString filepath;

   do
   {

      filepath.Format(_T("%s%s"), _T("D:\\san\\"), FindFileData.cFileName);
      fileList.push_back(filepath);

   } while(FindNextFile(hFile, &FindFileData));

   return true;
}

 

 

If you want to get all files from a folder irrespective of its extension then just use *.* as filter.





How to get the size of a file without opening it

24 03 2009

description5

 

 

Consider a situation that we are recording a WAV file using any of the Windows sound recorders to a particular path. But do you notice that the size of the file is updating every couple of seconds if you look it in the Windows Explorer.  That means Windows Explorer could check the file size without open it even though it is still being recorded.

howcanidoit5

 

 

There is WIN32_FIND_DATA structure for that, which describes a file found by the FindFirstFile, FindFirstFileEx or FindNextFile function. This structure helps us to get the file creation time, last access time, last write time, file size etc. These whole things could be accessed without open the file.

mycodesnippet5

 

 

ULONGLONG GetFileSizeEx( CString strPath )
{  
   WIN32_FIND_DATA FindData = { 0 };
   //gets a file search handle
   HANDLE hFirstFile = FindFirstFile( strPath, &FindData );  
  
   //if the handle is valid
   if( hFirstFile != INVALID_HANDLE_VALUE )
   {
      //closes the file search handle
      FindClose( hFirstFile );
     
      ULONGLONG FileSize = FindData.nFileSizeHigh;
      FileSize <<= sizeof( FindData.nFileSizeHigh ) * 8; // Push by count of bits
      FileSize |= FindData.nFileSizeLow;
     
      return FileSize;
   }
  
   return 0; // File not found
}

mynote8

 

 

Not all file systems can record creation and last access time and not all file systems can record then in the same manner.