Jump to content

Recommended Posts

Posted (edited)

Hello all, ran into something peculiar...within a Windows/DOS batch I have to write a series of key-value pairs to disk. No a big problem except when I do this:

echo InstallAsService=0>> props.file

That results in the line being displayed at the command line (without the zero) and NOT written to the file.

I tried this (adding a space in between the 0 and >>)

echo InstallAsService=0 >> props.file

It gets written to file. Unfortunately the consumer of the file is expecting a 0 with no spaces right after it. So of course he doesn't pickup the zero.

So the question is how can I write a key value pair to a file where the value is zero and have no trailing space after it? Seems quite doable but it's not immediately obvious to me. Thanks in advance.

Edited by JammerJoe

Posted

Just tried allen2 's suggestion and that seemed to do the trick (will try jaclaz's in a moment). Thank you both for your help.

Posted

Or if you still want to keep the same kind of syntax :

(echo InstallAsService=0)>> props.file

Additional to allen2's nice suggestion :thumbup , the redirection can be "multiline", like:

(
ECHO myothervar=1
ECHO andyetanothersetting=0
echo InstallAsService=0
)>> props.file

in this case, if you can do all the writing "together" you can also use the single ">" like in:

(
ECHO myothervar=1
ECHO andyetanothersetting=0
echo InstallAsService=0
)>props.file

jaclaz

Posted (edited)

And to throw in my two cents, you could also do this:

echo>>props.file InstallAsService=0

Edited by 5eraph
Posted (edited)

I guess it's because 0 has a special meaning, like 1 and 2 are stdout and stderr.

Jaclaz, any idea what 0 means?

Edited by uid0
Posted (edited)

I guess it's because 0 has a special meaning, like 1 and 2 are stdout and stderr.

Jaclaz, any idea what 0 means?

No, I guess that is one of the "quirks" of batch/command line parsing, if you try:

echo mytest=3>>mytest.txt

or:

echo mytest=9>>mytest.txt

the behaviour is the same,

BUT if you try:

echo mytest=1>>mytest.txt

(which is the redirection you suspect, the result is "mytest=" echoed to the file, using 2 works like the other bigger numbers and 0, but in the case of 2, as well as of 1 it is the "expected behaviour")

AND if you try:

echo mytest=10>>mytest.txt

or:

echo mytest=d>>mytest.txt

everything reverts to "normal".

So it is seemingly something that happens:

  1. with numbers only
  2. with a single number only
  3. with number 0 and numbers 3÷9 only (the behaviour with 1 and 2 is "as expected")

The same issue happens also if you put (.ini style) a space on both sides of the = sign:

echo mytest  = 0>>mytest.txt

Try running this:

@ECHO OFF
IF EXIST mytest.txt DEL mytest.txt

ECHO mytest=0>>mytest.txt
ECHO 0&TYPE mytest.txt&PAUSE
ECHO mytest=1>>mytest.txt
ECHO 1&TYPE mytest.txt&PAUSE
ECHO mytest=2>>mytest.txt
ECHO 2&TYPE mytest.txt&PAUSE
ECHO mytest=3>>mytest.txt
ECHO 3&TYPE mytest.txt&PAUSE
ECHO mytest=4>>mytest.txt
ECHO 4&TYPE mytest.txt&PAUSE
ECHO mytest=5>>mytest.txt
ECHO 5&TYPE mytest.txt&PAUSE
ECHO mytest=6>>mytest.txt
ECHO 6&TYPE mytest.txt&PAUSE
ECHO mytest=7>>mytest.txt
ECHO 7&TYPE mytest.txt&PAUSE
ECHO mytest=8>>mytest.txt
ECHO 8&TYPE mytest.txt&PAUSE
ECHO mytest=9>>mytest.txt
ECHO 9&TYPE mytest.txt&PAUSE
ECHO mytest=10>>mytest.txt
ECHO 10&TYPE mytest.txt&PAUSE
ECHO mytest=11>>mytest.txt
ECHO 11&TYPE mytest.txt&PAUSE

And watch attentively the behaviour.

EDIT:

Nice different behaviour also with DELAYEDEXPANSION:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET zero=0
SET one=1
SET two=2
SET three=3

