WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: User_McUser on February 12, 2024, 12:11:55 PM

Title: FileTimeSet but for Directories
Post by: User_McUser on February 12, 2024, 12:11:55 PM
Is there a function that is the equivalent of FileTimeSet but for directories instead of files?

Doesn't seem like there is but thought I'd ask to be sure.

Thanks! 👍
Title: Re: FileTimeSet but for Directories
Post by: td on February 12, 2024, 02:04:41 PM
You can try the following.

Code (winbatch) Select
; Date to set
Date = ObjectClrNew("System.DateTime", 2001, 1, 1, 8, 0, 15)

; Set the date on the target directory
Dir = ObjectClrNew("System.IO.Directory")
Dir.SetCreationTime("C:\Temp\Trash", Date)

; Make sure that no process is using the directory.
; This includes Windows File Explorer not having a
; window open to it.
Title: Re: FileTimeSet but for Directories
Post by: User_McUser on February 13, 2024, 01:29:26 PM
I actually need to change the Last Modified date on the directory - but with your example I was able to find the Microsoft Directory Class documentation (https://learn.microsoft.com/en-us/dotnet/api/system.io.directory) online and used the "SetLastWriteTime" method in place of "SetCreationTime" and it works perfectly.  :D

Thank you!

Code (winbatch) Select

Date = ObjectClrNew("System.DateTime", 1999,2,14,9,38,26)

Dir = ObjectClrNew("System.IO.Directory")
Dir.SetLastWriteTime("C:\temp", Date)
Title: Re: FileTimeSet but for Directories
Post by: td on February 13, 2024, 01:58:37 PM
Great. You took the initiative and found the solution that best fits your needs.