FTP Upload and FTP Download with VBScript

While Googling around the other day I noticed that lots of people are searching for a way to FTP files with VBScript. After looking for a while at the solutions to do this, it was clear that no real easy, free way of FTP uploading and downloading files was currently available. There are downloadable components that would present a programmable API. But these are costly, and you’d have to install them. So seeing the need I decided to whip up a couple of functions that would preform some basic uploading and downloading.

Pretty straight forward, you have to supply the credentials to connect to the machine, the IP address or DNS name for the machine and then the source and destination locations. Example of syntax:

When using the download function, if you don’t specify a location it will default to the working directory of the script. If for some reason there is a problem transferring the file the functions will return the error message. If they are successful, they will return “true”.

Update: I just made some corrections to this per the suggestions of some emails and comments I received. I retested this script, since it’s been quite some time since I’ve had the chance to use it, and it’s working like a charm. Please continue to report any issues you might have and thank you for your feedback.

 

14 thoughts on “FTP Upload and FTP Download with VBScript

  1. Thank you very much for taking the time to “whip” this up. I have been trying to use it in an application but have hit a problem and research would seem to show that there is no solution using the Windows built-in http://ftp.exe.

    The issue is that I need to use passive mode and, even though you could add ‘quote pasv’ to the command script, http://ftp.exe does not support passive mode.

    Thank you very much for the trouble but this is not a workable solution for using passive mode.

    Regards

    • I have used WinSCP in the past as a replacement for the limited functionality of the built in ftp client. You might see if that will work for your purposes.

  2. Don’t know what happened there. There shouldn’t be http:// in front of the “ftp.exe” as I was referring to the command.

  3. Thank you so much, saved me heaps of time and worked straight away.

    Usage for calling UploadFTP is:
    Wscript.Echo FTPDownload(“192.168.1.1”, “domain\user”, “password”, “C:\file.txt”, “\remote_dir\”)

  4. Oops my last comment was a bit premature… There’s an issue in the script when input file contains spaces e.g. “C:\Some Folder\file.txt”.

    The following statement returns false-
    oFTPScriptFSO.FileExists(sLocalFile)
    And therefore the function exits with value “Error: File Not Found.”

    The reason seems that it doesn’t like the string to have quotation marks in it. The quotation marks were added previously by the section commented –
    ‘Here we willcheck the path, if it contains
    ‘spaces then we need to add quotes to ensure
    ‘it parses correctly.

    So I’ve moved this section to after the path checks and everything seems to work fine.

    The path checks look like this now:
    ‘———-Path Checks———
    ‘Check to ensure that a remote path was
    ‘passed. If it’s blank then pass a “\”

    ‘Check the local path and file to ensure
    ‘that either the a file that exists was
    ‘passed or a wildcard was passed.

    ‘Here we willcheck the path, if it contains
    ‘spaces then we need to add quotes to ensure
    ‘it parses correctly.

    ‘——–END Path Checks———

    • I saw your comment, but I found it too complicated (I am not very good at programming). What I did is make a copy of the file and save it in the C: drive, so there is no possible way there could be a space. However, the macro is not permitted to put files inside the C: drive, so I first made a folder. To not get errors if you have to upload the file many times, we have to check if the directory exists, or doesn’t (this is the if statement). If it doesn’t exist, it makes a folder; if it does, it just goes on to create a SaveCopyAs.

      ———————-

      Public Sub DuplicateFile()

      If Len(Dir(“C:\Folder\YourFile”, vbDirectory)) = 0 Then
      MkDir “C:\Folder”
      End If

      ActiveWorkbook.SaveCopyAs “C:\Folder\TempNameOfFile” (or it can be your normal file’s name)

      End Sub

      ———————-

      Now, this function is called before the script for the upload/download (Wscript.Echo FTPDownload(“192.168.1.1”, “domain\user”, “password”, “C:\file.txt”, “\remote_dir\”)

      And of course, the “C:\file.txt” should be the “C:\Folder\TempNameOfFile”.

      What do you think about my solution?

  5. okay this script is the first uploaded script worked fine for me the problem its just upload the file name and show unknown error

    any idea about how to fix this

    Thnx

  6. If you want to use these scripts to access a server on the web behind a box, you must use the passive mode.
    Unlike a widely held idea http://ftp.exe works very well in passive mode. Just add in the elementary commands, the command ‘quote pasv’ (here : sFTPScript = sFTPScript & “quote pasv” & vbCRLF, behind : sFTPScript = sFTPScript & “binary” & vbCRLF)

    You must also remember to allow ‘C:\windows\System32\ftp.exe’ into your firewall.

    It is also necessary to modify the error detection at the end of the functions, because the numbers and the comments of the answers can vary according to the server, especially if, like me, you use a language other than English.

    Finally, and this is a personal opinion, I find that the Hungarian notation over-use in the scripts is harmful to the reading of the code, why not use the Csharp or Java – Best Practices – coding guidelines.

    I am french, excuse me if my English is sometimes incorrect.

Leave a Comment