Solved

Need to restart a VBScript upon error

Posted on 2008-10-03
16
1,428 Views
Last Modified: 2013-12-09
I have a VBScript doing FTP using the ChilKatFTP2  Active X control.  The code runs fine.  We have an operation that at random times puts a significant number of files at the server.  I have this script in an endless loop.  When files are found it downloads them, then disconnects for one minute, then restarts the loop.  If there is noting at the server, then it just logs in, sees there is nothing, then disconnects and waits the minute again.  This same script is expected to run for months on several PCs that are watching (different) download folders.  However after a day or so (not consistent) it returns an error message that the connection failed, and the WSCRIPT stops.  I need a way to bullet proof this so that when an error occurs before everything stops it just waits a while, then starts all over.  I need to run this same script on several machnes that are only checked every few days.
'******  	MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"		'   Where files go

 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")

 

' Do while 1=1

 

objChilkatFTP.Hostname = "ftp.servername.com"		' Source server

objChilkatFTP.Username = "userID"

objChilkatFTP.Password = "password"

 

objChilkatFTP.Passive = 1 ' Passive mode

 

'  Connect and login to the FTP server.

intSuccess = objChilkatFTP.Connect()

If (intSuccess <> 1) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If

 

objChilkatFTP.ListPattern = "*.pdf"		' Set file type as PDF

 

' Now go get files from server, then delete

 

n = objChilkatFTP.NumFilesAndDirs

If (n < 0) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If

 

If (n > 0) Then

 

    For i = 0 To n - 1

	

        fname = objChilkatFTP.GetFilename(i) '>> this is the line causing the error

	lcl_fname =	strDestFolderName & fname	' Add path to file name

 

        '  Download the file into the current working directory.

        success = objChilkatFTP.GetFile(fname,lcl_fname)

         If (success <> 1) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

         End If

 

        '  Now delete the file.

        success = objChilkatFTP.DeleteRemoteFile(fname)

        If (success <> 1) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

        End If

    Next

End If							' Terminates when list exhausted

 

objChilkatFTP.Disconnect		' All done; disconnect for now

 

' wscript.sleep 60000			' Restart in one minute

 

' Loop

Open in new window

0
Comment
Question by:Mike Caldwell
  • 8
  • 7
16 Comments
 
LVL 5

Expert Comment

by:si_shamil
ID: 22638918
put "On Error Resume Next" on top of your script.

Above statement enables error handling future of VBSCRIPT. So the script will not stop if there is some error.This is dirty fix, but it will work.
0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22638960
I've changed your script to handle the error you get.
On Error Resume Next

'******         MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"                '   Where files go

 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")

 

Do While 1 = 1 

	objChilkatFTP.Hostname = "ftp.servername.com"           ' Source server

	objChilkatFTP.Username = "userID"

	objChilkatFTP.Password = "password" 

	objChilkatFTP.Passive = 1 ' Passive mode

 

	'  Connect and login to the FTP server.

	intSuccess = objChilkatFTP.Connect()

	If (intSuccess <> 1) Then

		MsgBox objChilkatFTP.LastErrorText

		WScript.Quit

	End If

 

	objChilkatFTP.ListPattern = "*.pdf"             ' Set file type as PDF

	 

	' Now go get files from server, then delete 

	n = objChilkatFTP.NumFilesAndDirs

	If (n < 0) Then

	    MsgBox objChilkatFTP.LastErrorText

	    WScript.Quit

	End If

	 

	If (n > 0) Then

	    For i = 0 To n - 1

	        fname = objChilkatFTP.GetFilename(i) 	' this is the line causing the error
 

			' This will exit from the "For" loop in any error occured.

			If Err.Number <> 0 Then

				Exit For

			End if

			

	        lcl_fname = strDestFolderName & fname	' Add path to file name

	 

	        '  Download the file into the current working directory.

	        success = objChilkatFTP.GetFile(fname,lcl_fname)

	         If (success <> 1) Then

	            MsgBox objChilkatFTP.LastErrorText

	            WScript.Quit

	         End If

	 

	        '  Now delete the file.

	        success = objChilkatFTP.DeleteRemoteFile(fname)

	        If (success <> 1) Then

	            MsgBox objChilkatFTP.LastErrorText

	            WScript.Quit

	        End If

	    Next

	End If	' Terminates when list exhausted

	 

	objChilkatFTP.Disconnect	' All done; disconnect for now

	 

	Wscript.sleep 60000			' Restart in one minute	 

Loop

Open in new window

0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22638981
Thanks Si; I've started it and we'll let it cook for a while and see what happens.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22639027
It appears that the script encountered an error, and did not exit the WSCRIPT.  However it also appears to not be running.  I planted a test file at the FTP server and the script should rerun every minute.  After ten minutes the file has still not come down.  Do I perhaps also need something that clears the error condition, otherwise it might be in a permanent ON ERROR tight loop.

