I have created a batch file called FTP_automation.bat
which contains the code below.
SET MyPath=E\PGP\test_pg
ftp
open 142.245.30.165 21
TESTA9MS
test11
binary
put %MyPath%
quit
Pause
When I am running FTP_automation.bat
, it's not uploading the file which I am passing the parameter.
The script stops after ftp
which is mentioned in the batch file, and it's not connecting to the IP address and port number which I have passed in the batch file.
If I manually typed these commands it was working perfectly. What correction am I supposed to do to this code?
Best Answer
You have to put the
ftp
commands to a separate file. You cannot put lines you otherwise type on terminal to.bat
file and expect it to behave identically. The.bat
file can include only Windows commands. When you run theftp
command from the batch file, it waits for its commands. It does not know about the.bat
file, so it cannot read the commands from there.So put your commands to a separate text file (e.g.
ftp.txt
).And run it from the
.bat
file like:If you really need to use a dynamic file name (from an environment variable), you need to create the text file on-the-fly. The
ftp
does not support variables.If you use a better FTP client, you might be able to both have the commands in the batch file and use the environment variables.
For example with WinSCP:
For an introduction to scripting with WinSCP see:
https://winscp.net/eng/docs/guide_automation
There's also a guide to Converting Windows FTP script to WinSCP script.
(I'm the author of WinSCP)