• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

make a list of directory's files


i have the problem to delete file older than x days. all my file are in a unique directory. under unix are present the "readdir" function but not under windows and i  developing just on windows.. :(

i hope that someone have a simple sample for me.. tnks!
1 Solution
It's rather a messy business in Windows, as usual. You need the FindFirstFile and FindNextFile calls.

HANDLE FindFirstFile(
  LPCTSTR lpFileName,     // name of file for search
  LPWIN32_FIND_DATA lpFindFileData  // data returned

is the signature for the first one. The lpFindFileData returns:

typedef struct _WIN32_FIND_DATA {
    DWORD dwFileAttributes;
    FILETIME ftCreationTime;
    FILETIME ftLastAccessTime;
    FILETIME ftLastWriteTime;
    DWORD    nFileSizeHigh;
    DWORD    nFileSizeLow;
    DWORD    dwReserved0;
    DWORD    dwReserved1;
    TCHAR    cFileName[ MAX_PATH ];
    TCHAR    cAlternateFileName[ 14 ];

where lpFindFileData points. FILETIME is another windows structure that can be converted to something sensible with the FileTimeToSystemTime call.

To find subsequent calls use:

BOOL FindNextFile(
  HANDLE hFindFile,                // search handle
  LPWIN32_FIND_DATA lpFindFileData // pointer to data on found file

When you are done remember to call FindClose.
It is actually quite easy, I wouldn't call that messy. See the artile at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/listing_the_files_in_a_directory.asp ("Listing the Files in a Directory"), which comes with full source code.
Well the way it is done is very similiar in Linux and Windows. I can not see how you can make it much easier but with some extra stuff for globbing. The orignal question is still not fully answered. he wants to delete files older then a certain   time. And now it's getting hairy which time to use?
Access time?
Creaton time?
Modification time?

We simply do not know.

DavidAnulloAuthor Commented:
thanks jkr. very helpfull example!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now