Pradeep Vairamani
Pradeep Vairamani

Reputation: 4302

Redirecting command input using <

Input redirection is working for .exe files or internal windows commands.

    app.exe < ListOfNames.txt
    sort < input.txt

However it isn't working when I try to redirect it into a batch script.

test.bat :-

 @echo off
 echo %1 %2

Running it using :-

test.bat<input.txt

where input.txt has two strings.

However, it is working fine for redirecting output even in case of batch scripts.

Is this the expected behavior or I am making some syntax mistake? Is there any other way to read arguments from a file instead of manually parsing it?

Upvotes: 1

Views: 9840

Answers (2)

jeb
jeb

Reputation: 82247

Additionally to dbenhams answer, you could also read multiple lines with set/p for a input redirection, like myBatch.bat < commands.txt

@echo off
set "line_1="
set "line_2="
set "line_3="
set /p line_1=
set /p line_2=
set /p line_3=
set line_

But this would fail with an input pipe like type commands.txt | myBatch.bat

Upvotes: 0

dbenham
dbenham

Reputation: 130819

Parameters that are provided on the command line are completely different than stdin ( where your redirected input goes). This is true for both batch scripts as well as .exe programs.

Some programs are designed to accept the same values via command line arguments or stdin. But that is not the norm. That is a feature that is provided by the developer of the program.

If you want to read redirected input within a batch script, then you must do one of the following.

To read a single line:

set /p "ln="
echo %ln%

To read all lines in a loop:

for /f "delims=" %%A in ('findstr "^"') do (
  echo %%A
)

Upvotes: 3

Related Questions