Solved

Need to loop on error until cleared using ChilkatFTP

Posted on 2008-10-01
5
622 Views
Last Modified: 2013-12-09
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
Comment
Question by:Mike Caldwell
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22619597
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
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22638834
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
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 22641701
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22642647
>> 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
 
LVL 1

Author Closing Comment

by:Mike Caldwell
ID: 31502037
Already bought a license and will slepp better this way!  Thanks Ron.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
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, f…
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…

825 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