我让这个脚本运行得很好,但后来我意识到如果文件不存在,就会出错。我试图找到一种方法来适应这种情况,但我不断收到各种错误信息。
以下是我最近的尝试。
Dim fso, folder, file, todaysDate, recentFile, folder1, folder2, folderName1, folderName2
Dim folderName, searchFileName, renameFileTo
folderName1 = "C:\Lif\TMI\"
folderName2 = "C:\Lif\TMA\"
todaysDate = Date()
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.GetFolder(folderName1)
Set folder2 = fso.GetFolder(folderName2)
Set recentFile = Nothing
For Each file In folder1.Files
If (recentFile Is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate Then
Set recentFile = file
End If
Exit For
Next
If fso.FileExists(recentfile) Then
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
End If
For Each file In folder2.Files
If (recentFile Is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate Then
Set recentFile = file
End If
Exit For
Next
If fso.FileExists(recentfile) Then
recentFile.Name = Replace(recentFile.Name, "_", "A_")
End If
我也试过这个:
For Each file In folder1.Files
If fso.FileExists(file) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate Then
Set recentFile = file
Exit For
End IF
Next
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
我曾尝试实现@Answar建议的脚本,但我无法弄清楚如何让这两个名称更改都可以使用。
我为这个长度道歉,但我想展示我尝试过的一切。
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
End IF
Exit For
Next
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
If Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
和
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
ElseIf Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
Exit For
Next
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
和
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
End IF
Exit For
Next
If recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
ElseIf Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
和
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
End IF
Exit For
Next
For each recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt") Then
If fso.FileExists(recentFile)
WScript.Quit 0
End If
Exit For
和
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
ElseIf fso.FileExists(recentFile) Then
End IF
Exit For
Next
和
For each file In folder1.Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate then
Set recentFile = file
End IF
Exit For
Next
If recentFile.Name Then
Replace(recentFile.Name, ".txt", "A.txt")
ElseIf Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
和
For Each file In folder1.Files
If (recentFile Is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate Then
Set recentFile = file
End If
Exit For
Next
If fso.FileExists(recentfile) Then
recentFile.Name = Replace(recentFile.Name, ".txt", "A.txt")
End If
For Each file In folder2.Files
If (recentFile Is Nothing) Then
Set recentFile = file
ElseIf DateValue (file.DateLastModified) = todaysDate Then
Set recentFile = file
End If
Exit For
Next
If fso.FileExists(recentfile) Then
recentFile.Name = Replace(recentFile.Name, "_", "A_")
If Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
End If
答案 0 :(得分:0)
如果文件不存在,请退出:
If Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
如果您希望退出代码指示错误,请将0更改为值> 0。