1

H guys, using a windows batch script I am looking to pass a run a command x number of times with a different argument each time, the arguments being parsed in from a file.

For example have a textfile saying arg1 arg 2 arg3, the batch script would parse this and run

program.exe -arg1
program.exe -arg2
program.exe -arg3

I am thinking reading the file line by line into an array then doing a for each loop, but have no experience with windows scripting to do so

2 Answers 2

2

Alright, here we go... recall.bat. You always need to provide 1 argument - the executable to call each time. You also need to create an argument file: args.txt by default which should have one argument per line. If the argument has spaces or special characters it should be quote escaped.

Source: recall.bat

@echo off
setLocal EnableDelayedExpansion

::: recall.bat - Call an executable with a series of arguments
:::  usage: recall $exec [$argfile]
:::     exec - the executable to recall with arguments
:::     argfile - the file that contains the arguments, if empty will 
:::               default to args.txt
:::  argfile format:
:::    One argument per line, quote-escaped if there's spaces/special chars
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF

set argfile=args.txt
:: Reset argfile if supplied.
if "%~2" neq "" set argfile="%~2"
:: Remove quotes
set argfile=!argfile:"=!

for /f "tokens=*" %%G in (%argfile%) do (
  call %1 %%G
)

Example:

args.txt

hello
world
"hello world"

Call:

recall echo args.txt

Output:

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

Comments

1

You should be able to use a for loop. Assuming the file args.txt contains the parameters, then this should work:

for /f %a in (args.txt) do program.exe -%a

Edit Depending on the command processor you are using, you may need to use two % symbols in a row in the command if you run the above statement in a batch file. It is not necessary when using the very nice JP Software command prompt. I think, though, it is necessary with the default cmd.exe/command.com prompts.

for /f %%a in (args.txt) do program.exe -%%a

Comments

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.