FTP Multiple Files with ASP and WScript.Shell
This is a simple example of how to send multiple files via FTP from an ASP page using a shell object and a commands file for the FTP executable. There are variety of shareware and even freeware components out there that can do this but doing it yourself is not nearly as hard as it might first sound.
There are only two pieces to this, the ASP page itself and a command file to hold the commands for the FTP executable.
ASP Code
Your ASP code will look like this:
Set oShell = server.CreateObject("WScript.Shell")
cmdLine = "ftp.exe -v -i -s:D:\InetPub\wwwroot\siteName\includes\ftpCommands.txt"
tempRet = oShell.Run ("cmd.exe /c " & cmdLine, 0, false)
set oShell = nothing
' wait 2 seconds for each email created
waitTime = numberOfFiles * 2
startTime = Timer
do while timer < startTime + waitTime
loop
What we're doing here is creating a shell object so we can access the command line and from there the windows FTP executable.
The next line creates the command line instructions to be executed. The switches given to the FTP.exe are:
- -v Tells it that you don't want to see the server responses.
- -i Disables the confirmation prompts you would normally get when sending multiple files.
- -s This tells the executable where to look for the command file. (There are no spaces in this switch).
In the next line we are using the shell object to access the command line (cmd.exe) and telling it to execute the commands we built in the line above. The /c option tells it to run the command that follows and then stop. The three arguments to the run command are:
- The command to run. In this case cmd.exe.
- The window style. Since this is an ASP app we set it to 0 to hide the window since no-one will be there to see it anyway.
- The last argument is a true/false switch telling the shell object to wait until the command finishes before continuing (true) or to continue right away (false).
The final line just removes the shell object. The following 4 lines of code set up a timer that will delay for 2 seconds for every file being sent. This way we avoid any access violations if we need to do anything with the files later, like move them to an archive. If you don't, you can omit that step.
Commands File
The commands file is simply a list of commands that the FTP executable will read and run in order.
open ftp.domainName.com userName password cd destinationDirectory mput D:\InetPub\wwwroot\siteName\ftpFiles\*.* close bye
In order, what is happening here is:
- Use the open command to open the connection to the FTP site.
- Send the user name to access the site.
- Send the password for the user name just sent. If the site allows annonymous access these two lines can be removed, make sure to close up any blank lines.
- Use the CD command to change directories if needed. If this isn't neccesary remove this line but close up any blank lines.
- Use the mput command to send all of the files in the listed directory. This line can be changed to be more specific, for example to send only files of a certain type you could do this mput D:\InetPub\wwwroot\siteName\ftpFiles\*.pdf. If you're only sending one file use the put command instead.
- Close the connection.
- Exit the FTP executable.
This example has all of the commands hard coded in the commands file, if you need to create it on the fly you can use the Scripting.FileSystemObject to create your commands file with whatever instructions you need. Just make sure you name it and save it to the directory you're referencing in the line:
cmdLine = "ftp.exe -v -i -s:D:\InetPub\wwwroot\siteName\includes\ftpCommands.txt"
I hope this helps someone out, if you have a question, comment, addition,
correction, or better way of doing it let us know.
We'll add it to the demo with the proper credit. comments@directedinsight.com
