Click here for important information about

Writing FileCreated and FileModified from VBScript?

May 15, 2012 at 07:11:55
Specs: Windows Server 2003 R2, Athlon XP dual core
I have a folder on a Windows 2003 R2 PC, which syncs with my phone; the phone takes pictures in jpg format and places them in the folder, and an app on my phone syncs whatever is there. I have cobbled together a script which executes as a scheduled task; the script reads the date taken property and sorts/renames my photos, and from here I can move them to a NAS and back them up for safekeeping. The date created property on the PC is relative to the time the file was synchronized, and not the time the picture was taken on the phone. I would like to have my script write to the date created (and date modified, just to keep things neat) the same info as is stored in date taken.

In my script, I have tried using:
objRecordSet.Fields.Item("System.DateCreated") = objRecordset("System.Photo.DateTaken")
The script laughs at me, saying "800A0CB3, Current recordset does not support updating. This may be a limitation of the provider, or the selected locktype."

Admittedly, much of the script I'm using has been copied, and I don't have the strongest grasp on the full capabilities of "objRecordSet.Open", so I assume I may need to do something different to be able to write to these properties? (So far my attempts at Googling haven't led me to a good guide on this either...)

'=== My Script ==='
Option Explicit
Dim objFSO, objConnection, objRecordSet, objFolder
Dim OS, timeZone
Dim FilePath, FileNameAndExtension, FileName, FileExtension
Dim strYear, strMonth, strDay, strHour, strMinute, strSec
Dim OldFileAndPath, NewFileAndPath, SubDirectory
Dim SplitFileNameAndExtension, SplitFolderPath, dtmPhotoDate

'Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
For Each OS In GetObject("winmgmts:Win32_OperatingSystem").Instances_
timeZone = OS.CurrentTimeZone
Next 'os

objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
objRecordSet.Open "SELECT System.ItemPathDisplay, System.DateCreated, System.ItemNameDisplay, System.DateModified, System.ItemFolderPathDisplay, System.Photo.DateTaken FROM SYSTEMINDEX Where System.ItemFolderPathDisplay = 'C:\Documents and Settings\Administrator\My Documents\Dropbox\DCIM\100MEDIA'", objConnection

'Start at the Beginning of the RecordSet

Do Until objRecordSet.EOF
OldFileAndPath = objRecordset.Fields.Item("System.ItemPathDisplay")
FilePath = objRecordset.Fields.Item("System.ItemFolderPathDisplay")
FileNameAndExtension = objRecordset.Fields.Item("System.ItemNameDisplay")
SplitFileNameAndExtension = Split(FileNameAndExtension, ".") 'Separate file name from extension
If UBound(SplitFileNameAndExtension) > 0 Then 'Check for extension
SplitFolderPath = Split(FilePath, "\")' Separate file path into folder list array
FileName = SplitFileNameAndExtension(0)
FileExtension = SplitFileNameAndExtension(1)

'Extract "Date Taken" info:
dtmPhotoDate = DateAdd("n", timeZone, objRecordset("System.Photo.DateTaken"))
strDay = Day(dtmPhotoDate)
strMonth = Month(dtmPhotoDate)
strYear = Year(dtmPhotoDate)
strHour = Hour(dtmPhotoDate)
strMinute = Minute(dtmPhotoDate)
strSec = Second(dtmPhotoDate)

'Update Formatting to two characters if necessary:
If Len(strDay) = 1 Then strDay = "0" & strDay
If Len(strMonth) = 1 Then strMonth = "0" & strMonth
If Len(strHour) = 1 Then strHour = "0" & strHour
If Len(strMinute) = 1 Then strMinute = "0" & strMinute
If Len(strSec) = 1 Then strSec = "0" & strSec

SubDirectory = FilePath & "\" & strYear & "-" & strMonth & "-" & strDay
NewFileAndPath = SubDirectory & "\" & strHour & "h" & strMinute & "m" & strSec & "s" & "." & FileExtension

If strYear <> "" Then
'objRecordSet.Fields.Item("System.DateCreated") = objRecordset("System.Photo.DateTaken")
'objRecordSet.Fields.Item("System.DateModified") = objRecordset("System.Photo.DateTaken")
If objFSO.FolderExists(SubDirectory) Then
Set objFolder = objFSO.GetFolder(SubDirectory)
Set objFolder = objFSO.CreateFolder(SubDirectory)
End If
'WScript.Echo "Copy from " & OldFileAndPath & " to " & NewFileAndPath
objFSO.MoveFile OldFileAndPath, NewFileAndPath
End If
End If
'Next file in the RecordSet:

See More: Writing FileCreated and FileModified from VBScript?

May 18, 2012 at 19:02:08

It took me several days of searching to finally come up with search terms that brought me something useful. According to this link, you can use the Shell object to change the modified date/timestamp. I was able to use this, and it works perfectly. Unfortunately, there is only the .modifydate property, and not a .createdate property. So I have managed to solve half of my question. Anyone know any tricks for updating the creation date?

Report •

May 22, 2012 at 06:29:52
I know it's possible in PowerShell, but I don't know of any scriptable object ("scriptable objects" = "VBScript") that allows you to modify file creation time. If you did use PowerShell, you could call the UTC time functions to handle the UTC -> Local time conversion for you.
[System.IO.File]::SetCreationTimeUtc("some.file",(Get-Date "1/1/2020 20:20:20"))

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question