我正在尝试创建一个脚本,该脚本将扫描目录以查找特定文件名列表,然后检查 DateLastModified 是否比前一天更新。
如果任何文件不存在或确实存在但不“新鲜”,那么我希望在最后弹出一个摘要窗口,列出不符合条件的文件名。
作为示例,我们可以说路径是 C:\Testing\
我要扫描的文件是 File1.txt、File2.txt 和 File3.txt
注意:虽然这些文件都具有相同的扩展名,但我不想以这种方式扫描它们。我想专门识别脚本中的文件,因为目录中还有其他 txt 文件。
更新:这是我能够创建的一次处理一个文件的内容。我们将非常感谢您提供的任何帮助。
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
最佳答案
如果您想添加日志文件,当然可以尝试这样:
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
'***********************************************************
编辑:26/04/2015 ==> 以下是如何将 RootFolder 设置为变量
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
'***********************************************************
关于VBscript 用于确认文件列表存在且已在当天修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862265/