2016-03-08 56 views
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 

回答

0

只是退出,如果文件不存在:

If Not fso.FileExists(recentFile) Then 
    WScript.Quit 0 
End If 

更改0的值> 0,如果你想退出代碼表示有錯誤。

+0

我試圖以各種不同的方式添加此語句,但我無法獲得整個腳本來處理此添加。 該腳本重命名兩個不同位置的兩個不同的文件,我已成功獲取它在第一次名稱更改期間工作,但不是第二次。 我很快就回復了 - 我會在我的主帖中更新一些我的嘗試。 – jodies

+0

請詳細描述你想要達到什麼效果,哪些不能按預期工作。包括你得到的所有錯誤。如果你不知道自己在做什麼,試圖通過將我的代碼片段放在隨機位置來解決問題不太可能有所幫助。 –