make a list of directory's files

Posted on 2006-05-29
Last Modified: 2008-03-04

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!
Question by:DavidAnullo
    LVL 16

    Expert Comment

    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 ];
    } WIN32_FIND_DATA;

    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.
    LVL 86

    Accepted Solution

    It is actually quite easy, I wouldn't call that messy. See the artile at ("Listing the Files in a Directory"), which comes with full source code.
    LVL 24

    Expert Comment

    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.


    Author Comment

    thanks jkr. very helpfull example!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now