WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Directory and Deltree UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DirTimeSet UDF

 Keywords: Set Time Date Folder Directory Dir FileTimeSet DirTimeSet 


;Change the time/date stamps for an existing directory (folder)
;
;Developers: IFICantBYTE and ferchl
;At the time of writing (05/01/2008), Winbatch only had commands to change the
;TimeDate stamps for files not folders - hence the creation of this script
;
;NOTE: This script has only been tested on Win XP with NTFS - it assumes you have create/modify rights
;it should be fine on NT4 Win2K and up , but it MAY NOT WORK ON Win9x/Me NT3 - you have been warned!

#DefineFunction DirTimeSet(Dir,CreatedYmdHms,ModifiedYmdHms,AccessedYmdHms)
   If WinVersion(1)<4 Then Return @FALSE

   ;First we create a temporary file that we can manipulate using the built-in Winbatch FileTime commands
   ;Then we can copy it's dates to our Directory using a couple of DLL calls.
   TempFile = FileCreateTemp("AAA")
   FileTimeSetEx(TempFile,CreatedYmdHms,1) ; Sets the created time to our value
   FileTimeSetEx(TempFile,ModifiedYmdHms,2) ; Sets the modified time to our value
   FileTimeSetEx(TempFile,AccessedYmdHms,3) ; Sets the last accessed time to our value
   ;Here comes the trick...
   ;Some API constants
   GENERIC_READ = 2147483648 ;&H80000000
   GENERIC_WRITE = 1073741824 ;&H40000000
   FILE_SHARE_READ = 1
   FILE_SHARE_DELETE = 4
   FILE_FLAG_BACKUP_SEMANTICS = 33554432 ;&H2000000 ; This may not be available in older versions of Windows
   OPEN_EXISTING = 3
   ;Make some TIME structures - or at least allocate the correct buffer sizes for the structures to fill later
   CTFILETIME = BinaryAlloc(8) ;Will contain the TimeStamp for Created Time
   LAFILETIME = BinaryAlloc(8) ;Will contain Last Accessed Time
   LWFILETIME = BinaryAlloc(8) ;Will contain Last Written (Modified) Time
   Kernel32 = StrCat(DirWindows(1),"kernel32.dll") ; path to kernel32.dll
   ;Get handles to the existing directory and the tempfile that are suitable to pass to the other DLL calls
   hDir = DllCall(Kernel32,long:"CreateFileA",lpstr:Dir,long:GENERIC_READ | GENERIC_WRITE,long:FILE_SHARE_READ | FILE_SHARE_DELETE,lpnull,long:OPEN_EXISTING,long:FILE_FLAG_BACKUP_SEMANTICS,lpnull)
   hTempFile = DllCall(Kernel32,long:"CreateFileA",lpstr:TempFile,long:GENERIC_READ | GENERIC_WRITE,long:FILE_SHARE_READ | FILE_SHARE_DELETE,lpnull,long:OPEN_EXISTING,long:FILE_FLAG_BACKUP_SEMANTICS,lpnull)
   ;Get the 3 timestamps from the TempFile we modified earlier and save them as FILETIME structures in the 3 Buffers we made
   DllCall(Kernel32,long:"GetFileTime",long:hTempFile,lpbinary:CTFILETIME,lpbinary:LAFILETIME,lpbinary:LWFILETIME)
   DllCall(Kernel32,long:"SetFileTime",long:hDir,lpbinary:CTFILETIME,lpbinary:LAFILETIME,lpbinary:LWFILETIME)
   ;Close the dir and file handles and delete the Temp File
   DllCall(Kernel32,long:"CloseHandle",long:hDir)
   DllCall(Kernel32,long:"CloseHandle",long:hTempFile)
   FileDelete(TempFile)
   ;Free our binary buffers
   BinaryFree(CTFILETIME)
   BinaryFree(LAFILETIME)
   BinaryFree(LWFILETIME)
   Return @TRUE
#EndFunction

; Test
Dir = StrCat("C:\Test",GetTickCount())
DirMake(Dir)
Pause("Test directory created","Click OK to change the directory's time stamp")
DirTimeSet(Dir,"2000:01:01:00:00:00","2001:01:01:00:00:00","2002:01:01:00:00:00")
Exit





Article ID:   W18355
Filename:   DirTimeSet UDF.txt
File Created: 2008:05:01:08:19:52
Last Updated: 2008:05:01:08:19:52