Count 0 Byte Files

Sep 28, 2011 at 8:20pm
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.
Last edited on Sep 28, 2011 at 8:22pm
Sep 28, 2011 at 8:41pm
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().

Good luck!
Sep 28, 2011 at 9:12pm
Something like this?
1
2
3
4
5
6
7
HANDLE hFind;
    WIN32_FIND_DATA FindData;


    hFind = FindFirstFile(L"C:\\*.zip", &FindData);
    std::wcout << FindData.cFileName;
    return 0;
Sep 28, 2011 at 9:14pm
Fixed:

1
2
3
4
5
6
7
    HANDLE hFind;
    WIN32_FIND_DATAW FindData;


    hFind = FindFirstFileW(L"C:\\*.zip", &FindData);
    std::wcout << FindData.cFileName;
    return 0;


</anal about WinAPI TCHAR crap>
Sep 28, 2011 at 9:19pm
Is this closer?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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);
}
Topic archived. No new replies allowed.