0

Is it possible to pipe in a .txt file as a command line argument to a batch file?

For example, I am looking to call

./program components.txt

Then use every line of components.txt as arguments to a function I call inside my batch file.

1 Answer 1

3

If args.txt contains a list of command-line arguments to your executable (one per line), you could use something like this:

    @echo off
    setlocal enableextensions enabledelayedexpansion
    set ARGS=
    for /f "delims=" %%a in ('type args.txt') do (
      set ARGS=!ARGS! %%a
    )
    .\program %ARGS%
    endlocal
Sign up to request clarification or add additional context in comments.

4 Comments

Does it matter if ./program is not an executable? What if it is a local command?
It doesn't matter whether program is an internal cmd.exe command or an executable. However if it is a shell script (batch file), use call to run it (e.g., call .\scriptname %ARGS%).
Sorry let me specify... I'm calling "p4 sync %ARGS%" on that line... NO executable is being used from an internal scope.
The for command reads the file args.txt and constructs the ARGS environment variable as a single space-delimited string containing the lines from the file (i.e., spaces between instead of newlines). What you do with that string is up to you.

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.