Sunday, May 24, 2009

Built-in DOS Command-Line FTP client and DOS scripts

Did you know that there are two ways that you can use the FTP client (ftp.exe in the c:\windows\system32 directory) that comes packaged with Windows? (Yes, Vista included) This dates back to XP, Win98, and even Win95. From the DOS prompt you can type ftp to get the ftp> prompt, and from that environment you can sequentially type in the ftp commands, OR you can write a batch file to automate the FTP of multiple files using the single DOS command:

c:\>ftp -s:script.txt ftp.your_url.com

The batch file can WRITE the ftp> commands to a text file script.txt, invoke the commands in the file and erase the file to clean up, and can be run from the c:\> prompt by just calling the batch file name (without the trailing .bat). A couple of different syntaxes below gets the job done:

echo userid> script.txt
echo password>> script.txt
echo binary>> script.txt
echo put filetoupload.pdf>> script.txt
echo close>> script.txt
echo bye>> script.txt
ftp -s:script.txt ftp.insertyoururl.com
del script.txt

or

set /p FTPADDRESS=Enter URL or IP Address:
set /p SITEBACKUPFILE=Enter File to Download:
set /p FTPUSERNAME=Enter FTP User Name:
set /p FTPPASSWORD=Enter FTP Password:
>script.ftp ECHO %FTPUSERNAME%
>>script.ftp ECHO %FTPPASSWORD%
>>script.ftp ECHO binary
>>script.ftp ECHO get %SITEBACKUPFILE%
>>script.ftp ECHO close
>>script.ftp ECHO bye
FTP -s:script.ftp %FTPADDRESS%
TYPE NUL >script.ftp
DEL script.ftp

And now for the BUG that took me pretty much an entire day to solve. It is so stupid that no one else has ever done this, therefore I could not find it on the internet. I found that both typing ftp at the dos prompt or running the batch files from the dos prompt caused dos to "hang". As in, the cursor blinks on a new line but nothing happens and no keys allow escape. The final answer was I had inadvertently named my batch file ftp.bat -- HUGE mistake because that name overrides the path to ftp.exe in the windows system directory and essentially causes the cmd utility to crash. Oh boy.

No comments:

Post a Comment