1

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.

ruaok
  • 45
  • 6
  • It's not clear what you ask for. Can you show us your complete code and the way you run it? It may help us understand what you want. – Martin Prikryl May 13 '16 at 15:38
  • "I would like to know how to build a loop that runs the ftp commands and passes variables to it." Seems clear he is looking for info on `FOR` loops. How to read from a file is beyond (anymore) my .cmd-fu :-/ Good luck to all – shellter May 13 '16 at 17:13
  • That is correct, I'm looking for info on running a for loop and also passing arguments to that loop from the windows command line. I do not have any example code, because I'm unfamiliar with this particular environment. – ruaok May 16 '16 at 20:13

1 Answers1

0

The simplest way is to create a sub-function in a batch file and call it multiple times with different arguments. This way you do not need any external configuration file and hence no loop:

@echo off

call :download ftp.domain1.com username1 password1
call :download ftp.domain2.com username2 password2

exit

:download
echo open %1>ftp.txt
echo %2>>ftp.txt
echo %3>>ftp.txt
echo bye>>ftp.txt
ftp.exe -s:ftp.txt
del ftp.txt

exit /b

If you really need to configure the parameters in an external file use for command to read the file:

@echo off

FOR /F "tokens=1,2,3" %%i in (config.txt) do call :download %%i %%j %%k

exit

:download
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

ftp.exe -s:ftp.txt

del ftp.txt

exit /b

Where the config.txt has the format your wanted:

ftp.domain1.com username1 password1
ftp.domain2.com username2 password2
Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Great, I can give this a try. Any ideas on how to quickly verify that the file was successfully uploaded and is identical to the local file without outputting the entire content of the file to the command line window. Basically, looking for a way to do a "diff" and validate that the files contain the same data. – ruaok May 18 '16 at 21:01
  • Using the batch file recommendation above, the file "ftp.txt" created contains: ` open ECHO is off. ECHO is off. cd /public_html bin hash put "test.txt" close bye ` – ruaok May 18 '16 at 22:39
  • diff: That's a different question, ask it. – Martin Prikryl May 19 '16 at 05:48
  • Which batch file did you try? What version of Windows? How does your `config.txt` look like? – Martin Prikryl May 19 '16 at 05:49
  • I'm running on windows 10. I tried with the second batch file which passes arguments from the `config.txt` file into the subroutine. The `config.txt` file looks like: `ftp.domain1.com username1 password1 ftp.domain2.com username2 password2` Sorry having some trouble with the markdown, but each domain, username, password is on a newline. I put in some debug code to look at the arguments as they are created from the FOR loop before they are passed to the subroutine and they are correct, but once passed into the sub, they seem to produce the `ECHO is off` statements. Thoughts? – ruaok May 21 '16 at 21:39
  • Can you append your full batch file with debug code to your question? – Martin Prikryl May 22 '16 at 05:45