VBScript File Existence Check

Posted on 2011-09-21
Last Modified: 2012-06-27
I have the following loop that SHOULD be checking for the existence of the file with each loop.  To test, I remove the file, start the program then immediately put the file back in the correct folder.  It loops 5 times but reports the file as not found.
' ==== verify required file exists, indicating previous process has completed
iLoopCtr = 1
lFound = "Y"
If (ucase(GetXMLText("Required\Enabled"))="TRUE") Then
         If GetXMLText("Required\FileName")<> "" Then
              For iLoopCtr = 1 to (GetXMLText("Required\Attempts"))
                    If objFSO.FileExists(GetXMLText("Required\FileName")) = True then
                            iLoopCtr = (GetXMLText("Required\Attempts")+1)
                            WScript.Sleep (GetXMLText("Required\WaitTime"))
                            lFound = "N"
                    End If
                OutputLogText " - Required File entry missing from XML file - " & NOW
          End If
          If lFound = "N" Then
                 OutputLogText " - Required File (" & GetXMLText("Required\FileName") & ") to proceed NOT found.  Load CANCELLED. - " & NOW
                  OutputLogText " - Required File to proceed found.  Load proceeding. - " & NOW
           End If
End if

Open in new window

Is something wrong with the loop?
Question by:kkbenj

Expert Comment

ID: 36576741
Try this, please.

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\FSO\ScriptLog.txt") Then
 Set objFile = objFSO.GetFile("C:\FSO\ScriptLog.txt")
 Wscript.Echo "File does not exist."
End If

Verifying That a File Exists
LVL 65

Accepted Solution

RobSampson earned 500 total points
ID: 36578039
Hi, I think you have some logic problems.  I think all you need to do is, above this line:
      iLoopCtr = (GetXMLText("Required\Attempts")+1)

add this:
      lFound = "Y"

because what is happening is when the file isn't found in the first loop, lFound becomes "N", but then is never reset back to "Y" if it *is* found.

Also, if you wanted to, you could change
      lFound = "Y"
      iLoopCtr = (GetXMLText("Required\Attempts")+1)

to just
      lFound = "Y"
      Exit For



Author Closing Comment

ID: 36712697
Thank you!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now