Solved

VBScript File Existence Check

Posted on 2011-09-21
3
340 Views
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)
                    Else
                            WScript.Sleep (GetXMLText("Required\WaitTime"))
                            lFound = "N"
                    End If
                Next
          Else
                OutputLogText " - Required File entry missing from XML file - " & NOW
                WScript.Quit
          End If
          If lFound = "N" Then
                 OutputLogText " - Required File (" & GetXMLText("Required\FileName") & ") to proceed NOT found.  Load CANCELLED. - " & NOW
                 WScript.Quit
           Else
                  OutputLogText " - Required File to proceed found.  Load proceeding. - " & NOW
           End If
End if

Open in new window


Is something wrong with the loop?
0
Comment
Question by:kkbenj
3 Comments
 
LVL 6

Expert Comment

by:jorgedeoliveiraborges
Comment Utility
Try this, please.

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

Verifying That a File Exists
http://technet.microsoft.com/en-us/library/ee198727.aspx
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
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

Regards.

Rob.
0
 

Author Closing Comment

by:kkbenj
Comment Utility
Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

15 Experts available now in Live!

Get 1:1 Help Now