Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ftp with a batch file?

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?

like image 621
user1935683 Avatar asked Apr 22 '13 22:04

user1935683


People also ask

How do I automate an FTP file transfer?

Generating script Select the files you want to transfer. Use one of the file transfer commands: Upload, Download, Upload and Delete, Download and Delete. On the transfer confirmation dialog, setup transfer options (if you need any non default settings). Use the Transfer Settings > Generate Code command.


2 Answers

The answer by 0x90h helped a lot...

I saved this file as u.ftp:

open 10.155.8.215  user password lcd /D "G:\Subfolder\" cd  folder/ binary mget file.csv disconnect quit 

I then ran this command:

ftp -i -s:u.ftp 

And it worked!!!

Thanks a lot man :)

like image 154
Outlier Avatar answered Sep 21 '22 17:09

Outlier


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:

@echo off  REM Generate the script. Will overwrite any existing temp.txt echo open servername> temp.txt echo username>> temp.txt echo password>> temp.txt echo get %1>> temp.txt echo quit>> temp.txt  REM Launch FTP and pass it the script ftp -s:temp.txt  REM Clean up. del temp.txt 

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.

like image 35
0x90h Avatar answered Sep 22 '22 17:09

0x90h