'How to exit script if file doesn't exist
I had this script working perfectly, but then I realized that if a file doesn't exist, it errors. I have tried to figure out a way to accommodate for this, but I keep getting various error messages.
The following is my most recent attempt.
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
I have also tried this:
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")
I have tried to implement the script that @Answar suggested, but I can't figure out how to get both name changes to work with this.
I apologize for the length, but I wanted to show everything I have tried.
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
and
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")
and
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
and
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
and
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
and
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
and
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
Solution 1:[1]
Just exit if the file doesn't exist:
If Not fso.FileExists(recentFile) Then
WScript.Quit 0
End If
Change 0 to a value >0 if you want the exit code to indicates an error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ansgar Wiechers |
