
script to update a file's last modified date
Quote:
> Hi,
> I am new to scripting.
> I would like to get a script to check the last modified date of a file.
> If the file is less than a specified date, I would like to update it with
a
> different date.
> Please give me some examples.
You need to get a copy of the ported UNIX utility called touch.exe. I wont
attach a binary to this post, but if you can't find it, I'll email you
directly.
You can run touch from the command line, or as I've done, create a VBS
wrapper. The below script expects the touch.exe file to be in the same
folder that it is in. Use the script by dropping a file top of it, or by
setting it up in the "Send To" context folder. It will change any file's
date to today's date.
With a little modification, you can have it sort through a given folder and
set the date to something other that today's date. Search for posts under my
name relating to deleting files older that a certain date. Those examples
will contain the appropriate subroutines to find files X days old or older.
[--- Begin: Touch.VBS ---]
001. ' Windows Script Host - VBScript
002.
'---------------------------------------------------------------------------
-----
003. ' Name: Touch
004. ' By: Harvey Colwell
005. ' Version: 1.0
006. ' CopyRight: (c) Jul 2000, All Rights Reserved!
007. '
008.
'***************************************************************************
*****
009.
010. Option Explicit
011.
012. Dim oWS, oWN, oFS, oSA
013.
014. Set oWS = WScript.CreateObject("WScript.Shell")
015. Set oWN = WScript.CreateObject("WScript.Network")
016. Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
017. '----------
018. ' Script SetUp
019. '----------
020. Dim sTouchPgm, sFile
021.
022. sTouchPgm = WScript.ScriptFullName
023. sTouchPgm = GetShortPath(Left(sTouchPgm, InStrRev(sTouchPgm, "\")) &
"touch.exe")
024. If Not IsFile(sTouchPgm) then Call CleanUp("ERROR: Could not find the
required file!" & vbCrlf & vbCrlf & sTouchPgm)
025.
026. If Wscript.Arguments.Count = 0 Then Call CleanUp("ERROR: To use this
script, drag a file onto it!")
027. sFile = Wscript.Arguments(0)
028. If Not IsFile(sFile) Then Call CleanUp("ERROR: The specified file
does not exist!" & vbCrlf & vbCrlf & sFile)
029.
030. '----------
031. ' Touch File Date
032. '----------
033. Call Touch(GetShortPath(sFile))
034.
035. '----------
036. ' Clean Up
037. '----------
038.
039. Call CleanUp("")
040.
041.
'---------------------------------------------------------------------------
-----
042. ' Subroutines
043.
'***************************************************************************
*****
044.
045. '---------------------
046. Sub CleanUp(sExitMsg)
047. If Not sExitMsg = "" Then MsgBox sExitMsg,,"Exit Message"
048.
049. Set oWS = Nothing
050. Set oWN = Nothing
051. Set oFS = Nothing
052. WScript.Quit
053. End Sub
054.
055. '---------------------
056. Sub Touch(fName)
057. oWS.Run sTouchPgm & " " & fName, 0, True
058. End Sub
059.
060.
'---------------------------------------------------------------------------
-----
061. ' Functions
062.
'***************************************************************************
*****
063.
064. '---------------------
065. Function IsFile(fName)
066. If oFS.FileExists(fName) Then IsFile = True Else IsFile = False
067. End Function
068.
069. '---------------------
070. Function GetShortPath(pName)
071. GetShortPath = oFS.GetFile(pName).ShortPath
072. End Function
073.
074.
075.
'***************************************************************************
*****
[--- End: Touch.VBS ---]