Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for clonedate.vbs

(view source code of clonedate.vbs as plain text)

  1. Option Explicit
  2.  
  3. ' Lots and lots of variables, most of them required to allow wildcards
  4. Dim arrTgtFiles
  5. Dim dtmFileDate
  6. Dim i
  7. Dim colTgtFiles, objFile, objFolder, objFSO, objRegEx, objShell
  8. Dim objFSTgtFolder, objSrcFile, objSrcFolder, objTgtFile, objTgtFolder
  9. Dim strSrcFile, strSrcFolder, strTgtFiles, strTgtFolder, strTgtStr
  10.  
  11. ' We'll use the Windows Shell as well as the FileSystem Object to handle files
  12. Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  13. Set objShell = CreateObject( "Shell.Application" )
  14. ' A Regular Expression is used to check if file names match wildcards
  15. Set objRegEx = New RegExp
  16. objRegEx.IgnoreCase = True
  17. objRegEx.Global     = True
  18.  
  19. With WScript.Arguments
  20. 	' 2 command line arguments are required
  21. 	If .Named.Count    > 0 Then Syntax ""
  22. 	If .Unnamed.Count  = 0 Then Syntax ""
  23. 	If .Unnamed.Count <> 2 Then Syntax "Wrong number of command line arguments"
  24. 	If Not objFSO.FileExists( .Unnamed(0) ) Then Syntax "Source file not found (" & .Unnamed(0) & ")"
  25. 	strSrcFolder = objFSO.GetParentFolderName( objFSO.GetAbsolutePathName( .Unnamed(0) ) )
  26. 	Set objSrcFolder = objShell.NameSpace( strSrcFolder )
  27. 	strTgtFolder = objFSO.GetParentFolderName( objFSO.GetAbsolutePathName( .Unnamed(1) ) )
  28. 	Set objTgtFolder = objShell.NameSpace( strTgtFolder )
  29. 	strSrcFile = objFSO.GetFileName( .Unnamed(0) )
  30. 	Set objSrcFile = objSrcFolder.ParseName( strSrcFile )
  31. 	dtmFileDate = objSrcFile.ModifyDate
  32. 	If InStr( .Unnamed(1), "?" ) Or InStr( .Unnamed(1), "*" ) Then
  33. 		' Handle wildcards in the target filespec
  34. 		strTgtFolder = objFSO.GetAbsolutePathName( .Unnamed(1) & "\.." )
  35. 		If Not objFSO.FolderExists( strTgtFolder ) Then Syntax
  36. 		Set objFSTgtFolder = objFSO.GetFolder( strTgtFolder )
  37. 		If objFSTgtFolder.Files.Count < 1 Then Syntax
  38. 		arrTgtFiles = Split( .Unnamed(1), "\" )
  39. 		strTgtFiles = arrTgtFiles( UBound( arrTgtFiles ) )
  40. 		' Convert the filespec with DOS wildcards to a RegEx pattern
  41. 		objRegEx.Pattern = "^" & Replace( strTgtFiles, "*", "\w*" )
  42. 		objRegEx.Pattern = Replace( objRegEx.Pattern, "$", "\$" )
  43. 		objRegEx.Pattern = Replace( objRegEx.Pattern, "?", "\w?" ) & "$"
  44. 		strTgtStr = ""
  45. 		For Each objFile In objFSTgtFolder.Files
  46. 			If objRegEx.Test( objFile.Name ) Then
  47. 				If LCase( objFile.Name ) <> LCase( strSrcFile ) Then
  48. 					If objFSO.FileExists( objFile.Path ) Then
  49. 						strTgtStr = strTgtStr & "," & objFile.Name
  50. 					End If
  51. 				End If
  52. 			End If
  53. 		Next
  54. 		If strTgtStr = "" Then Syntax "No files found matching " & .Unnamed(1)
  55. 		strTgtStr   = Mid( strTgtStr, 2 )
  56. 		arrTgtFiles = Split( strTgtStr, "," )
  57. 	Else
  58. 		' Single target file
  59. 		If Not objFSO.FileExists( .Unnamed(1) ) Then Syntax "File " & .Unnamed(1) & " not found"
  60. 		strTgtFiles = objFSO.GetFileName( .Unnamed(1) )
  61. 		arrTgtFiles = Array( strTgtFiles )
  62. 	End If
  63. 	' For every matching file . . .
  64. 	For i = 0 To UBound( arrTgtFiles )
  65. 		Set objTgtFile = objTgtFolder.ParseName( arrTgtFiles(i) )
  66. 		' . . . change the timestamp (LastModified date)
  67. 		objTgtFile.ModifyDate = dtmFileDate
  68. 	Next
  69. End With
  70.  
  71. Set objRegEx = Nothing
  72. Set objShell = Nothing
  73. Set objFSO   = Nothing
  74.  
  75.  
  76. Sub Syntax( errMsg )
  77. 	Dim strMsg
  78. 	If Trim( errMsg ) <> "" Then strMsg = vbCrLf & "ERROR: " & errMsg & vbCrLf
  79. 	strMsg = strMsg & vbCrLf _
  80. 	       & "CloneDate,  Version 2.11" _
  81. 	       & vbCrLf _
  82. 	       & "Modify the LastModified date (timestamp) of the target file(s) to" _
  83. 	       & vbCrLf _
  84. 	       & "match the specified source file's timestamp" _
  85. 	       & vbCrLf & vbCrLf _
  86. 	       & "Usage:    CLONEDATE.VBS  sourcefile targetfiles" _
  87. 	       & vbCrLf & vbCrLf _
  88. 	       & "Where:    ""sourcefile""   is the file whose timestamp is to be cloned" _
  89. 	       & vbCrLf _
  90. 	       & "          ""targetfiles""  is (are) the file(s) whose timestamp is to" _
  91. 	       & vbCrLf _
  92. 	       & "                         be modified (wildcards * and ? are allowed)" _
  93. 	       & vbCrLf & vbCrLf _
  94. 	       & "Example:  CLONEDATE.VBS  C:\boot.ini  C:\test.log" _
  95. 	       & vbCrLf _
  96. 	       & "          will change C:\test.log's timestamp" _
  97. 	       & vbCrLf & vbCrLf _
  98. 	       & "Notes:    sourcefile may be hidden, but hidden targetfiles will be skipped." _
  99. 	       & vbCrLf _
  100. 	       & "          If targetfiles includes sourcefile, sourcefile will be skipped." _
  101. 	       & vbCrLf _
  102. 	       & "          Script based on an article by the Scripting Guys in the June 2008" _
  103. 	       & vbCrLf _
  104. 	       & "          issue of the Dutch TechNet Magazine." _
  105. 	       & vbCrLf & vbCrLf _
  106. 	       & "Written by Rob van der Woude" & vbCrLf _
  107. 	       & "http://www.robvanderwoude.com"
  108. 	WScript.Echo strMsg
  109. 	WScript.Quit 1
  110. End Sub
  111.  

page last modified: 2024-02-26; loaded in 0.0225 seconds