0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22639067
If you used a script I've changed, it has a mistake, sorry
change folowing, and let me know if it works
On Error Resume Next

'******         MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"                '   Where files go

 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")

 

Do While 1 = 1 

        objChilkatFTP.Hostname = "ftp.servername.com"           ' Source server

        objChilkatFTP.Username = "userID"

        objChilkatFTP.Password = "password" 

        objChilkatFTP.Passive = 1 ' Passive mode

 

        '  Connect and login to the FTP server.

        intSuccess = objChilkatFTP.Connect()

        If (intSuccess <> 1) Then

                MsgBox objChilkatFTP.LastErrorText

                WScript.Quit

        End If

 

        objChilkatFTP.ListPattern = "*.pdf"             ' Set file type as PDF

         

        ' Now go get files from server, then delete 

        n = objChilkatFTP.NumFilesAndDirs

        If (n < 0) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

        End If

         

        If (n > 0) Then

            For i = 0 To n - 1

                fname = objChilkatFTP.GetFilename(i)    ' this is the line causing the error

 

                        ' This will exit from the "For" loop in any error occured.

                        If Err.Number <> 0 Then

                                Exit For

                                Err.Clear

                        End if

                        

                lcl_fname = strDestFolderName & fname   ' Add path to file name

         

                '  Download the file into the current working directory.

                success = objChilkatFTP.GetFile(fname,lcl_fname)

                 If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    WScript.Quit

                 End If

         

                '  Now delete the file.

                success = objChilkatFTP.DeleteRemoteFile(fname)

                If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    WScript.Quit

                End If

            Next

        End If  ' Terminates when list exhausted

         

        objChilkatFTP.Disconnect        ' All done; disconnect for now

         

        Wscript.sleep 60000                     ' Restart in one minute  

Loop

Open in new window

0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22639086
Sorry, mistake again
Use fixed one:
On Error Resume Next

'******         MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"                '   Where files go

 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")

 

Do While 1 = 1 

        objChilkatFTP.Hostname = "ftp.servername.com"           ' Source server

        objChilkatFTP.Username = "userID"

        objChilkatFTP.Password = "password" 

        objChilkatFTP.Passive = 1 ' Passive mode

 

        '  Connect and login to the FTP server.

        intSuccess = objChilkatFTP.Connect()

        If (intSuccess <> 1) Then

                MsgBox objChilkatFTP.LastErrorText

                WScript.Quit

        End If

 

        objChilkatFTP.ListPattern = "*.pdf"             ' Set file type as PDF

         

        ' Now go get files from server, then delete 

        n = objChilkatFTP.NumFilesAndDirs

        If (n < 0) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

        End If

         

        If (n > 0) Then

            For i = 0 To n - 1

                fname = objChilkatFTP.GetFilename(i)    ' this is the line causing the error

 

                        ' This will exit from the "For" loop in any error occured.

                        If Err.Number <> 0 Then

                                Err.Clear

                                Exit For

                        End if

                        

                lcl_fname = strDestFolderName & fname   ' Add path to file name

         

                '  Download the file into the current working directory.

                success = objChilkatFTP.GetFile(fname,lcl_fname)

                 If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    WScript.Quit

                 End If

         

                '  Now delete the file.

                success = objChilkatFTP.DeleteRemoteFile(fname)

                If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    WScript.Quit

                End If

            Next

        End If  ' Terminates when list exhausted

         

        objChilkatFTP.Disconnect        ' All done; disconnect for now

         

        Wscript.sleep 60000                     ' Restart in one minute  

Loop

Open in new window

0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22639957
If it exits the FOR loop, won't it then just quit?  That's what I'm trying to prevent; I need it to restart.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22639969
Actually, the code I posted was previously posted for a different issue and the line I indicated is the problem point is not.  The problem point is when it fails to connect.  Can I just move your inserted code up to that point?  BUt where it is might actually fix that problem too; can I have it in both places?

Then, to repeat the question above, will it cause a restart, or just exit the FOR loop, exit WSCRIPT, and stop?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:si_shamil
ID: 22640325
Can you post the exact Error you get when the script exists?
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22641189
No; it's in a dialog box that won't copy.  It is not a Vista message, but from Chilkat.  But it always ends with "Could not connect to server."  Take a look at what I have here.  I put the error checking statement right after the connect command, with an "Exit For", and have it just inside what is a useless For under normal circumstances.  But if it trips and exits that For, it goes to just after the Disconnect (if it gets a Disconnect but is not connected, that is an error too), but before the Loop.  Think this will work?  It's running now.

ON ERROR RESUME NEXT

