I wnat to count the number of 0 byte files in a directory and if the count is equal to or greater than 1, delete them otherwise do nothing. How can I do this? This is windows.
Thank you.
Use FindFirstFile() and FindNextFile() to get the names of all the files in the directory. The functions fill a WIN32_FIND_DATA structure which also contains the length of the file. If both the nFileSizeHigh and nFileSizeLow values are zero, then you can delete the file. Use DeleteFile().
#include <Shlwapi.h> // for PathAppend()
void EnumerateFolderFS(LPCTSTR path)
{
TCHAR searchPath[MAX_PATH];
// a wildcard needs to be added to the end of the path, e.g. "C:\*"
lstrcpy(searchPath, path);
PathAppend(searchPath, _T("*")); // defined in shell lightweight API (v4.71)
WIN32_FIND_DATA ffd; // file information struct
HANDLE sh = FindFirstFile(searchPath, &ffd);
if(INVALID_HANDLE_VALUE == sh) return; // not a proper path i guess
// enumerate all items; NOTE: FindFirstFile has already got info for an item
do {
cout << "Name = " << ffd.cFileName << endl;
cout << "Type = " << ( (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
? "dir\n" : "file\n" );
cout << "Size = " << ffd.nFileSizeLow << endl;
} while (FindNextFile(sh, &ffd));
FindClose(sh);
}