www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - SetFileTime Help

reply Steve Topilnycky <no.spam.steve topcatcomputing.com> writes:
I have been working on this function to update the time stamp of a file.  
I can open the file, but each time SetFileTime is called, it reports an 
"Invalid Handle".  What are the correct "Declarations to get the 
SetFileTime function to work correctly? Please help. Thanks,

char strFile2Touch[255];
   int result;
   HFILE hfile;
   OFSTRUCT info;
   SYSTEMTIME st;
   FILETIME ft;

  st.wYear=atoi(strYr);
  st.wMonth=atoi(strMon);
  st.wDay=atoi(strDay);
  st.wHour=atoi(strHr);
  st.wMinute=atoi(strMin);
  st.wSecond=atoi(strSec);


  SystemTimeToFileTime(&st, &ft);
  sprintf(strFile2Touch,"%s%s",strlwr(strDLFolder), strlwrstrFileName));
   
	   if(hfile=OpenFile(strFile2Touch, &info, OF_WRITE) != -1){
            printf("File Open..Setting Time\n");
            result=SetFileTime((HANDLE)hfile, (LPFILETIME)
&ft,LPFILETIME)&ft, LPFILETIME)&ft);
		  if (result)
		  reportError("Time Stamp Error");
               else
	      printf("Time Stamp Error\n");
	   }

     CloseHandle((HANDLE) hfile);

-- 

Regards,

Steve Topilnycky             
Top Cat Computing
Web:  http://www.topcatcomputing.com/




-- 

Regards,

Steve Topilnycky             
Top Cat Computing
Web:  http://www.topcatcomputing.com/
Jul 10 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
Your problem is that OpenFile is a Win16 function, and not compatible with
Win32. It returns an HFILE, whereas the Win32 API uses HANDLE. (It may be
the case that they're both void* under the covers, but the difference in
name indicates the different semantic.)

Use CreateFile(), as in

  HANDLE hfile = CreateFile(strFile2Touch, GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);

btw, if you're interested, there's a nifty shell-extension at
http://shellext.com will do touching of single or multiple files from with
the Explorer shell.


"Steve Topilnycky" <no.spam.steve topcatcomputing.com> wrote in message
news:MPG.1977847a5d1418fd989686 news.digitalmars.com...
 I have been working on this function to update the time stamp of a file.
 I can open the file, but each time SetFileTime is called, it reports an
 "Invalid Handle".  What are the correct "Declarations to get the
 SetFileTime function to work correctly? Please help. Thanks,

 char strFile2Touch[255];
    int result;
    HFILE hfile;
    OFSTRUCT info;
    SYSTEMTIME st;
    FILETIME ft;

   st.wYear=atoi(strYr);
   st.wMonth=atoi(strMon);
   st.wDay=atoi(strDay);
   st.wHour=atoi(strHr);
   st.wMinute=atoi(strMin);
   st.wSecond=atoi(strSec);


   SystemTimeToFileTime(&st, &ft);
   sprintf(strFile2Touch,"%s%s",strlwr(strDLFolder), strlwrstrFileName));

    if(hfile=OpenFile(strFile2Touch, &info, OF_WRITE) != -1){
             printf("File Open..Setting Time\n");
             result=SetFileTime((HANDLE)hfile, (LPFILETIME)
 &ft,LPFILETIME)&ft, LPFILETIME)&ft);
   if (result)
   reportError("Time Stamp Error");
                else
       printf("Time Stamp Error\n");
    }

      CloseHandle((HANDLE) hfile);

 -- 

 Regards,

 Steve Topilnycky
 Top Cat Computing
 Web:  http://www.topcatcomputing.com/




 -- 

 Regards,

 Steve Topilnycky
 Top Cat Computing
 Web:  http://www.topcatcomputing.com/
Jul 10 2003
parent reply Steve Topilnycky <no.spam.steve topcatcomputing.com> writes:
In the c++.windows.32-bits newsgroup Matthew Wilson wrote: 

 
   HANDLE hfile = CreateFile(strFile2Touch, GENERIC_WRITE, FILE_SHARE_READ,
 NULL, OPEN_EXISTING, 0, NULL);
 
Will using CreateFile destroy the original file.. I hope not I am going to try it now. I'm using this function to preserve the time stamp on a downloaded files from within my app. I did see CreateFile, but I took it as "Creating a New file" not just opening one for touching.. Thanks for the links too, I will be sure to check them out. -- Regards, Steve Topilnycky Top Cat Computing Web: http://www.topcatcomputing.com/
Jul 10 2003
parent reply Steve Topilnycky <no.spam.steve topcatcomputing.com> writes:
In the c++.windows.32-bits newsgroup Steve Topilnycky wrote: 

 
 Will using CreateFile destroy the original file..
 
It worked.. Thanks.. -- Regards, Steve Topilnycky Top Cat Computing Web: http://www.topcatcomputing.com/
Jul 10 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
You're most welcome.

Just remember that CreateFile returns INVALID_HANDLE_VALUE when it fails,
and *not* NULL.

:)

P.S. If you install the shell extensions, please feel free to give me any
feedback, and to register for the mailing list, if you wish.

"Steve Topilnycky" <no.spam.steve topcatcomputing.com> wrote in message
news:MPG.1977e5de2d9dd7a7989688 news.digitalmars.com...
 In the c++.windows.32-bits newsgroup Steve Topilnycky wrote:


 Will using CreateFile destroy the original file..
It worked.. Thanks.. -- Regards, Steve Topilnycky Top Cat Computing Web: http://www.topcatcomputing.com/
Jul 10 2003
parent Steve Topilnycky <no.spam.steve topcatcomputing.com> writes:
In the c++.windows.32-bits newsgroup Matthew Wilson wrote: 

 
 Just remember that CreateFile returns INVALID_HANDLE_VALUE when it fails,
 and *not* NULL.
 
 
Actually, in this case, I wanted a "silent" response if it fails. The one thing I noticed it that if I pass the time stamp of the file (broken down as shown in my example, say 8 PM, when I check the file, it shows 4 PM. Almost as if it is converting the hours to UTC.. Any thoughts on that? I have the app out to my testers on different OS's and see what results they get. I do recall one tidbit where NTFS saves the time in UTS.. But I passed the Exact to to the function that I wanted to save.. Other than than, it works perfectly.. -- Regards, Steve Topilnycky Top Cat Computing Web: http://www.topcatcomputing.com/
Jul 10 2003