I want a batch file to ftp to a server, read out a text file, and disconnect. The server requires a user and password. I tried
@echo off
pause
@ftp example.com
username
password
pause
but it never logged on. How can I get this to work?
I want a batch file to ftp to a server, read out a text file, and disconnect. The server requires a user and password. I tried
@echo off
pause
@ftp example.com
username
password
pause
but it never logged on. How can I get this to work?
Using the Windows FTP client you would want to use the
-s:filename
option to specify a script for the FTP client to run. The documentation specifically points out that you should not try to pipe input into the FTP client with a<
character.Execution of the script will start immediately, so it does work for username/password.
However, the security of this setup is questionable since you now have a username and password for the FTP server visible to anyone who decides to look at your batch file.
Either way, you can generate the script file on the fly from the batch file and then pass it to the FTP client like so:
Replace servername, username, and password with your details and the batch file will generate the script as temp.txt launch ftp with the script and then delete the script.
If you are always getting the same file you can replace the
%1
with the file name. If not you just launch the batchfile and provide the name of the file to get as an argument.Use
as decribed in Windows XP Professional Product Documentation.
The file name that you have to specify in place of FileName must contain FTP commands that you want to send to the server. Among theses commands are
More commands can be found under Ftp subcommands.
Each line of a batch file will get executed; but only after the previous line has completed. In your case, as soon as it hits the ftp line the ftp program will start and take over user input. When it is closed then the remaining lines will execute. Meaning the username/password are never sent to the FTP program and instead will be fed to the command prompt itself once the ftp program is closed.
Instead you need to pass everything you need on the ftp command line. Something like: