Soumya ranjan Rout
Soumya ranjan Rout

Reputation: 3

looping Batch file from text input

I am using this code to run a batch file with arguments from a text file . but it is taking only last value from input file.

@echo off 

cd /d C:\Users\infodba\Desktop\Export


for /f "eol=; tokens=1,2 delims=," %%a in (Input.txt) do
 ( 
    set ITEMID=%%a
    set REV=%%b
    call :write %%a %%b
)

:write
echo ITEMID=%ITEMID%, REV=%REV%

cd /d C:\Program Files\Siemens\NX 8.5\UGMANAGER
ug_export -part=%ITEMID% -rev=%REV% -u=arvind -p=asfa@p -role=dba -export_dir=C:\Users\infodba\Desktop\Export

exit /B 0

Upvotes: 0

Views: 369

Answers (1)

aschipfl
aschipfl

Reputation: 34899

You need to add an exit /B command after the closing ) to not fall into the :write subroutine unintentionally after the main program (for loop) has finished.
The opening ( of the for body needs to be in the same line as the do keyword.

You do not need to set ITEMID and REV as you are passing %%a and %%b to your subroutine.
Within :write use %1 to access the first parameter and %2 for the second one.

This should work:

@echo off 

pushd "C:\Program Files\Siemens\NX 8.5\UGMANAGER"
for /f "usebackq eol=; tokens=1,2 delims=," %%a in (
    "C:\Users\infodba\Desktop\Export\Input.txt"
) do (
    call :write "%%~a" "%%~b"
)
popd
exit /B

:write
echo ITEMID="%~1", REV="%~2"

ug_export -part="%~1" -rev="%~2" -u=arvind -p=asfa@p -role=dba -export_dir=C:\Users\infodba\Desktop\Export

exit /B 0

If the tool ug_export does not support -part and -rev arguments to be quoted, write ug_export -part=%~1 -rev=%~2 ... instead.

I avoided changing to the directory path ug_export for every execution of :write by changing to it once in the main section (using pushd and popd).

Hint: For debugging it is very helpful to remove @echo off temporarily...

Upvotes: 1

Related Questions