Win32 API - C-Language - Get/Set Folder Creation Date-Time

Posted on 2006-05-31
Last Modified: 2013-12-03

I'm a retired Unix/C programmer; never moved up to C++, and
I don't program windows.  But I do still write DOS-Box style
c-programs for myself, and when I need to, I include WIN32
API calls in my otherwise vanilla C-code.

I am trying to write a program to display and modify the
creation-date-time for a folder, and I am trying to learn
the Win32 API for this.  The code below is a simplified
version just to test if I can get the the API calls working.

Normally, I use unix compatible i/o [FILE * handle=fopen(...);]
programming style, so I'm not familiar with the "CreateFile" API.
It seems though that I must use this since the "GetFileTime" API
requires a compatible handle from "CreateFile".

Anyway, I just can't seem to get it to work.  I keep getting
"INVALID_FILE_HANDLE" on the open.  Also, since I seem
to be stuck on the first step, and since I need for the whole
program to work, I'd appreciate if whoever helps on this
would eyeball the whole code to see if I'm on the right track.

Thx, TC

Test Code:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

#include <stdio.h>
#include <windows.h>

   HANDLE      hFolder;
   FILETIME    FileDate;

   char folderpath[]="c:\\John\\Doe";
   int  status;

/**   Part-1 Display Original Folder CrDate   **/

   /* get "Generic-Read" handle to the folder */
   hFolder = CreateFile(folderpath, GENERIC_READ, FILE_SHARE_READ, NULL,

   if(hFolder == INVALID_HANDLE_VALUE)
      // always getting here!   <----------------------------------------------------------
      printf("invalid handle for read\n");

   /* extract folder creation date-time */
   status = GetFileTime(hFolder, &FileDate, NULL, NULL);

   if(status == 0)
      printf("error getting filetime\n");

   /* done with handle */

   /* convert filetime to readable date format */
   FileTimeToSystemTime(&FileDate, &MSDate);

   /* display date/time */
   printf("year = %d\n", MSDate.wYear);
   printf("mon  = %d\n", MSDate.wMonth);
   printf("day  = %d\n", MSDate.wDay);
   printf("hour = %d\n", MSDate.wHour);
   printf("min  = %d\n", MSDate.wMinute);
   printf("sec  = %d\n", MSDate.wSecond);

/**   Part-2 Modify Folder CrDate   **/

   /* set date-time to some value */
   MSDate.wYear   = 2005;
   MSDate.wMonth  = 9;
   MSDate.wDay    = 16;
   MSDate.wHour   = 11;
   MSDate.wMinute = 23;
   MSDate.wSecond = 2;

   /* convert specified date to filetime format */
   SystemTimeToFileTime(&MSDate, &FileDate);

   /* get "Generic-Write" handle to the folder */
   hFolder = CreateFile(folderpath, GENERIC_WRITE, FILE_SHARE_READ, NULL,

   if(hFolder == INVALID_HANDLE_VALUE)
      printf("invalid handle for write\n");

   /* set folder creation date-time */
   status = SetFileTime(hFolder, &FileDate, NULL, NULL);

   if(status == 0)
      printf("error setting filetime\n");

   /* done with handle */

/**   Done   **/



Question by:ToughCustomer
    LVL 86

    Accepted Solution

    From the docs on 'CreateFile()': "You can obtain a handle to a directory by setting the FILE_FLAG_BACKUP_SEMANTICS flag. A directory handle can be passed to some Win32 functions in place of a file handle."

    So, just make that

       /* get "Generic-Write" handle to the folder */
       hFolder = CreateFile(folderpath, GENERIC_WRITE, FILE_SHARE_READ, NULL,

    and you're there.

    Author Comment

    Well THAT wasn't obvious ...

    Can't argue with success though.  I made the same change
    to the set logic as well, and bang!  It works like a champ.

    ... all it takes is an expert!  Thanks a bunch.

    LVL 86

    Expert Comment

    You're most welcome ;o)

    This is one of the 'need to run into it at least once before you know' things on Windows.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    731 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

    18 Experts available now in Live!

    Get 1:1 Help Now