Re: How to repeatedly check for a condition before continuing? by Highlander
Highlander
Sat Feb 18 20:43:18 CST 2006
After testing this I'm not sure if that statement about error handling
is correct. Here's my full script, with your additional code labeled as
such:
(watch for word-wrap)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim path, FileToRead
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set TargetFile = objFSO.CreateTextFile ("Merged_Files.txt", True)
' Get list of files to merge
path = "C:\Files_To_Merge\"
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /C Dir /b " & path & " > FileList.txt"
Do While FileNotLocked <> True ' ~~~additional code~~~
On Error Resume Next ' ~~~additional code~~~
Set FileList = objFSO.OpenTextFile("FileList.txt", 1)
If Err.Number = 0 Then FileNotLocked = True ' ~~~additional
code~~~
' WScript.Sleep 2000 ' ~~~additional code~~~
Loop ' ~~~additional code~~~
' Read each file on the list and write it's contents to target file
Do Until FileList.AtEndOfStream
FileToRead = FileList.ReadLine
FileToRead = path & FileToRead
Set f = objFSO.OpenTextFile(FileToRead, 1)
TextReadAll = f.ReadAll
TargetFile.Write TextReadAll
TargetFile.WriteLine vbCrlf
Loop
FileList.Close
TargetFile.Close
' On Error GoTo 0
If objFSO.FileExists ("FileList.txt") then
objFSO.DeleteFile ("FileList.txt")
End If
Set objFSO = nothing
Set TargetFile = nothing
Set oShell = nothing
Set FileList = nothing
Set f = nothing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For the record this script was working for me before I inserted your
additional code. I had to use a workaround though; I had to insert a
WScript.Sleep 2000 after the oShell.run command.
Now what I've discovered is that when I run the script exactly as it
appears above, it doesn't work; I'm left with a FileList.txt that
should have been deleted, and a Merged_Files.txt that's empty.
If I enable "On Error GoTo 0" and run it again, I get the same results,
and a popup tells me what the error was:
Line: 34
Character: 4
Error: Permission Denied
This error refers to the line that attempts to delete FileList.txt:
objFSO.DeleteFile ("FileList.txt")
So it appears that error handling WAS still in effect - throughout the
entire script and not just within the Do While Loop - until I turned
off error handling and subsequently got the popup.
There is a workaround that will make the above script work - if I
enable the WScript.Sleep 2000 in the Do While Loop. But that defeats
the purpose of what I'm trying to accomplish.
I have a feeling my structuring of your additional code, or the entire
script in general, is not exactly correct. Any suggestions? Thanks.
Steven Burn wrote:
> Thats correct ;o)
>
> --
> Regards
>
> Steven Burn
> Ur I.T. Mate Group
> www.it-mate.co.uk
>
> Keeping it FREE!
>
> "Highlander" <tron9901@msn.com> wrote in message
> news:1140308295.973427.20790@g44g2000cwa.googlegroups.com...
> > I should have been mentioned that this is only one block of code in a
> > larger script. So what I meant was that I wanted to turn off error
> > handling for the rest of the script.
> >
> > The more I think about it though, I would guess that since the
> > statement 'On Error Resume Next' is within the Do While Loop, then
> > error handling is only in effect within the Loop. Correct?
> >
> > Steven Burn wrote:
> > > If you turn off error handling, the code isn't going to work (it's just
> > > going to bork when an error occurs).
> > >
> > > As for the sleep, that would be placed just before "Loop"
> > >
> > > --
> > > Regards
> > >
> > > Steven Burn
> > > Ur I.T. Mate Group
> > > www.it-mate.co.uk
> > >
> > > Keeping it FREE!
> > >
> > > "Highlander" <tron9901@msn.com> wrote in message
> > > news:1140304010.325722.122840@g47g2000cwa.googlegroups.com...
> > > > That works great, thanks!
> > > >
> > > > Couple questions if you will. I noticed that while running the code,
> > > > CPU usage hovered around 50%, until I unlocked the file and allowed
> the
> > > > code to complete. I'd like to place a 'Wscript.Sleep 20000' in the
> code
> > > > while it's looping to reduce the CPU usage. At what point in the code
> > > > would I insert that?
> > > >
> > > > Also, since error handling was turned on, I'd like to turn it off with
> > > > 'On Error GoTo 0'. At what point in the code would I insert that,
> > > > before or after 'Loop'?
> > > >
> > > > Thanks again!
> > > >
> > > > Steven Burn wrote:
> > > > > "On Error Resume Next" is used twice in the below, both upon opening
> the
> > > > > file, and upon writing to it (basically to catch the error at either
> > > point
> > > > > in the process). It then uses the Err result to determine if the
> write
> > > > > process was successful.
> > > > >
> > > > > Do While blnWrite <> True
> > > > > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > > > > On Error Resume Next
> > > > > Set f = objFSO.OpenTextFile ("c:\test.txt",8,True)
> > > > > On Error Resume Next
> > > > > f.writeline Now & vbCrlf
> > > > > f.writeline "File was not locked, and was written to." & vbCrlf
> > > > > If Err.Number = 0 Then blnWrite = True
> > > > > f.close
> > > > > Set objFSO = nothing
> > > > > Set f = nothing
> > > > > Loop
> > > > >
> > > > > --
> > > > > Regards
> > > > >
> > > > > Steven Burn
> > > > > Ur I.T. Mate Group
> > > > > www.it-mate.co.uk
> > > > >
> > > > > Keeping it FREE!
> > > > >
> > > > > "Highlander" <tron9901@msn.com> wrote in message
> > > > > news:1140301694.703124.150430@g43g2000cwa.googlegroups.com...
> > > > > > Steven,
> > > > > >
> > > > > > Thanks for the reply but I can't quite grasp your example. I've
> tried
> > > > > > plugging it into my code but can't get it to work.
> > > > > >
> > > > > > Here's my code:
> > > > > >
> > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > > > On Error Resume Next
> > > > > > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > > > > > Set f = objFSO.OpenTextFile ("c:\test.txt",8,True)
> > > > > > f.writeline Now & vbCrlf
> > > > > > f.writeline "File was not locked, and was written to." & vbCrlf
> > > > > > f.close
> > > > > > Set objFSO = nothing
> > > > > > Set f = nothing
> > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > > >
> > > > > > I've got the file c:\test.txt locked by another process, so when I
> try
> > > > > > to run the above code it does NOT write to c:\test.txt.
> > > > > > How can I modify the above code so that once I unlock c:\test.txt
> it
> > > > > > will get written to?
> > > > > >
> > > > > > Thanks.
> > > > > >
> > > > > > Steven Burn wrote:
> > > > > > > Do While [Condition] <> [Value_or_boolean]
> > > > > > > '// rest of code here ...
> > > > > > > If [Whatever] Then [Condition] = [Value_or_boolean]
> > > > > > > '// ... or here
> > > > > > > Loop
> > > > > > >
> > > > > > > --
> > > > > > > Regards
> > > > > > >
> > > > > > > Steven Burn
> > > > > > > Ur I.T. Mate Group
> > > > > > > www.it-mate.co.uk
> > > > > > >
> > > > > > > Keeping it FREE!
> > > > > > >
> > > > > > > "Highlander" <tron9901@msn.com> wrote in message
> > > > > > > news:1140294280.052220.136000@g44g2000cwa.googlegroups.com...
> > > > > > > > Hello.
> > > > > > > >
> > > > > > > > In a batch file I can repeatedly check for a conditon before
> > > > > > > > continuing:
> > > > > > > >
> > > > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > > > > > :Repeat
> > > > > > > > IF EXIST c:\temp\finished.log GOTO Continue
> > > > > > > > ECHO "Not finished yet..."
> > > > > > > > Sleep 60
> > > > > > > > GOTO Repeat
> > > > > > > > :Continue
> > > > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > > > > >
> > > > > > > > Can anyone provide a simple, basic code snippet that will
> allow me
> > > to
> > > > > > > > do the equivalent in VBScript? More specifically, I'm trying
> to
> > > check
> > > > > > > > if a file has closed before continuing - and I need this
> checking
> > > to
> > > > > > > > repeat itself in continuous loop.
> > > > > > > >
> > > > > > > > Thanks!
> > > > > > > >
> > > > > >
> > > >
> >