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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

718 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