ECHO with "%%"
IF EXIST mytest.txt DEL mytest.txt
ECHO mytest=%zero%>>mytest.txt
ECHO 0&TYPE mytest.txt
ECHO mytest=%one%>>mytest.txt
ECHO 1&TYPE mytest.txt
ECHO mytest=%two%>>mytest.txt
ECHO 2&TYPE mytest.txt
ECHO mytest=%three%>>mytest.txt
ECHO 3&TYPE mytest.txt
PAUSE

ECHO Now with "^!"
IF EXIST mytest.txt DEL mytest.txt
ECHO mytest=!zero!>>mytest.txt
ECHO 0&TYPE mytest.txt
ECHO mytest=!one!>>mytest.txt
ECHO 1&TYPE mytest.txt
ECHO mytest=!two!>>mytest.txt
ECHO 2&TYPE mytest.txt
ECHO mytest=!three!>>mytest.txt
ECHO 3&TYPE mytest.txt
PAUSE

jaclaz

Edited by jaclaz
Posted

Since I am not CMD Script expert

Example 1


@Echo Off
CLS
echo mytest0=0>mytest.txt
echo mytest1=1>>mytest.txt
echo mytest2=2>>mytest.txt
echo mytest3=3>>mytest.txt
echo mytest4=4>>mytest.txt
echo mytest5=5>>mytest.txt
echo mytest6=6>>mytest.txt
echo mytest7=7>>mytest.txt
echo mytest8=8>>mytest.txt
echo mytest9=9>>mytest.txt

Would produce this text output

mytest1=

Add this ^ before the numbers


@Echo Off
CLS
echo mytest0=^0>mytest.txt
echo mytest1=^1>>mytest.txt
echo mytest2=^2>>mytest.txt
echo mytest3=^3>>mytest.txt
echo mytest4=^4>>mytest.txt
echo mytest5=^5>>mytest.txt
echo mytest6=^6>>mytest.txt
echo mytest7=^7>>mytest.txt
echo mytest8=^8>>mytest.txt
echo mytest9=^9>>mytest.txt

Produces this text output

mytest0=0

mytest1=1

mytest2=2

mytest3=3

mytest4=4

mytest5=5

mytest6=6

mytest7=7

mytest8=8

mytest9=9

Posted

Could this be at all OS related? I tried GSM's first script exactly. I also got only "mytest1=" sent to mytest.txt, but I got

mytest0=

mytest2=

mytest3=

mytest4=

mytest5=

mytest6=

mytest7=

mytest8=

mytest9=

echoed to the screen.

Then, on a hunch, I changed the "x=x" to "x+x", ie I changed the equals sign to a plus sign, and reran the script. I didn't get anything echoed to the screen and in mytest.txt I now had:

mytest0+0

mytest1+1

mytest2+2

mytest3+3

mytest4+4

mytest5+5

mytest6+6

mytest7+7

mytest8+8

mytest9+9

So it's not just the number. The character before the number effects things as well. If it was just the =0 that acted "funny" I was going to suggest it was interpreting it as octal somehow, but now I'm confused again. No matter how long CMD script has been around I'm still learning quirks that is has.

Cheers and Regards

Posted

Could this be at all OS related? I tried GSM's first script exactly. I also got only "mytest1=" sent to mytest.txt, but I got

....

echoed to the screen.

That's exactly the behaviour I described earlier (which I tested on XP). Gunsmokingman introduced a "variation" on the theme adding a number to the name of the item that may have confused you (or I confused you by NOT adding the number to the name and echoing instead the number :ph34r: )

Then, on a hunch, I changed the "x=x" to "x+x", ie I changed the equals sign to a plus sign, and reran the script. I didn't get anything echoed to the screen and in mytest.txt I now had:

.......

So it's not just the number. The character before the number effects things as well. If it was just the =0 that acted "funny" I was going to suggest it was interpreting it as octal somehow, but now I'm confused again.

Yes, the point - as I see it - is that creating a .ini stile file, i.e. with lines of the type:

item=value

through ECHO should be a "common enough" kind of task, and it is strange that noone around here had noticed this strange behaviour,

No matter how long CMD script has been around I'm still learning quirks that is has.

Yep, same here, and IMHO it is the "fun" part of it!

jaclaz

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...