Changing creation date on a folder

I am trying to change the creation date of a folder and it is not working. Changing the creation date of files is not a problem.

Is there any reason why this quick test shouldn’t work? LastErrorCode is 0.

Any help would be appreciated.

Dim f as FolderItem

f = GetFolderItem(“C:\FolderName”)

Dim newDate As New Date
newDate.Year = 1999
newDate.Month = 9
newDate.Day = 9
f.CreationDate = newDate
MsgBox Str(f.LastErrorCode)

Is the file open? On WIndows you can’t change the creation date if it is.

On AppleScript, here’s what I got for CreationDate and Modification date:

[b]creation date[/b] (date, r/o) : the date on which the item was created [b]modification date[/b] (date) : the date on which the item was last modified

I am not saying that it is not possible to change the creation date, but you have to make a quest on Apple Developer web about it to make sure (IMHO).

Is f really <> Nil?

In a Method:

[code]Dim f as FolderItem

f = GetFolderItem(“C:\FolderName”)
If f = Nil Then
MsgBox Str(f.LastErrorCode)
Return
End If

Dim newDate As New Date
newDate.Year = 1999
newDate.Month = 9
newDate.Day = 9
f.CreationDate = newDate

If f .LastErrorCode <> 0 Then
MsgBox Str(f.LastErrorCode)
End If
[/code]

The code above works fine on El Capitan (in a button):

I added to a Project a PushButton,
I pasted Sasha code (read below) to its Action Event,
I created a folder beside the project and I called it “FolderName”,
Then run the project and clicked in the PushButton.

The create date is now 1999-09-09 as asked.

[code]Sub Action()
Dim f as FolderItem

f = GetFolderItem(“FolderName”)
If f = Nil Then
MsgBox Str(f.LastErrorCode)
Return
End If

Dim newDate As New Date
newDate.Year = 1999
newDate.Month = 9
newDate.Day = 9
f.CreationDate = newDate

If f .LastErrorCode <> 0 Then
MsgBox Str(f.LastErrorCode)
End If
End Sub[/code]

Move the file to the temp folder, open in binarystream with read mode, open another binarystream in the folder it came from and write the file back. The creation date will be the moment it was written.

Michael, out of interest, how are you viewing the Creation Date of the folder?

Works fine here W10. Don’t forget that the default view of explorer is “Date Modified” not “Date created”. If you add the Column into the explorer view you can see the correctly changed creation date. Maybe you’re looking at the wrong data?

I’ve tried several things with no luck. I’m assuming that this is a bug in the build I’m using (REALStudio 2011R1).

The folder is not in use, it is not nil, it does exist and there are no errors generated. Getting the creation date from Windows Explorer. Even added to code pop-up with the creation date in case Explorer was misbehaving. It’s simply not happening.

Thanks for the help!

Letting us know the version at the start might help :stuck_out_tongue: I can’t test that far back sorry, I don’t have a licence that old, I tested it on 2019r1 and it worked fine.

You could do it quite trivially in declares if its not working for you https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-setfiletime

[quote=437425:@Michael Brogdon]Dim f as FolderItem

f = GetFolderItem(“C:\FolderName”)

Dim newDate As New Date
newDate.Year = 1999
newDate.Month = 9
newDate.Day = 9
f.CreationDate = newDate
[/quote]
Your code works in RS2010r5.1, which is pretty close to the version you’re using. Windows 10.

You can do the same with folders using FolderItem.CreateAsFolder

Well it works for me on my own machine configuration, but fails on the few client configurations that I have. It gets flagged in the logs as a security violation.

Any chance a security issue is causing you issues?

What OS are you seeing this problem in? James, is there a more detailed error than a security violation? Does the call still return 0 when it errors?

Windows 7 Pro, 64-bit.

I appreciate that you are still interested in this.

Yesterday I found that I could set the creation date under certain circumstances, but it was the end of the day and I couldn’t see a pattern or cause. It seems that if I reference the parent folder first, then the child folder (the true target), it will work at least sometimes.

Could you give this a try, (create the C:\FolderName folder first) if/when it fails, could you post the debug output here?

https://www.dropbox.com/s/jwph9h8z1imlyhh/TestSetFileTime.xojo_binary_project?dl=0

Windows 10:

18:47:35 : My Application.exe Launched
Trying >C:\FolderName<
GetLastError CreateFileW=2
18:47:43 : My Application.exe Ended

Did you create the folder in c:\ called FolderName Andre?

I didn’t made it by hand, i thought your program did that. But now i have done it and the message is now:
19:13:09 : My Application.exe Launched
Trying >C:\FolderName<
CreateFileW=316
SetFileTime=1

Cool Andre, that worked, thanks. My code was more for those that have issues with the original not working to see if my rewrite works but thanks for trying :slight_smile:

I’ll alter the code and incorporate the original test in it though, thanks for reminding me!

I could not figure out what exactly was allowing the folder creation date to be set sometimes and not other times using just RB.

Using declares worked for me as well. Thanks, Julian!