Using Microsoft Visual Basic to Upload Files to an FTP Server

by xn1ch1 in Circuits > Microsoft

16520 Views, 4 Favorites, 0 Comments

Using Microsoft Visual Basic to Upload Files to an FTP Server

ScreenShot003.jpg

For Microsoft Office users, Visual Basic (VB) is a mighty power tool. Repetitive and longwinded operations have become a one button task, saving countless hours of our time, not to mention brain cells. With it, we can move and organise data, save files, process emails and more. But the scope of its functions is not just contained within the Office software, VB can interact with many parts of our computers.

For this Instructable, I’m going to show how we can use VB to upload files (not limited to office files) to an FTP file server with ease. We’re going to achieve this with a simple .bat file that will execute commands via the command line interpreter.

In the next few steps we will learn about the .bat file and the command line interpreter, so if you’re looking to get stuck in with the code, you may skip these steps.

How Does a .bat File Work

.bat is short for ‘batch’, a script file containing a series of commands that can be executed by the command line interpreter, stored as plain text. Below is the most simple of commands stored in a .BAT file.

ECHO Hello World!

The purpose of which, is to display ‘Hello World!’ on screen and nothing more. How does this help us? Well, at the moment, it doesn’t, so we’re going to look at how we could upload a file using command line.

If you would like more information on these files have a look here.

The Command Line Interpreter

Let’s start this the manual way to get a feel for how this part works. Open up your Command Prompt in Windows, command prompt works in a similar way to the Interpreter, so it helps to know what’s going on to hep understand the VB code later.

On the first line in Command Prompt type in the following line, replacing myserver.com with your ftp server.

ftp ftp.myserver.com

The two parts to this line are ‘ftp’, which tells the command you are connecting via ftp protocol, and the second part ‘ftp.myserver.com’ which you will put in the address of your file server.

You will then be prompted for your user name and password on separate lines. Type these in and press enter to see the following on a separate line

ftp>

This means we are connected and ready to upload or download.

To change the local directory type in the following line.

lcd “C:/Users/Me/My Documents/”

This will set the location where the files we wish to upload or download are stored on our computer. Also note, the quotes are only needed where this is a space in the directory name. You will receive a message line starting with OK, if the directory change was successful.

To change the remote directory type in the following line.

cd your/remote/destination/

Again, you will receive a message line starting with OK if the remote folder change was successful.

Now to send a file. Type to following line into command prompt and press enter, replacing myfile.png with your desired file.

put myfile.png

This line is telling the command prompt to send file the C:/Users/Me/My Documents/myfile.png to the your/remote/destination/ folder on the server, of course your details will be in there. You will then see a line stating the following

226 File successfully transferred

What does ‘put’ mean? It simply means put this file there. What if I want to send all the files in the folder? Then we use ‘mput’ with a wildcard as below…

mput *.png

Will send all .png files in the folder

mput *

Will send all files in the folder

mput a*

Will send all files starting with an ‘a’ in the folder

Once we have finished our uploads, we can type in ‘bye’ on a new line to close the connection, then 'exit' to close the command prompt.

Now we’ve covered those basics we can get stuck into the Visual Basic aspect in the next step.

Getting Visual Basic to Create the .bat File

Please note, this won’t be a full guide on how to use VB, I’m assuming you are here because you can use its basic functions already. If you need some help on VB check out this resource.

Head to the part of your VB script where you’re looking to upload files and let’s create some variables. The first will store our local directory and the rest will be variables to create our text file and .bat file.

Dim MyDirectory As String
Dim File1 As Integer
Dim File2 As Integer

File1 = FreeFile
File2 = FreeFile

Now we need to set a directory to store the temporary ftp command files, this can be any directory of your choosing. Try to make it an unused directory as the program will erase all .txt .bat and .out files each run.

MyDirectory = “C:\Temp\FTPFiles"

If we use this folder often we might want to check for any .out completion files and erase them from this directory, we can do this with the below code

If Dir(MyDirectory & ".out") <> "" Then Kill (MyDirectory & ".out")

Next we will create our first text file and enable us to write command lines to it under the name #File1

Open MyDirectory & ".txt" For Output As #File1

Now we begin writing out ftp commands to this file. These are very similar to the command prompt we did earlier, but please pay attention to the syntax used. First off, the connection to the ftp server. Each line we wish to write to file has to start with the word Print, then we list the file name then the text to write in quotes.

