Reputation: 23
I am trying to create a script that will scan a directory for a list of specific file names and will then check whether or not the DateLastModified is newer than prior day.
If any of the files do not exist or do exist but are not 'fresh' then I would like a summary window to pop up at the end listing the file names that did not meet the criteria.
As an example we can say the path is C:\Testing\
and the Files I want to scan for are File1.txt, File2.txt and File3.txt
Note: Although these files all have the same extension I do not want to scan for them that way. I want to specifically identify the files in my script since there are other txt files located in the directory.
Update:Here is what I was able to create as far as handling one file at a time. Any help you can offer will be greatly appreciated.
Option Explicit
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim InputFile1
InputFile1 = ("C:\Testing\File1.txt")
Dim InputFile2
InputFile2 = ("C:\Testing\File2.txt")
Dim InputFile3
InputFile3 = ("C:\Testing\File3.txt")
Dim CurrentDate, OldDate
'* Default Date format is MM/DD/YYYY
CurrentDate = Date()
OldDate = Date() - 1
WScript.Echo ("The Current Date on the system is set to " & CurrentDate) & VbCrLf & ("Checking if the file is newer than " & OldDate)
If FSO.FileExists(InputFile1) Then
If CDATE (FSO.GetFile(InputFile1).DateLastModified) > OldDate Then
WScript.Echo ("All good!")
Else
WScript.Echo ("File is not current.")
End If
Else
WScript.Echo ("File does not exist.")
Wscript.Quit
End If
Upvotes: 2
Views: 2708
Reputation: 18827
And if you like to add a Logfile of course try like this :
Option Explicit
Dim Ws,FSO,files,file,oldDate,LogFile
Set Ws = CreateObject("Wscript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
files = Array("C:\Testing\File1.txt", "C:\Testing\File2.txt", "C:\Testing\File3.txt")
oldDate = Date() - 1
WScript.Echo "Checking if any files are older than " & OldDate
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
For Each file In files
If FSO.FileExists(file) Then
If FSO.GetFile(file).DateLastModified < oldDate Then
WScript.Echo "Not current: " & DblQuote(file)
WriteLog "Not current: " & DblQuote(file),LogFile
End If
Else
WScript.Echo "Not found: " & DblQuote(file)
WriteLog "Not found: " & DblQuote(file),LogFile
End If
Next
if fso.FileExists(LogFile) Then
Ws.Run DblQuote(LogFile)
end If
'**********************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'***********************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************************
EDIT : 26/04/2015 ==> Here is how to set a RootFolder into a variable
Option Explicit
Dim Ws,FSO,files,file,oldDate,LogFile,RootFolder
Set Ws = CreateObject("Wscript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
RootFolder = "c:\Testing"
Set RootFolder = fso.GetFolder(RootFolder)
oldDate = Date() - 1
WScript.Echo "Checking if any files are older than " & OldDate
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
For Each file In RootFolder.files
If FSO.FileExists(file) Then
If FSO.GetFile(file).DateLastModified < oldDate Then
WScript.Echo "Not current: " & DblQuote(file)
WriteLog "Not current: " & DblQuote(file),LogFile
End If
Else
WScript.Echo "Not found: " & DblQuote(file)
WriteLog "Not found: " & DblQuote(file),LogFile
End If
Next
if fso.FileExists(LogFile) Then
Ws.Run DblQuote(LogFile)
end If
'**********************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'***********************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************************
Upvotes: 2
Reputation: 338148
You could use an array and a loop.
Option Explicit
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim files, file, oldDate
files = Array("C:\Testing\File1.txt", "C:\Testing\File2.txt", "C:\Testing\File3.txt")
oldDate = Date() - 1
WScript.Echo "Checking if any files are older than " & OldDate
For Each file In files
If FSO.FileExists(file) Then
If FSO.GetFile(file).DateLastModified < oldDate Then
WScript.Echo "Not current: " & file
End If
Else
WScript.Echo "Not found: " & file
End If
Next
General tip: If you find yourself creating variables with a trailing counter (file1
, file2
, ...) then it's very probably that you actually want an array.
Upvotes: 1