• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 640
  • Last Modified:

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

0
Mike Caldwell
Asked:
Mike Caldwell
  • 3
  • 2
1 Solution
 
RobSampsonCommented:
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

0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
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!
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
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.
0
 
RobSampsonCommented:
>> it instantly deleted all of them at the server

That seems odd, seeing as the functionality should not have changed....you could comment out this line:

        success = objChilkatFTP.DeleteRemoteFile(fname)

so that it doesn't delete them, but should still download them, and see what happens....

But of course, purchasing the fully supported version may be better in the long run.

Regards,

Rob.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Already bought a license and will slepp better this way!  Thanks Ron.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now