1
我有这个脚本工作完美,但后来我意识到,如果一个文件不存在,它的错误。我试图找出一种方法来适应这一点,但我不断收到各种错误消息。如何退出脚本如果文件不存在
以下是我最近的尝试。
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
我试图以各种不同的方式添加此语句,但我无法获得整个脚本来处理此添加。 该脚本重命名两个不同位置的两个不同的文件,我已成功获取它在第一次名称更改期间工作,但不是第二次。 我很快就回复了 - 我会在我的主帖中更新一些我的尝试。 – jodies
请详细描述你想要达到什么效果,哪些不能按预期工作。包括你得到的所有错误。如果你不知道自己在做什么,试图通过将我的代码片段放在随机位置来解决问题不太可能有所帮助。 –