I would like to build a script that runs from the Windows command line to update an existing file to multiple web domains using FTP. I would like to know how to build a loop that runs the ftp commands and passes variables to it.
The file that passes the arguments to the FTP script will be in the format of:
ftp.domain1.com username1 password1
ftp.domain2.com username2 password2
and so forth.
The FTP script will have the following commands:
open "$variable_for_domain_name"
$variable_for_username
$variable_for_password
cd /public_html
bin
hash
put "test.txt"
close
bye
Using this example, the first iteration of the loop would execute the following FTP commands:
open "ftp.domain1.com"
username1
password1
cd /public_html
bin
hash
put "test.txt"
close
bye
and the second iteration of the loop would execute the following:
open "ftp.domain2.com"
username2
password2
cd /public_html
bin
hash
put "test.txt"
close
bye
I understand to run the script once I would execute:
c:\windows\system32\ftp.exe -i <ftp_script.txt
Where ftp_script.txt
contains the above FTP commands with the values stated explicitly. How can the loop be done so that I can execute one command from the windows command line but update files on multiple domains?
Also, I would like to add a command to the FTP script to verify the new contents of test.txt
.
The current version of the batch file I'm running is: `
@echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=1,2,3 delims= " %%a in (ftp_config.txt) do (
echo %%a
echo %%b
echo %%c
call :SUB_ftp_cmd %%a %%b %%c
)
::exit
:SUB_ftp_cmd
echo open %1>ftp.txt
echo %2>>ftp.txt
echo %3>>ftp.txt
echo cd /public_html>>ftp.txt
echo bin>>ftp.txt
echo hash>>ftp.txt
echo put "test.txt">>ftp.txt
echo close>>ftp.txt
echo bye>>ftp.txt
::c:\windows\system32\ftp.exe -i <ftp.txt
::del ftp.txt
::exit /b
`
The resulting file ftp.txt
contains the following:
`
open
ECHO is off.
ECHO is off.
cd /public_html
bin
hash
put "test.txt"
close
bye
`
I added the setlocal EnableDelayedExpansion
to try and resolve the fact that the variables are not correctly being passed to the subroutine, but it did not change the result.