Jon Craigs
Jon Craigs

Reputation: 109

How to use a txt file that has a list of filenames as input, to download those files from FTP server?

I have a basic batch file that connects to an FTP server, and saves the list off all the csv files as a txt file on my laptop.
Later, I will want to download all the files on that list (using each line of the txt file as the input to a batch file), and also 'move' each file on the FTP server to a different folder on the FTP server.

List.txt contains:

 A.csv
 B.csv
 C.csv
 etc...

Batch file uses List.txt as the input:

  1. Connect to the FTP server
  2. Then for each line of List.txt
  3. GET filename
  4. REN filename /otherfolder/filename
  5. close the connection

I can connect to the FTP server, but when I try to use the for /f %%i in the batch it executes it outside the FTP shell.

Open example.com
user
password
binary
for /f %%i in "List.txt" do get %%i
disconnect
bye

Upvotes: 1

Views: 2517

Answers (2)

Paul
Paul

Reputation: 2710

@echo off
rem making the ftp script
(
  echo Open example.com
  echo user
  echo password
  echo binary
)>ftp.txt
for /f "delims=" %%i in (List.txt) do echo get %%i>>ftp.txt
(
  echo disconnect
  echo bye
)>>ftp.txt

rem connecting to ftp
ftp -s:ftp.txt

Edit:
Resource you must read https://technet.microsoft.com/en-us/library/cc959810.aspx

sample:

!     Runs the specified command on the local computer.  
?     Displays descriptions for Ftp commands. ? is identical to help .  
Mget  Copies multiple remote files to the local host using the current file transfer type.

Upvotes: 3

Jon Craigs
Jon Craigs

Reputation: 109

Thanks to Paul's answer, I realized my issue. This is what I did:

@echo off
REM Enter the username
echo user USERNAME> ftpcmd.dat

REM Enter the password
echo PASSWORD>> ftpcmd.dat

REM Change the local computers' directory
echo lcd D:\LocalFolder\>> ftpcmd.dat

REM create the list of commands to download the csv files, and then
REM 'move' them to the Backup folder
for /f "usebackq delims=" %%i in ("d:\LocalFolder\List.txt") do (echo get %%i>>ftpcmd.dat) && (echo ren %%i /Backup/%%i>>ftpcmd.dat)

REM Close the connection
echo quit >> ftpcmd.dat

REM use -d for debugging, -i for preventing user interaction questions
ftp  -i -n -s:ftpcmd.dat ftp.example.com

Upvotes: 2

Related Questions