(view source code of clonedate.vbs as plain text)
Option Explicit
' Lots and lots of variables, most of them required to allow wildcards
Dim arrTgtFiles
Dim dtmFileDate
Dim i
Dim colTgtFiles, objFile, objFolder, objFSO, objRegEx, objShell
Dim objFSTgtFolder, objSrcFile, objSrcFolder, objTgtFile, objTgtFolder
Dim strSrcFile, strSrcFolder, strTgtFiles, strTgtFolder, strTgtStr
' We'll use the Windows Shell as well as the FileSystem Object to handle files
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objShell = CreateObject( "Shell.Application" )
' A Regular Expression is used to check if file names match wildcards
Set objRegEx = New RegExp
objRegEx.IgnoreCase = True
objRegEx.Global = True
With WScript.Arguments
' 2 command line arguments are required
If .Named.Count > 0 Then Syntax ""
If .Unnamed.Count = 0 Then Syntax ""
If .Unnamed.Count <> 2 Then Syntax "Wrong number of command line arguments"
If Not objFSO.FileExists( .Unnamed(0) ) Then Syntax "Source file not found (" & .Unnamed(0) & ")"
strSrcFolder = objFSO.GetParentFolderName( objFSO.GetAbsolutePathName( .Unnamed(0) ) )
Set objSrcFolder = objShell.NameSpace( strSrcFolder )
strTgtFolder = objFSO.GetParentFolderName( objFSO.GetAbsolutePathName( .Unnamed(1) ) )
Set objTgtFolder = objShell.NameSpace( strTgtFolder )
strSrcFile = objFSO.GetFileName( .Unnamed(0) )
Set objSrcFile = objSrcFolder.ParseName( strSrcFile )
dtmFileDate = objSrcFile.ModifyDate
If InStr( .Unnamed(1), "?" ) Or InStr( .Unnamed(1), "*" ) Then
' Handle wildcards in the target filespec
strTgtFolder = objFSO.GetAbsolutePathName( .Unnamed(1) & "\.." )
If Not objFSO.FolderExists( strTgtFolder ) Then Syntax
Set objFSTgtFolder = objFSO.GetFolder( strTgtFolder )
If objFSTgtFolder.Files.Count < 1 Then Syntax
arrTgtFiles = Split( .Unnamed(1), "\" )
strTgtFiles = arrTgtFiles( UBound( arrTgtFiles ) )
' Convert the filespec with DOS wildcards to a RegEx pattern
objRegEx.Pattern = "^" & Replace( strTgtFiles, "*", "\w*" )
objRegEx.Pattern = Replace( objRegEx.Pattern, "$", "\$" )
objRegEx.Pattern = Replace( objRegEx.Pattern, "?", "\w?" ) & "$"
strTgtStr = ""
For Each objFile In objFSTgtFolder.Files
If objRegEx.Test( objFile.Name ) Then
If LCase( objFile.Name ) <> LCase( strSrcFile ) Then
If objFSO.FileExists( objFile.Path ) Then
strTgtStr = strTgtStr & "," & objFile.Name
End If
End If
End If
Next
If strTgtStr = "" Then Syntax "No files found matching " & .Unnamed(1)
strTgtStr = Mid( strTgtStr, 2 )
arrTgtFiles = Split( strTgtStr, "," )
Else
' Single target file
If Not objFSO.FileExists( .Unnamed(1) ) Then Syntax "File " & .Unnamed(1) & " not found"
strTgtFiles = objFSO.GetFileName( .Unnamed(1) )
arrTgtFiles = Array( strTgtFiles )
End If
' For every matching file . . .
For i = 0 To UBound( arrTgtFiles )
Set objTgtFile = objTgtFolder.ParseName( arrTgtFiles(i) )
' . . . change the timestamp (LastModified date)
objTgtFile.ModifyDate = dtmFileDate
Next
End With
Set objRegEx = Nothing
Set objShell = Nothing
Set objFSO = Nothing
Sub Syntax( errMsg )
Dim strMsg
If Trim( errMsg ) <> "" Then strMsg = vbCrLf & "ERROR: " & errMsg & vbCrLf
strMsg = strMsg & vbCrLf _
& "CloneDate, Version 2.11" _
& vbCrLf _
& "Modify the LastModified date (timestamp) of the target file(s) to" _
& vbCrLf _
& "match the specified source file's timestamp" _
& vbCrLf & vbCrLf _
& "Usage: CLONEDATE.VBS sourcefile targetfiles" _
& vbCrLf & vbCrLf _
& "Where: ""sourcefile"" is the file whose timestamp is to be cloned" _
& vbCrLf _
& " ""targetfiles"" is (are) the file(s) whose timestamp is to" _
& vbCrLf _
& " be modified (wildcards * and ? are allowed)" _
& vbCrLf & vbCrLf _
& "Example: CLONEDATE.VBS C:\boot.ini C:\test.log" _
& vbCrLf _
& " will change C:\test.log's timestamp" _
& vbCrLf & vbCrLf _
& "Notes: sourcefile may be hidden, but hidden targetfiles will be skipped." _
& vbCrLf _
& " If targetfiles includes sourcefile, sourcefile will be skipped." _
& vbCrLf _
& " Script based on an article by the Scripting Guys in the June 2008" _
& vbCrLf _
& " issue of the Dutch TechNet Magazine." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0116 seconds