Search code examples
loopsbatch-fileftpdos

Batch Script FTP Looping Through Host List


I have the following batch script to FTP and pull back a file from a host. (ftpscript.bat)

@echo off
@ftp -i -s:"%~f0"&GOTO:EOF

open host1
username
password
cd apples
bin
hash
get fileName.txt fileName.host1
disconnect

I have the following text file that contains a list of hosts. (hosts.txt)

     host1
     host2
     host3
     host4

How can I loop through each host in the file, each line of the file, and run this script with the host variable in it?

This is an example of how I want it to look like when it runs:

@echo off
@ftp -i -s:"%~f0"&GOTO:EOF

open host1
username
password
cd apples
bin
hash
get fileName.txt fileName.host1
disconnect

open host2
username
password
cd apples
bin
hash
get fileName.txt fileName.host2
disconnect

open host3
username
password
cd apples
bin
hash
get fileName.txt fileName.host3
disconnect

open host4
username
password
cd apples
bin
hash
get fileName.txt fileName.host4
disconnect

Solution

  • If you want to append all the hosts you have in the hosts.txt to the existing ftpscript.bat script then this should work

    for /f %%a in (hosts.txt) do (
    echo open %%a
    echo username
    echo password
    echo cd apples
    echo bin
    echo hash
    echo get fileName.txt fileName.%%a
    echo disconnect
    echo.
    ) >>ftpscript.bat