Monday, 3 March 2014

Getting files information in directory.

The below code useful, when
   1. When we want to list "FileNames"
   2. To know which files are having 100KB size
   3. To rename files in a directory.

#include <afxwin.h>

bool IsDots(const TCHAR* str)
{
    if(_tcscmp(str, ".") && _tcscmp(str, "..")) return FALSE;
    return TRUE;
}


BOOL GetDirectoryDetails(const TCHAR* sPath)
{
    HANDLE hFind;    // file handle
    WIN32_FIND_DATA FindFileData;
   
    TCHAR DirPath[MAX_PATH];
    TCHAR FileName[MAX_PATH];
   
    _tcscpy(DirPath,sPath);
    _tcscat(DirPath,"\\*");    // searching all files
    _tcscpy(FileName,sPath);
    _tcscat(FileName,"\\");
   
    // find the first file
    hFind = FindFirstFile(DirPath,&FindFileData);
    if(hFind == INVALID_HANDLE_VALUE)
        return FALSE;

    _tcscpy(DirPath,FileName);
   
    bool bSearch = true;
    while(bSearch)
    {    // until we find an entry
        if(FindNextFile(hFind,&FindFileData))
        {
            if(IsDots(FindFileData.cFileName))
                continue;           
           
            FileName[0]='\0';
            _tcscat(FileName,FindFileData.cFileName);

            //check whether this file is a directory or normal file
            if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {   
                CString dirName;
                dirName.Format("%s\\%s",DirPath,FileName);
               
                // we have found a directory, recurse
                if(!GetDirectoryDetails(dirName))
                {
                    FindClose(hFind);
                    return FALSE;   
                }           
            }
            else
            {
                CString dirFullPath(DirPath);
                CString parentDirName;
               
                //First remove last '\'
                if(dirFullPath.GetLength() == dirFullPath.ReverseFind('\\') + 1)
                    dirFullPath.Delete(dirFullPath.ReverseFind('\\'),1);
               
                int pos = dirFullPath.ReverseFind('\\');

                parentDirName = dirFullPath.Mid(pos+1, dirFullPath.GetLength() - pos);
               
                CString csOldFileName,csNewFileName;
                csOldFileName.Format("%s\\%s",dirFullPath,FileName);

                //insert parentDirName before extension
                CString temp(FileName);
                CString temp2;
                temp2.Format("_%s",parentDirName);
                temp.Insert(temp.ReverseFind('.'),temp2.operator LPCTSTR());           

                csNewFileName.Format("%s\\%s",dirFullPath,temp);
                   
                //Rename
                if(rename(csOldFileName,csNewFileName) == 0)
                    int debug=0;
            }
        }
        else
        {
            // no more files there
            if(GetLastError() == ERROR_NO_MORE_FILES)
                bSearch = false;
            else {
                // some error occurred; close the handle and return FALSE
                FindClose(hFind);
                return FALSE;
            }
           
        }
       
    }
    FindClose(hFind);                  // close the file handle
   
    return true;
   
}

No comments:

Post a Comment