1.

Solve : Problem Echoing a Digit to a File?

Answer» <html><body><p>Hi,<br/><br/>I <a href="https://interviewquestions.tuteehub.com/tag/apologize-363663" style="font-weight:bold;" target="_blank" title="Click to know more about APOLOGIZE">APOLOGIZE</a> if this has been covered before, but I've been searching for 2 days for a solution to no avail. I've wasted enough time on it, so I'm going to go ahead and ask.<br/><br/>The root of the problem is this:<br/><br/>echo 9&gt; myfile.txt<br/><br/>The 9 (or any single digit for that matter) and the &gt; next to each other cause the command to be interpreted incorrectly.<br/><br/><br/>I've found it easy enough to overcome this by "escaping" the 9, ie:<br/><br/>echo ^9&gt; myfile.txt<br/><br/>However, here's the rub, what I'm actually <a href="https://interviewquestions.tuteehub.com/tag/trying-3234509" style="font-weight:bold;" target="_blank" title="Click to know more about TRYING">TRYING</a> to do is echo a variable. The variable is set by the user with the SET /P command. The user is prompted to type in the path to their Flight Simulator <a href="https://interviewquestions.tuteehub.com/tag/installation-16361" style="font-weight:bold;" target="_blank" title="Click to know more about INSTALLATION">INSTALLATION</a> which is then set as the variable %fspath%. To avoid the user having to type the path each time he runs the .bat file, I'm attempting to store it (along with some other variables) in an .ini file. When the .bat file is run, the .ini is copied to temp.bat which is then called by the main .bat to set the variables. Something like this:<br/><br/>:start<br/>if not exist myfile.ini goto firstrun<br/>copy myfile.ini temp$$$.bat<br/>call temp$$$.bat<br/>del temp$$$.bat<br/>goto menu1<br/><br/>:firstrun<br/>set /p fspath=Please type the path to your FS installation:<br/>echo set fspath=%fspath%&gt; myfile.ini<br/>copy myfile.ini temp$$$.bat<br/>call temp$$$.bat<br/>del temp$$$.bat<br/>goto menu1<br/><br/>The problem is that typically the path to the user's FS installation is "[drive]:\Program Files\Microsoft Games\Flight Simulator <strong>9</strong>" and because of the "9" at the end of the directory name, nothing at all gets written to the myfile.ini. Any other directory name seems to work fine.<br/><br/>I need some way "escape" the 9 in %fspath%, but short of instructing the user to insert the caret character before the 9 while typing the path, I'm drawing a blank. Any suggestions?<br/><br/>Thanks,<br/>JimTry:<br/><br/>echo 9 &gt; myfile.txt<br/><br/>note the space between 9 and &gt;Quote from: Dusty on October 04, 2007, 12:49:09 AM</p><blockquote>Try:<br/><br/>echo 9 &gt; myfile.txt<br/><br/>note the space between 9 and &gt;<br/></blockquote><br/>This will (probably) echo the trailing space too.<br/>Try this<br/>&gt;myfile.txt echo 9<br/><br/><br/>GrahamYES!! That's exactly what I needed, Graham! Thank you so much. I had no idea you could do that. That actually solved another problem I had too. This batch stores settings "profiles" for starting Flight Simulator and the user is prompted to give each profile a name. My only hope before this was that no one ever chose a name like "Profile 4" because obviously the same problem rears it's ugly <a href="https://interviewquestions.tuteehub.com/tag/head-247370" style="font-weight:bold;" target="_blank" title="Click to know more about HEAD">HEAD</a> when trying to store the name to the .ini file. Now I don't have to worry about, they can name it whatever they want.<br/><br/>Went through and changed 48 occurances in this 1200+ line batch file and everything works perfectly now. Again, a very sincere thank you.<br/><br/>Dusty,<br/>Graham's correct in that the space between the 9 and the &gt; <a href="https://interviewquestions.tuteehub.com/tag/caused-2502957" style="font-weight:bold;" target="_blank" title="Click to know more about CAUSED">CAUSED</a> the trailing space to be echoed to the .ini file. Oddly enough it still worked throughout the rest of the batch file even though it was effectively calling up the program with "...Flight Simulator 9 \fs9.exe" (note the space). The problem was when the user names or renames a profile the .ini gets completely re-written and echo %fspath% &gt; myfile.ini adds a trailing space each time the variable is echoed so before long I wound up with 10 or 20 trailing spaces after the 9.<br/><br/>Thank you both for your replies, I really appreciate it.<br/><br/>Jim</body></html>


Discussion

No Comment Found