Link to home
Start Free TrialLog in
Avatar of Mike Caldwell
Mike CaldwellFlag for United States of America

asked on

Need to loop on error until cleared using ChilkatFTP

Using the free version of ChilkatFTP to download PDFs from server to local folder.  Script genrally works fine, but after 2 or 3 files I get a run time error "Failed to get directory informatoin", failing at the line in the loop where it gets the name of the next file.  Chilkat states that the free version had some bugs, and rather than fix they now offer FTP2 for $150.  I'm using it for upload with no problems, but the download has this error.  If I use the FTP2 Active X controler instead of FTP, it works fine.  So looking for a method to trap this error, and loop back around to try again to get the file name until successful.

'******  	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

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi Mike, there's a possibility that the script is having problems getting the next file after deleting a file within the same loop.
Say "n" was 6, when you delete a file, "n" becomes 5 (for the total number of files available), so you'll probably get the error.

What I've done to try to get around that is to record each file name that was downloaded during the loop, then after the files have been downloaded, it goes back and deletes those files.

Regards,

Rob.
'******  	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
 strFileNames = ""
    For i = 0 To n - 1
	
        fname = objChilkatFTP.GetFilename(i) '>> this is the line causing the error
        If strFileNames = "" Then
        	strFileNames = fname
        Else
        	strFileNames = strFileNames & VbCrLf & fname
        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
 		
    Next
    
    arrFileNames = Split(strFileNames, VbCrLf)
    For i = 0 To UBound(arrFileNames)
    	 '  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

Avatar of Mike Caldwell

ASKER

Hmmmm, Rob.  Put 25 files at the server for a test.  Then ran this code as-is, and it instantly deleted all of them at the server and did not download any of them!
Rob, stability is very important.  I will have as many as eight PCs running these scripts, all but two unattended.  My business depends upon this, so think I'l just purchase a license to the FTP2 version and be done with it.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Already bought a license and will slepp better this way!  Thanks Ron.