'******  	MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"		'   Where files go
 

Set objFSO = CreateObject("Scripting.fileSystemObject")

' Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")	' Free Active X control
 

'******  In case free version won't work ***************

Set objChilkatFTP = CreateObject("Chilkat.FTP2")		' In case free versio won't work
 

' Any string unlocks the component for the 1st 30-days.

success = objChilkatFTP.UnlockComponent("Anything for 30-day trial")

If (success <> 1) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If

'**********************************************************************
 

Do while 1=1						' Constantly watch server folder
 

objChilkatFTP.Hostname = "ftp.server.com"		' 

objChilkatFTP.Username = "ID"

objChilkatFTP.Password = "PW"

 

objChilkatFTP.Passive = 1 ' Passive mode
 

For j = 0 to 1
 

'  Connect and login to the FTP server.

intSuccess = objChilkatFTP.Connect()
 

	If Err.Number <> 0 Then

		Err.Clear

		Exit For

	End if
 

If (intSuccess <> 1) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If
 

objChilkatFTP.ListPattern = "*.pdf"		' Set file type as PDF
 

' Now go get files from server, then delete
 

n = objChilkatFTP.NumFilesAndDirs

If (n < 0) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If
 

If (n > 0) Then
 

    For i = 0 To n - 1

	

        fname = objChilkatFTP.GetFilename(i)

	lcl_fname =	strDestFolderName & fname	' Add path to file name
 

        '  Download the file into the current working directory.

        success = objChilkatFTP.GetFile(fname,lcl_fname)

         If (success <> 1) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

         End If
 

        '  Now delete the file.

        success = objChilkatFTP.DeleteRemoteFile(fname)

        If (success <> 1) Then

            MsgBox objChilkatFTP.LastErrorText

            WScript.Quit

        End If

    Next

End If							' Terminates when list exhausted
 

objChilkatFTP.Disconnect		' All done; disconnect for now
 

Next
 

wscript.sleep 60000			' Restart in one minute
 

Loop
 

msgbox "PDF DN Bailed out of the FOR loop"

Open in new window

0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22641428
Just failed again.  

Attached is a PDF of a screen dump of the message.
error.pdf
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22641434
I checked the link mentioned in the error message, and it seems to be sort of an explanation, but not a work around.  The bottom line is I can prevent it or recover from it; I'm hoping to just recover from it.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 22642106
I suggest putting your for loop into a sub routine.  that way you can exit the sub when the error happens, but keep your code running with the ON Error Resume Next

Try the code attached.


ON ERROR RESUME NEXT

'******  	MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"		'   Where files go

 

Set objFSO = CreateObject("Scripting.fileSystemObject")

' Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")	' Free Active X control

 

'******  In case free version won't work ***************

Set objChilkatFTP = CreateObject("Chilkat.FTP2")		' In case free versio won't work

 

' Any string unlocks the component for the 1st 30-days.

success = objChilkatFTP.UnlockComponent("Anything for 30-day trial")

If (success <> 1) Then

    MsgBox objChilkatFTP.LastErrorText

    WScript.Quit

End If

'**********************************************************************

 

Do while 1=1						' Constantly watch server folder

 

	objChilkatFTP.Hostname = "ftp.server.com"		' 

	objChilkatFTP.Username = "ID"

	objChilkatFTP.Password = "PW"
 

	objChilkatFTP.Passive = 1 ' Passive mode
 

	RunLoop

	 

	wscript.sleep 60000			' Restart in one minute

 

Loop

 

msgbox "PDF DN Bailed out of the FOR loop"
 

wsript.quit
 

Sub RunLoop

	For j = 0 to 1

	 

		'  Connect and login to the FTP server.

		intSuccess = objChilkatFTP.Connect()

	 

		If Err.Number <> 0 Then

			Err.Clear

			Exit Sub

		End if

	 

		If (intSuccess <> 1) Then

		    MsgBox objChilkatFTP.LastErrorText

		    WScript.Quit

		End If

	 

		objChilkatFTP.ListPattern = "*.pdf"		' Set file type as PDF

		 

		' Now go get files from server, then delete

		 

		n = objChilkatFTP.NumFilesAndDirs

		If (n < 0) Then

		    MsgBox objChilkatFTP.LastErrorText

		    WScript.Quit

		End If

	 

		If (n > 0) Then

		 

		    For i = 0 To n - 1

			

			fname = objChilkatFTP.GetFilename(i)

			lcl_fname =	strDestFolderName & fname	' Add path to file name

		 

			'  Download the file into the current working directory.

			success = objChilkatFTP.GetFile(fname,lcl_fname)

			 If (success <> 1) Then

			    MsgBox objChilkatFTP.LastErrorText

			    WScript.Quit

			 End If

		 

			'  Now delete the file.

			success = objChilkatFTP.DeleteRemoteFile(fname)

			If (success <> 1) Then

			    MsgBox objChilkatFTP.LastErrorText

			    WScript.Quit

			End If

		    Next

		End If							' Terminates when list exhausted

	 

		objChilkatFTP.Disconnect		' All done; disconnect for now

	 

	Next

