MSFN Forum: 0>> filename vs 0 >> filename - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

0>> filename vs 0 >> filename Rate Topic: -----

#1 User is offline   JammerJoe 

  • Group: Members
  • Posts: 3
  • Joined: 23-February 12
  • OS:none specified
  • Country: Country Flag

Posted 23 February 2012 - 01:59 PM

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.

This post has been edited by JammerJoe: 23 February 2012 - 02:00 PM



#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 23 February 2012 - 02:44 PM

Interesting. :unsure:

A workaround (just to solve the issue):
SET InstallAsService=0&SET InstallAsService>>props.file

using the SET command, and another one using redirection BEFORE:
>>props.file ECHO InstallAsService=0

method #3 here:
http://www.robvander...redirection.php

jaclaz

This post has been edited by jaclaz: 23 February 2012 - 02:50 PM


#3 User is offline   allen2 

  • Not really Newbie
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,731
  • Joined: 13-January 06

Posted 23 February 2012 - 02:53 PM

Or if you still want to keep the same kind of syntax :
(echo InstallAsService=0)>> props.file


#4 User is offline   JammerJoe 

  • Group: Members
  • Posts: 3
  • Joined: 23-February 12
  • OS:none specified
  • Country: Country Flag

Posted 23 February 2012 - 02:59 PM

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.

#5 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 23 February 2012 - 03:03 PM

View Postallen2, on 23 February 2012 - 02:53 PM, said:

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

#6 User is offline   JammerJoe 

  • Group: Members
  • Posts: 3
  • Joined: 23-February 12
  • OS:none specified
  • Country: Country Flag

Posted 23 February 2012 - 03:32 PM

Good stuff. Thanks again!

#7 User is offline   5eraph 

  • Update Packrat
  • Group: Supreme Sponsor
  • Posts: 954
  • Joined: 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag

Posted 23 February 2012 - 07:21 PM

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

echo>>props.file InstallAsService=0

This post has been edited by 5eraph: 23 February 2012 - 07:21 PM


#8 User is offline   uid0 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 348
  • Joined: 12-June 06

Posted 24 February 2012 - 07:57 AM

I guess it's because 0 has a special meaning, like 1 and 2 are stdout and stderr.
Jaclaz, any idea what 0 means?

This post has been edited by uid0: 24 February 2012 - 07:58 AM


#9 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 24 February 2012 - 09:20 AM

View Postuid0, on 24 February 2012 - 07:57 AM, said:

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:
  • with numbers only
  • with a single number only
  • 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

This post has been edited by jaclaz: 24 February 2012 - 11:29 AM


#10 User is online   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 24 February 2012 - 12:56 PM

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

Quote

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

Quote

mytest0=0
mytest1=1
mytest2=2
mytest3=3
mytest4=4
mytest5=5
mytest6=6
mytest7=7
mytest8=8
mytest9=9


#11 User is offline   bphlpt 

  • MSFN Expert
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,075
  • Joined: 12-May 07

Posted 25 February 2012 - 06:44 AM

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

Quote

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:

Quote

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

#12 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 25 February 2012 - 11:34 AM

View Postbphlpt, on 25 February 2012 - 06:44 AM, said:

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

View Postbphlpt, on 25 February 2012 - 06:44 AM, said:

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,

View Postbphlpt, on 25 February 2012 - 06:44 AM, said:

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

#13 User is online   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 25 February 2012 - 12:07 PM

For the record I tested the CMD script on Windows 8 Developers Preview and Windows 7 with the same results.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy