【问题标题】:How to exit script if file doesn't exist如果文件不存在如何退出脚本
【发布时间】:2016-06-23 02:40:33
【问题描述】:

我让这个脚本完美运行,但后来我意识到如果文件不存在,它就会出错。我试图找出一种方法来适应这种情况,但我不断收到各种错误消息。

以下是我最近的尝试。

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】:

    如果文件不存在就退出:

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

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

    【讨论】:

    • 我尝试以各种不同的方式添加此语句,但我无法让整个脚本与此添加一起工作。脚本在两个不同的位置重命名两个不同的文件,我一直在第一次更名期间成功地让它工作,但不是第二次。我按 Enter 太快了 - 我将在我的主帖中更新我的一些尝试。
    • 请详细描述您想要实现的目标以及无法按预期实现的目标。包括您遇到的所有错误。如果您不知道自己在做什么,尝试通过将我的代码 sn-p 放在随机位置来解决问题不太可能有帮助。
    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多