End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22642367
I've changed the script
Let me know if it works for you...
On Error Resume Next

' ****** MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"                '   Where files go
 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")
 

Do While 1 = 1 

    objChilkatFTP.Hostname = "ftp.servername.com"           ' Source server

    objChilkatFTP.Username = "userID"

    objChilkatFTP.Password = "password" 

    objChilkatFTP.Passive = 1 ' Passive mode
 

    '  Connect and login to the FTP server.

    intSuccess = objChilkatFTP.Connect()

    If (intSuccess <> 1) Then

        MsgBox objChilkatFTP.LastErrorText

    Else

        objChilkatFTP.ListPattern = "*.pdf"             ' Set file type as PDF
 

        ' Now go get files from server, then delete 

        n = objChilkatFTP.NumFilesAndDirs

        If (n < 0) Then MsgBox objChilkatFTP.LastErrorText
 

        If (n > 0) Then

            For i = 0 To n - 1

                fname = objChilkatFTP.GetFilename(i)    ' this is the line causing the error
 

                lcl_fname = strDestFolderName & fname   ' Add path to file name
 

                '  Download the file into the current working directory.

                success = objChilkatFTP.GetFile(fname,lcl_fname)

                If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    Exit For

                End If
 

                '  Now delete the file.

                success = objChilkatFTP.DeleteRemoteFile(fname)

                If (success <> 1) Then

                    MsgBox objChilkatFTP.LastErrorText

                    Exit For

                End If

            Next

        End If  ' Terminates when list exhausted
 

        objChilkatFTP.Disconnect    ' All done; disconnect for now

        Wscript.sleep 60000         ' Restart in one minute  

    End If

Loop

Open in new window

0
 
LVL 5

Accepted Solution

by:
si_shamil earned 500 total points
ID: 22642388
Improved the script
run it from command line using cscript, so the script will not wait for user input, it will only print the errors to the console

Let me know if it works for you...
On Error Resume Next

' ****** MOVE FILES FROM A FLAT FOLDER TO A SERVER

Const strDestFolderName = "D:\PDF FLAT\"                '   Where files go
 

Set objFSO = CreateObject("Scripting.fileSystemObject")

Set objChilkatFTP = CreateObject("ChilkatFTP.ChilkatFTP.1")
 

Do While 1 = 1 

    objChilkatFTP.Hostname = "ftp.servername.com"           ' Source server

    objChilkatFTP.Username = "userID"

    objChilkatFTP.Password = "password" 

    objChilkatFTP.Passive = 1 ' Passive mode
 

    '  Connect and login to the FTP server.

    intSuccess = objChilkatFTP.Connect()

    If (intSuccess <> 1) Then

        Wscript.Echo objChilkatFTP.LastErrorText

    Else

        objChilkatFTP.ListPattern = "*.pdf"             ' Set file type as PDF
 

        ' Now go get files from server, then delete 

        n = objChilkatFTP.NumFilesAndDirs

        If (n < 0) Then Wscript.Echo objChilkatFTP.LastErrorText
 

        If (n > 0) Then

            For i = 0 To n - 1

                fname = objChilkatFTP.GetFilename(i)    ' this is the line causing the error
 

                lcl_fname = strDestFolderName & fname   ' Add path to file name
 

                '  Download the file into the current working directory.

                success = objChilkatFTP.GetFile(fname,lcl_fname)

                If (success <> 1) Then

                    Wscript.Echo objChilkatFTP.LastErrorText

                    Exit For

                End If
 

                '  Now delete the file.

                success = objChilkatFTP.DeleteRemoteFile(fname)

                If (success <> 1) Then

                    Wscript.Echo objChilkatFTP.LastErrorText

                    Exit For

                End If

            Next

        End If  ' Terminates when list exhausted

        

        objChilkatFTP.Disconnect    ' All done; disconnect for now

    End If

    

    Wscript.sleep 60000         ' Restart in one minute  

Loop

Open in new window

0
 
LVL 1

Author Closing Comment

by:Mike Caldwell
ID: 31502956
All working fine now.  Thanks for the help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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…
As a financial services provider, your business is impacted by two of the strictest federal regulations on record: the Sarbanes-Oxley Act and the Gramm-Leach-Bliley Act. Correctly implementing faxing into your organization to provide secure, real-ti…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

10 Experts available now in Live!

Get 1:1 Help Now