Can't output certain line to batch file - 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?

>> 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

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.

Related

Batch not echoing single digit to file

In part of my code, I am trying to echo a single number to a file:
echo 1>test.txt
Except the command interpreter does not pick up the single digit, echoing
Echo is off.
to the file. Is there any way to work around this?
This is the easiest solution
>test.txt echo 1
Why does it solve the problem? What the problem is?
In batch files, input and output streams are numbered from 0 to 9: 0= stdin = default input stream, 1= stdout = default output stream, 2= stderr = default error stream and 3 to 9 available to the user.
The problem with your code is that the expression 1> file means redirect the stream 1 (standard output) to the file. The parser handles the digit as part of the redirection, not as data to send to the redirection.
To solve the problem it is necessary to separate the digit from the redirection operator, but something as echo 1 >test.txt will also output an aditional space, usually an undesired behaviour.
Changing how the command is written by reordering the line we can separate the digit and the redirection without any aditional element.
edited to adapt to comments
Can it be done without reordering?
Yes, you can also separate the command from the redirection operator enclosing the command in parenthesis
(echo 1)>test.txt
or you can add aditional indications to the parser, escaping the digit
echo ^1>test.txt
or use a for replaceable parameter to handle the digit, hiding it to the parser at redirection time
for %%a in (1) do echo %%a>test.txt
or, use delayed expansion, or .... virtually anything that ensures that the parser does not handle the digit as a stream number.

batch file echo line with 0 does not write to file

I have this in a batch file...
ECHO ActionNumber=0>> %wkdir%\some.ini
...problem is, it never gets written to the file but rather displayed on the console like this...
ActionNumber=
If I had...
ECHO ActionNumber=20>> %wkdir%\some.ini
...it get's written just fine
How can I write a line to this file that is simply "ActionNumber=0"
(without quotes, I'm just showing that it needs to be all one line with no spaces, no trailing space either)
>>%wkdir%\some.ini ECHO ActionNumber=20
Unfortunately, the space-after-digit solution echoes the space into the file, so you have trailing spaces.
(That's a gotcha with any digit immediately preceding a redirector)
The 0 before the < makes the parser thinks you are attempting to redirect stdin. Probably the simplest solution is to move the redirection as Peter Wright has done. Another option is to enclose the command in parentheses.
(ECHO ActionNumber=0)>> %wkdir%\some.ini
Use a Space after the 0
ECHO ActionNumber=0 >> %wkdir%\so
Edit: Thanks to #Christian K
0>> redirects to the Console stdin (stdout is 1 and stderr is 2)

is there a way to distinguish stderr in console output?

That is display both streams on console but distinguish somehow one from another.
Normally is that I don't know which lines are stderr they are mixed with normal output. If I redirect it to a file then I don't know when in relation to normal output the error happened. So is there a way so every line that comes from stderr would have some indicator like for example a string at the beginning like this:
c:\>command.exe
normal output
normal output
normal output
stderr: error message
normal output
stderr: error message
normal output
This is especially problem in compilers/make which spew lot of information and mix those two streams. I know I can prepend every line of text with given string using unix sed but I do not know how to use it with relation to main program. If I join two streams string will be prepended to every line. If I redirect stderr to file it won't be displayed on console + it will get out of context from stdout.
If the purpose is to distinguish error messages from normal output in the same screen, then it may be done via this trick:
anycompiler params ... 2>&1 1>&3 | findstr /N /A:4E "^"
This way the error messages appears preceded by a line number in yellow color on red background.
Previous answer extracted from: Batch - How can I redirect stderr and stdout from within the batch script itself?
If you redirect both outputs of previous line to a disk file, STDERR output will be preceded by a line number.

Batch use of | symbol

What is "|" symbol used for in batch?
Because its not a command I cant use | /? to find out what it does, and if I have something like Stackoverflow|Stackoverflow (as an example) I'm told "stackoverflow is not a recognized as an internal or external command, operable program or batch file"
It's the pipe operator, or redirect operator. From TechNet:
Reads the output from one command and writes it to the input of another command. Also known as a pipe.
The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. For example, the following command sorts a directory:
dir | sort
In this example, both commands start simultaneously, but then the sort command pauses until it receives the dir command's output. The sort command uses the dir command's output as its input, and then sends its output to handle 1 (that is, STDOUT).
The pipe operator | directs (pipes) the output of the first command to the standard input of the second command. So running
somecmd | anothercmd
Would first run somecmd, then gather the output of somecmd and run anothercmd, giving it the output of somecmd as input. You can read about it in innumerable places, just google "pipe command line" or something like it.
This character is used to chain commands.
There's a lot of documentation available on this. Wikipedia is probably a good place to start.

Messing up of non-typical characters in DOS

A command-line program outputs a list of file paths. I pipe the output to another program to do further processing. It all works fine until a file path is encountered that contains a "strange" character (ó, î, ä, etc.).
The cause of the problem seems to be the behavior described next:
Test 1
When I execute this command in cmd.exe,
C:\temp> echo ó > o.txt
the contents of the created file will be the character ¢.
Test 2
I created an input file, o_src.txt, (with my text editor) that contains the character ó.
The command
C:\temp> type o_src.txt
will print this character in cmd.exe
¾
If I execute
C:\temp>type o.txt
(o.txt from TEST 1) the result is ó
Test 3
After execution of the command
C:\temp> type o_src.txt > o_dst.txt
then the contents of o_src.txt and o_dst.txt are the same.
How does this work?
ASCII code of ó: F3 / 243
ASCII code of ¢: A2 / 162
ASCII code of ¾: BE / 190
I believe you need to change the code page using CHCP. Look at these questions for more info.
Batch script is not executed if chcp was called
What encoding/code page is cmd.exe using
Unicode characters in Windows command line - how?

Resources