Robert Holland
Robert Holland

Reputation: 21

Can't output certain line to batch file

I'm using a batch file to create an RDP file using various variables to populate the contents.

Every line uses an Echo command and then outputs to a file with >> For instance -

@echo screen mode id:i:1>> "C:\TEMP\file.RDP"
@echo use multimon:i:1>> "C:\TEMP\file.RDP"

Whilst this works for every line, one single line is giving me a problem and will not output -

@echo selectedmonitors:s:2,0>> "C:\TEMP\file.RDP"

For some reason, this line actually outputs selectedmonitors:s:2, (the 0 disappears) to the command window and outputs nothing into the .RDP file. Whilst @echo selectedmonitors:s:2,0 works in the command window and outputs as expected, I can't output to a file. What's going wrong here?

Upvotes: 1

Views: 58

Answers (2)

Stephan
Stephan

Reputation: 56180

>> is just an abbreviation for 1>>, where 1 is an output stream.
There are ten of those streams:
0 is STDIN (Input) and not allowed for redirecting output.
1 is STDOUT("normal" output), 2 is STDERR (error output)
and 3 to 9 are not defined (but usable).

Remove the @ to see what happens:

A batch file like

echo selectedmonitors:s:2,0>>file.txt

shows as executed command:

echo selectedmonitors:s:2, 0>>file.txt

which tries to redirect Stream 0 (STDIN) (which holds nothing here) to the file.

The reason your other lines are working is that , is a standard delimiter and : is not, so the comma snips the zero off the echo and adds it to the redirection while the colon doesn't.

Two possible workarounds:

>>file.txt echo selectedmonitors:s:2,0
(echo selectedmonitors:s:2,0)>>file.txt

Upvotes: 2

dxiv
dxiv

Reputation: 17638

Try the following, instead.

@(echo selectedmonitors:s:2,0)>> "C:\TEMP\file.RDP"

Without the parentheses, and since the , comma works as a delimiter in batch command lines (see cmd- comma to separate parameters Compared to space? for example) the last token in the command is parsed as 0>> "C:\TEMP\file.RDP" which literally means "append stream 0 output to the given file". Since stream 0 is the standard input stream, this is invalid and ignored, but the "0" parameter gets "eaten" in the process.

Upvotes: 0

Related Questions