0

I have a CSV file with a list of IP addresses and URL's for specified folders within that ftp.

I am using a tool to do work with the files converting them from one type to another and reloading back to the source.

The batch in it's entirety works fine, but I am needing to get the CSV imported and then the script to loop through its processes for each line in the CSV. As of now, it only uses the last line of the information.

SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=1-2 delims=," %%a in (Convert.csv) do (
    set IPAddress=%%a
    set ProjectURL=%%b
)

ECHO Retrieving File
start /wait tool.exe get "ssh %IPAddress%" "%~dp0%IPAddress%\Backup" "%~dp0%IPAddress%\Report\Backup" --ctrl_path="%ProjectURL%"
ECHO Retrieved
Timeout 2 > NUL
ECHO Please wait as we... Convert file after retrieval
ECHO Converting File
start /wait tool.exe convert "%~dp0%IPAddress%\Backup" "%~dp0%IPAddress%\Converted" "%~dp0%IPAddress%\Report\Conversion" "%~dp0HTML.xml" --cnx="%~dp0CNX.xml"
ECHO Converted
Timeout 2 > NUL
ECHO Please wait as we... Push the file
ECHO Pushing File
start /wait tool.exe put "ssh %IPAddress%" "%~dp0%IPAddress%\Converted" "%~dp0%IPAddress%\Report\Placement" --ctrl_path="%ProjectURL%"
ECHO Completed

I understand that this pulls and it shows the variables updating, but I need the rest of the script to run for each line that is read and processed in this manner.

I have tried encompassing the entire script within the For /F loop and it does not seem to wait for the commands within to complete. It blasts through for each line of the CSV and thus does nothing at all.

Is there a looping mechanism to do this within batch?

I did get it to work by putting the entire code section within the loop and changing the variables inline to !IPAddress! and !ProjectURL! respectively, but it seems janky? Is there a cleaner method? This is what I currently have, it works but isn't at all pretty.

SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=1-2 delims=," %%a in (Convert.csv) do (
    set IPAddress=%%a
    set ProjectURL=%%b

ECHO Retrieving File
start /wait tool.exe get "ssh !IPAddress!" "%~dp0!IPAddress!\Backup" "%~dp0!IPAddress!\Report\Backup" --ctrl_path="!ProjectURL!"
ECHO Retrieved
Timeout 2 > NUL
ECHO Please wait as we... Convert file after retrieval
ECHO Converting File
start /wait tool.exe convert "%~dp0!IPAddress!\Backup" "%~dp0!IPAddress!\Converted" "%~dp0!IPAddress!\Report\Conversion" "%~dp0HTML.xml" --cnx="%~dp0CNX.xml"
ECHO Converted
Timeout 2 > NUL
ECHO Please wait as we... Push the file
ECHO Pushing File
start /wait tool.exe put "ssh !IPAddress!" "%~dp0!IPAddress!\Converted" "%~dp0!IPAddress!\Report\Placement" --ctrl_path="!ProjectURL!"
ECHO Completed
)
1
  • Why do you use start /WAIT to run an executable? why not just running tool.exe? is tool.exe a console application? Commented Dec 4, 2020 at 10:09

1 Answer 1

1
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=1-2 delims=," %%a in (Convert.csv) do (
 rem   set IPAddress=%%a
 rem   set ProjectURL=%%b

ECHO Retrieving File
start /wait tool.exe get "ssh %%a" "%~dp0%%a\Backup" "%~dp0%%a\Report\Backup" --ctrl_path="%%b"
ECHO Retrieved
Timeout 2 > NUL
ECHO Please wait as we... Convert file after retrieval
ECHO Converting File
start /wait tool.exe convert "%~dp0%%a\Backup" "%~dp0%%a\Converted" "%~dp0%%a\Report\Conversion" "%~dp0HTML.xml" --cnx="%~dp0CNX.xml"
ECHO Converted
Timeout 2 > NUL
ECHO Please wait as we... Push the file
ECHO Pushing File
start /wait tool.exe put "ssh %%a" "%~dp0%%a\Converted" "%~dp0%%a\Report\Placement" --ctrl_path="%%b"
)
ECHO Completed

That is, simply replace %IPAddress% withe %%a and %ProjectURL% with %%b within the loop. %%a and %%b are simply strings with no magical properties and and in-context within the parentheses defining the do action.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Magoo. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.