如果文件不存在如何退出脚本

问题描述 投票:0回答: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
vbscript file-exists
1个回答
0
投票

如果文件不存在则退出:

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

如果您希望退出代码指示错误,请将 0 更改为 >0 的值。

© www.soinside.com 2019 - 2024. All rights reserved.