Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm trying to write a small VBscrip

Status
Not open for further replies.

m2t9664

Technical User
Dec 7, 2003
5
CA
I'm trying to write a small VBscript program that read the word "226 Transfer Complete." on the FTPlog.txt file. if the program can't find the word "226 Transfer Complete." then it will rerun the batch file again until it find the "226 Transfer complete." line.

Dim FSO
Dim Found226
Dim objFile
Dim objRun
Dim strLine

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFile = FSO.OpenTextFile ("H:\Test\FTPWEBLOG.TXT")

Set Found266 = strLine

Do While objFile.AtEndOfStream <> true
strLine = objFile.ReadAll
strLine = &quot;226 Transfer complete.&quot;
'Found226 = strLine
If Found226 = True then
objFile.close
End if

Loop
If Found226 = False Then
Set objRun = CreateObject(&quot;WScript.shell&quot;)
objRun.Run &quot;H:\Test\gWdataXP.bat&quot;,1,true
Set ObjRun = Nothing
End if

objFile.close

Thankyou very much


 
Here is how I could write this:
-----------------------------------
Set SH = CreateObject(&quot;WScript.Shell&quot;)
Set FS = CreateObject(&quot;Scripting.FileSystemObject&quot;)

BatchFile = &quot;h:\test\gWdataXP.bat&quot;
OutputFile = &quot;H:\Test\FTPWEBLOG.TXT&quot;
FindString = &quot;226 Transfer Complete&quot;

do while not CheckFile
Runs = Runs + 1
wscript.echo &quot;Current run: &quot; & Runs
RunBatch
loop

Set SH = Nothing
Set FS = Nothing


Function CheckFile()
Set InFile = FS.OpenTextFile(OutputFile)
if instr(lcase(InFile.ReadAll), lcase(FindString)) > 0 then
CheckFile = True
else
CheckFile = False
end if
InFile.Close: Set InFile = Nothing
End Function

Function RunBatch
SH.Run &quot;%comspec% /c &quot; & BatchFile, 0, true
End Function

------------------------------------------------
That would effectively loop the batch till it finds the string. It's also configurable so you can use it for other tasks that may require similar functionality. You just set BatchFile var to the batch to run, OutputText var to the output file to check and FindString to whatever you want it to look for. I would also do the FTP a little different too, I would write it so it would generate an FTP text script then launch FTP within the script itself rather than rely on a batch file. Here is an example of that:

--------------------------------------------------------
Set SH = CreateObject(&quot;WScript.Shell&quot;)
Set FS = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set ENV= SH.Environment(&quot;PROCESS&quot;)

FindString = &quot;226 Transfer&quot;
FTP_HostName = &quot;ftp.site.com&quot;
FTP_UserName = &quot;anonymous&quot;
FTP_Password = &quot;me@me.com&quot;
FTP_Path = &quot;/pub/misc&quot;
FTP_StoreAt = &quot;c:\temp&quot;
FTP_Filename = &quot;filename.zip&quot;

WriteFTPScript
RunBatch

do while not CheckFile
Runs = Runs + 1
wscript.echo &quot;Current run: &quot; & Runs
RunBatch
loop

Set SH = Nothing
Set FS = Nothing


Function CheckFile()
Set InFile = FS.OpenTextFile(ENV(&quot;temp&quot;) & &quot;\tmpftpout.dat&quot;)
if instr(lcase(InFile.ReadAll), lcase(FindString)) > 0 then
CheckFile = True
else
CheckFile = False
end if
InFile.Close: Set InFile = Nothing
End Function

Function RunBatch
SH.Run &quot;%comspec% /c %systemroot%\system32\ftp -s:&quot; & ENV(&quot;temp&quot;) & &quot;\tmpftp.txt >&quot; & ENV(&quot;temp&quot;) & &quot;\tmpftpout.dat&quot;, 0, true
End Function

Function WriteFTPScript
Set OutFile = FS.CreateTextFile(ENV(&quot;temp&quot;) & &quot;\tmpftp.txt&quot;)
OutFile.Writeline &quot;open &quot; & FTP_HostName
OutFile.Writeline FTP_UserName
OutFile.Writeline FTP_Password
OutFile.Writeline &quot;bin&quot;
OutFile.Writeline &quot;cd &quot; & FTP_Path
OutFile.Writeline &quot;lcd &quot; & FTP_StoreAt
OutFile.Writeline &quot;get &quot; & FTP_Filename
OutFile.Writeline &quot;close&quot;
OutFile.Writeline &quot;quit&quot;
OutFile.Close: Set OutFile = Nothing
End Function
 
thanks for your help, I will give that a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top