FTP multiple files using Loop

CHS

New member
Local time
Today, 12:51
Joined
May 16, 2014
Messages
4
Hi everyone,
I am wondering if someone can help me with a challenge that I am facing right now I have been trying to come up with a solution for the past 3 days with no success. Basically what I am trying to accomplish is uploading multiples file automatically into server. The code I am trying to use is the one from Dev Ashish that can be found in his website "Sorry, this is my first post and I couldn't post the link here". The code works great if the user wants to upload a single file manually because the code prompts you to choose the file and I am wondering if there is a way to tweak the code.
here is the code I am using

Code:
Private Sub Form_Load()
Dim objFTP As FTP
Dim strfile As String
strfile = Dir("C:\Documents and Settings\%username%\My Documents\TEST1" & "\" & "*.txt")

Const conTARGET = "ftp://servername.com"
      
Do While Len(strfile) > 0
  Set objFTP = New FTP
  With objFTP
    .FtpURL = conTARGET
    .SourceFile = strfile
    .DestinationFile = "/TEST/" & strfile
    .AutoCreateRemoteDir = True
    If Not .IsConnected Then .DialDefaultNumber
    .ConnectToFTPHost "username", "password"
    .UploadFileToFTPServer
    strfile = Dir
  End With
Loop

End Sub


the code that needs to be tweaked is the one below
Code:
Public Property Let SourceFile(strFilename As String)
  'Pass vbNullString for Common Dialog
    If strFilename = vbNullString Then
    If lnghWnd = 0 Then lnghWnd = apiGetActiveWindow()
    mstrSrcFile = fGetNewFileName("Please select the" _
                  & " file to upload.", lnghWnd, True)
  Else
  mstrSrcFile = strFilename
  End If
  mblnUpload = True
  mlngSize = FileLen(mstrSrcFile)
End Property

whenever I try to run the code it will display error 53 File not found

.SourceFile = strfile

thanks
 
Last edited:
Your code assumed that the wildcard "*" is acceptable. It isn't. So loop over the files yourself - search "loop files access" - and call your ftp-thingy for each.
 
Your code assumed that the wildcard "*" is acceptable. It isn't. So loop over the files yourself - search "loop files access" - and call your ftp-thingy for each.

thanks spikepl for pointing that out, I will give it a try and let you know.
thanks again
 
Last edited:
Got it to work finally

Code:
Private Sub Form_Load()
Dim objFTP As FTP
Dim fs, f, f1, fc, s
folderspec = "C:\Documents and Settings\%username%\My Documents\TEST1\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)

Const conTARGET = "ftp://servername.com"
Set fc = f.Files
For Each f1 In fc
s = s & f1.Name
  Set objFTP = New FTP
  With objFTP
    .FtpURL = conTARGET
    .SourceFile = "C:\Documents and Settings\%username%\My Documents\TEST1\" & s
    .DestinationFile = "/TEST/" & s
    .AutoCreateRemoteDir = True
    If Not .IsConnected Then .DialDefaultNumber
    .ConnectToFTPHost "username", "password"
    .UploadFileToFTPServer
  End With
Next
End Sub

thanks for your help
 
Last edited:

Users who are viewing this thread

Back
Top Bottom