JammerJoe Posted February 23, 2012 Posted February 23, 2012 (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.fileThat 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.fileIt 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 February 23, 2012 by JammerJoe
jaclaz Posted February 23, 2012 Posted February 23, 2012 (edited) Interesting. A workaround (just to solve the issue):SET InstallAsService=0&SET InstallAsService>>props.fileusing the SET command, and another one using redirection BEFORE:>>props.file ECHO InstallAsService=0method #3 here:http://www.robvanderwoude.com/redirection.phpjaclaz Edited February 23, 2012 by jaclaz
allen2 Posted February 23, 2012 Posted February 23, 2012 Or if you still want to keep the same kind of syntax :(echo InstallAsService=0)>> props.file
JammerJoe Posted February 23, 2012 Author Posted February 23, 2012 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.
jaclaz Posted February 23, 2012 Posted February 23, 2012 Or if you still want to keep the same kind of syntax :(echo InstallAsService=0)>> props.fileAdditional to allen2's nice suggestion , the redirection can be "multiline", like:(ECHO myothervar=1ECHO andyetanothersetting=0echo InstallAsService=0)>> props.filein this case, if you can do all the writing "together" you can also use the single ">" like in:(ECHO myothervar=1ECHO andyetanothersetting=0echo InstallAsService=0)>props.filejaclaz
Guest Posted February 24, 2012 Posted February 24, 2012 (edited) And to throw in my two cents, you could also do this:echo>>props.file InstallAsService=0 Edited February 24, 2012 by 5eraph
uid0 Posted February 24, 2012 Posted February 24, 2012 (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 February 24, 2012 by uid0
jaclaz Posted February 24, 2012 Posted February 24, 2012 (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.txtor:echo mytest=9>>mytest.txtthe 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.txtor:echo mytest=d>>mytest.txteverything reverts to "normal".So it is seemingly something that happens:with numbers onlywith a single number onlywith 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.txtTry running this:@ECHO OFFIF EXIST mytest.txt DEL mytest.txtECHO mytest=0>>mytest.txtECHO 0&TYPE mytest.txt&PAUSEECHO mytest=1>>mytest.txtECHO 1&TYPE mytest.txt&PAUSEECHO mytest=2>>mytest.txtECHO 2&TYPE mytest.txt&PAUSEECHO mytest=3>>mytest.txtECHO 3&TYPE mytest.txt&PAUSEECHO mytest=4>>mytest.txtECHO 4&TYPE mytest.txt&PAUSEECHO mytest=5>>mytest.txtECHO 5&TYPE mytest.txt&PAUSEECHO mytest=6>>mytest.txtECHO 6&TYPE mytest.txt&PAUSEECHO mytest=7>>mytest.txtECHO 7&TYPE mytest.txt&PAUSEECHO mytest=8>>mytest.txtECHO 8&TYPE mytest.txt&PAUSEECHO mytest=9>>mytest.txtECHO 9&TYPE mytest.txt&PAUSEECHO mytest=10>>mytest.txtECHO 10&TYPE mytest.txt&PAUSEECHO mytest=11>>mytest.txtECHO 11&TYPE mytest.txt&PAUSEAnd watch attentively the behaviour.EDIT:Nice different behaviour also with DELAYEDEXPANSION:@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET zero=0SET one=1SET two=2SET three=3ECHO with "%%"IF EXIST mytest.txt DEL mytest.txtECHO mytest=%zero%>>mytest.txtECHO 0&TYPE mytest.txtECHO mytest=%one%>>mytest.txtECHO 1&TYPE mytest.txtECHO mytest=%two%>>mytest.txtECHO 2&TYPE mytest.txtECHO mytest=%three%>>mytest.txtECHO 3&TYPE mytest.txtPAUSEECHO Now with "^!"IF EXIST mytest.txt DEL mytest.txtECHO mytest=!zero!>>mytest.txtECHO 0&TYPE mytest.txtECHO mytest=!one!>>mytest.txtECHO 1&TYPE mytest.txtECHO mytest=!two!>>mytest.txtECHO 2&TYPE mytest.txtECHO mytest=!three!>>mytest.txtECHO 3&TYPE mytest.txtPAUSEjaclaz Edited February 24, 2012 by jaclaz
gunsmokingman Posted February 24, 2012 Posted February 24, 2012 Since I am not CMD Script expert Example 1@Echo OffCLSecho mytest0=0>mytest.txtecho mytest1=1>>mytest.txtecho mytest2=2>>mytest.txtecho mytest3=3>>mytest.txtecho mytest4=4>>mytest.txtecho mytest5=5>>mytest.txtecho mytest6=6>>mytest.txtecho mytest7=7>>mytest.txtecho mytest8=8>>mytest.txtecho mytest9=9>>mytest.txtWould produce this text outputmytest1=Add this ^ before the numbers@Echo OffCLSecho mytest0=^0>mytest.txtecho mytest1=^1>>mytest.txtecho mytest2=^2>>mytest.txtecho mytest3=^3>>mytest.txtecho mytest4=^4>>mytest.txtecho mytest5=^5>>mytest.txtecho mytest6=^6>>mytest.txtecho mytest7=^7>>mytest.txtecho mytest8=^8>>mytest.txtecho mytest9=^9>>mytest.txtProduces this text outputmytest0=0mytest1=1mytest2=2mytest3=3mytest4=4mytest5=5mytest6=6mytest7=7mytest8=8mytest9=9
bphlpt Posted February 25, 2012 Posted February 25, 2012 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+0mytest1+1mytest2+2mytest3+3mytest4+4mytest5+5mytest6+6mytest7+7mytest8+8mytest9+9So 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
jaclaz Posted February 25, 2012 Posted February 25, 2012 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 )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=valuethrough 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
gunsmokingman Posted February 25, 2012 Posted February 25, 2012 For the record I tested the CMD script on Windows 8 Developers Preview and Windows 7 with the same results.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now