Print #File1, "open ftp.myserver.com"

Next will be our user name for this ftp directory, substituting ‘my_username’ for your actual user name.

Print #File1, “my_username”

Next the password, again substitute, ‘your_password’ with your actual password.

Print #File1, “my_password”

Next we will change your remote destination, again, if the root directory is fine, omit this part.

Print #File1, “cd my/remote/directory/”

Now to send some files, you can use the wildcards details in step 2 to send more than one file or the mput function also detailed in step 2.

Print #File1, “put C:/MyFiles/thisfile1.png” 
Print #File1, “put C:/MyFiles/thisfile2.png”

Once we’ve added a line for all of the files we wish to upload in our text file we will add a ‘bye’ command and close the file with the following.

Print #File1, "bye"
Close #File1

Now to create the .bat file to pass our written instructions too it.

Open MyDirectory & ".bat" For Output As #File2
Print #File2, "ftp -s:" & MyDirectory & ".txt"

And we will add a Completed message to the bottom of the .bat file to let us know when our upload in finished and close the file

Print #File2, "Echo ""Complete"" > " & MyDirectory & ".out"
Close #File2

Executing and Cleaning Up

Now we have written all of our ftp instructions and saved our command line files, we will invoke this .bat file and run it with the below code.

Here you can replace the vbNormalFocus with vbHide if you wish to hide the upload window, but it is advised to keep it in view to see the status of the transfer

Shell (MyDirectory & ".bat"), vbNormalFocus

Now we will tell VB to wait for our uploads to complete. We do this by telling VB to look for an .out file in the directory, which will only be created once the .bat file completes.

Do While Dir(MyDirectory & ".out") = ""
DoEvents
Loop

Then we will add a delay to VB to give us enough time to see the completed message in the command interface.

Application.Wait (Now + TimeValue("0:00:03"))

Once all the files have been uploaded, and VB can continue, we will delete all of the files created for the upload.

If Dir(MyDirectory & ".bat") <> "" Then Kill (MyDirectory & ".bat")
If Dir(MyDirectory & ".out") <> "" Then Kill (MyDirectory & ".out")
If Dir(MyDirectory & ".txt") <> "" Then Kill (MyDirectory & ".txt")

And that’s it we are all done. You may continue your VB process. Of course during this code you can apply many VB tricks to generate your commands so feel free to get creative. If you want to see the whole code, check out the next step.

Script Without the Commentary

‘Your VB before the FTP upload

Dim MyDirectory As String

Dim File1 AsInteger
Dim File2 As Integer
File1 = FreeFile
File2 = FreeFile

‘Change this to a directory of your choosing

MyDirectory = “C:\Temp\FTPFiles"

‘Kill existing .out files
If Dir(MyDirectory & ".out") <> "" Then Kill (MyDirectory & ".out")

Open MyDirectory & ".txt" For Output As #File1

‘Put your ftp server address here
Print #File1, "open ftp.myserver.com"

‘Put your user name here
Print #File1, “my_username”

‘Put your password here
Print #File1, “my_password”

‘Change your remote directory. Omit this line if root is fine.
Print #File1 “cd my/remote/directory/”

‘Add your list of files here using put or mput with wildcard if needed
Print #File1, “put C:/MyFiles/thisfile1.png”
Print #File1, “put C:/MyFiles/thisfile2.png”

Print #File1, "bye"
Close #File1

Open MyDirectory & ".bat" For Output As #File2
Print #File2, "ftp -s:" & MyDirectory & ".txt"
Print #File2, "Echo ""Complete"" > " & MyDirectory & ".out"

Close #File2

‘Execute the bat file
Shell (MyDirectory & ".bat"), vbNormalFocus

Do While Dir(MyDirectory & ".out") = ""

DoEvents

Loop

Application.Wait (Now + TimeValue("0:00:03"))

‘Delete the batch files
If Dir(MyDirectory & ".bat") <> "" Then Kill (MyDirectory & ".bat")
If Dir(MyDirectory & ".out") <> "" Then Kill (MyDirectory & ".out")
If Dir(MyDirectory & ".txt") <> "" Then Kill (MyDirectory & ".txt")

‘Your VB after the upload