[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2947
  • Last Modified:

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


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>

main()
{
   HANDLE      hFolder;
   FILETIME    FileDate;
   SYSTEMTIME  MSDate;

   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,
                        OPEN_EXISTING, FILE_ATTRIBUTE_DIRECTORY, NULL);

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

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

   if(status == 0)
   {
      printf("error getting filetime\n");
      exit(-2);
   }

   /* done with handle */
   CloseHandle(hFolder);

   /* 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,
                        OPEN_EXISTING, FILE_ATTRIBUTE_DIRECTORY, NULL);

   if(hFolder == INVALID_HANDLE_VALUE)
   {
      printf("invalid handle for write\n");
      exit(-3);
   }

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

   if(status == 0)
   {
      printf("error setting filetime\n");
      exit(-4);
   }

   /* done with handle */
   CloseHandle(hFolder);


/**************/
/**   Done   **/
/**************/

   exit(0);

}

0
ToughCustomer
Asked:
ToughCustomer
  • 2
1 Solution
 
jkrCommented:
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,
                        OPEN_EXISTING, FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS, NULL);

and you're there.
0
 
ToughCustomerAuthor Commented:
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.

-TC
0
 
jkrCommented:
You're most welcome ;o)

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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