Solved

Need to loop on error until cleared using ChilkatFTP

Posted on 2008-10-01
5
617 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now