1.

Solve : Echoing variables into text documents?

Answer» <html><body><p>For some reason, when I run any code along these lines:<br/><br/> Code: <a>[Select]</a>SET Variable=Hello<br/>ECHO %Variable%&gt;filename.txt<br/>It creates the file, but what is echoed into the file is just "ECHO is off" or "ECHO is on" depending on whether echoing is on or off.<br/><br/>Does anybody know why this is?<br/><br/>EDIT: This is my real code, and I've modified it from when this topic was posted. Now nothing is echoed into the file, and it's contents are left <a href="https://interviewquestions.tuteehub.com/tag/blank-899028" style="font-weight:bold;" target="_blank" title="Click to know more about BLANK">BLANK</a>.<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/><br/>IF EXIST NextFile.txt GOTO Continue<br/>SET FileNumber=1<br/><br/>:Start<br/><br/>Set /p Input=""<br/>ECHO %Input%&gt;%FileNumber%.txt<br/>SET /a FileNumber +=1<br/>ECHO %FileNumber%&gt;NextFile.txt<br/>GOTO Start<br/><br/>:Continue<br/><br/>SET /p FileNumber= &lt;NextFile.txt<br/>GOTO Start Code: <a>[Select]</a>S:\&gt;SET Variable=Hello<br/><br/>S:\&gt;ECHO %Variable%&gt;filename.txt<br/><br/>S:\&gt;type filename.txt<br/>Hello<br/><br/><br/>Possibly you have left something out of what you are telling us. "ECHO is off/on" is what you get if you try to echo an undefined variable. For example if you spell it differently in the SET line and the ECHO line. Please give an <a href="https://interviewquestions.tuteehub.com/tag/actual-361632" style="font-weight:bold;" target="_blank" title="Click to know more about ACTUAL">ACTUAL</a> example of some code that does not do what you expected.<br/><br/><br/><br/>Thanks for the <a href="https://interviewquestions.tuteehub.com/tag/reply-1185278" style="font-weight:bold;" target="_blank" title="Click to know more about REPLY">REPLY</a> Salmon Trout,<br/><br/>I've changed my code a little bit, and now it just doesn't echo anything into the file.<br/><br/>What I'm was trying to accomplish was so achieve a checkpoint kind of thing so the batch file would pick up where it left off. What I did was have it echo the last file number created into a file, then next <a href="https://interviewquestions.tuteehub.com/tag/time-19467" style="font-weight:bold;" target="_blank" title="Click to know more about TIME">TIME</a> it runs, if it exists, echo user input into filenumber.txt<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/><br/>IF EXIST NextFile.txt GOTO Continue<br/>SET FileNumber=1<br/><br/>:Start<br/><br/>Set /p Input=""<br/>ECHO %Input%&gt;%FileNumber%.txt<br/>SET /a FileNumber +=1<br/>ECHO %FileNumber%&gt;NextFile.txt<br/>GOTO Start<br/><br/>:Continue<br/><br/>SET /p FileNumber= &lt;NextFile.txt<br/>GOTO Start<br/>The problem I'm having is that %FileNumber% isn't being echoed into NextFile.txt<br/>Now it just created the file, and leaves what is in it blank.<br/><br/>Any suggestions?if the file exists the IF EXIST test will jump to the label :Continue and the variable will be undefined.<br/>But it sets FileNumber from NextFile.txt (SET /p FileNumber= <br/>That is not the problem I am having though. The batch file isn't echoing %FileNumber% into NextFile.txt.<br/><br/>If I manually enter a number into NextFile.txt, it works<br/> Code: <a>[Select]</a>ECHO OFF<br/>echo  nextfile.txt<br/>type nextfile.txt<br/>pause<br/><br/>IF EXIST NextFile.txt GOTO Continue<br/>SET FileNumber=1<br/><br/>:Start<br/><br/>Set /p Input=""<br/>ECHO %Input%&gt; %FileNumber%.txt<br/>SET /a FileNumber +=1<br/>ECHO %FileNumber%&gt;NextFile.txt<br/>echo  nextfile.txt<br/>type nextfile.txt<br/><br/>GOTO Start<br/><br/>:Continue<br/><br/>SET /p FileNumber=&lt;NextFile.txt<br/>echo  nextfile.txt<br/>type nextfile.txt<br/>GOTO Start<br/>paw.bat<br/> nextfile.txt<br/>18<br/>Press any key to continue . . .<br/> nextfile.txt<br/>18<br/><br/> nextfile.txt<br/>19<br/><br/> nextfile.txt<br/>20<br/><br/> nextfile.txt<br/>21<br/><br/> nextfile.txt<br/>22<br/><br/> nextfile.txt<br/>23<br/>^CTerminate batch job (Y/N)? y<br/><br/>C:\test&gt;On Windows 7 I got it to work by escaping %Filenumber% with a caret<br/><br/>ECHO ^%FileNumber%&gt;NextFile.txtahh, thank you Salmon. That worked perfectly. =)SET /p FileNumber=echo Filenumber=%Filenumber%<br/><strong>set newnumber= &lt;NextFile.txt</strong><br/>echo newnumber=%newnumber%<br/>pause<br/><br/><strong>Allow no white space around "=" when the text line is assigned to the variable</strong><br/><br/><br/><br/>paw2.bat<br/> nextfile.txt<br/>25<br/>Press any key to continue . . .<br/>Filenumber=25<br/><strong>newnumber=</strong><br/>Press any key to continue . . .<br/><br/><br/>paw2.bat<br/> nextfile.txt<br/>25<br/>Press any key to continue . . .<br/>Filenumber=25<br/><strong>newnumber=</strong><br/>Press any key to continue . . .MattPwns, your original problem arose because:<br/><br/>The command console has two numbered output streams, STDOUT (1) and <a href="https://interviewquestions.tuteehub.com/tag/stderr-1226675" style="font-weight:bold;" target="_blank" title="Click to know more about STDERR">STDERR</a> (2) stream 1 is the normal screen output of most commands and the default stream used by batch files. Stream 2 is the error stream and is used by some programs and commands to output their error messages to the screen.<br/><br/>These numbers can be used with &gt; and &gt;&gt; to redirect the streams. We place the number immediately before the redirection symbol like this 1&gt; 2&gt;<br/>When you do this (echo to the default stream, STDOUT)<br/><br/>echo %variable% &gt; filename<br/><br/>you are really doing this<br/><br/>echo %variable% 1&gt; filename<br/><br/>This happens with all single digit numbers<br/><br/>So when you do this when filenumber equals 1<br/><br/>ECHO %FileNumber%&gt;NextFile.txt<br/><br/>you are really doing this<br/><br/>ECHO 1&gt;NextFile.txt<br/><br/>That is echoing [nothing] to NextFile.txt<br/><br/>When you use ECHO with no parameter it announces the ECHO state (ECHO is on or ECHO is off) <br/><br/>We can escape special characters so they are processed literally, mostly with a caret which is why the caret trick works but it is better to:<br/><br/><br/><strong>EITHER</strong><br/><br/><strong>always put a space before the redirection character</strong><br/><br/> Code: <a>[Select]</a>ECHO %FileNumber% &gt;NextFile.txt<br/><strong>OR</strong><br/><br/><strong>Use the other (transposed) redirection syntax</strong> (some people use this method always)<br/><br/> Code: <a>[Select]</a>&gt;NextFile.txt ECHO %FileNumber%<br/>One thing you should learn from this thread is that in cmd (batch) scripting, spaces matter sometimes in ways that you may not expect.<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>Ohh, thank you Salmon. I completely understand now.<br/><br/>I did however try<br/><br/> Code: <a>[Select]</a>&gt;filename.txt ECHO Text<br/>But for some reason It wouldn't work for me. This is excellent though, thanks a bunch. Quote from: MattPwns on July 26, 2010, 03:35:48 PM</p><blockquote>Ohh, thank you Salmon. I completely understand now.<br/><br/>I did however try<br/><br/> Code: <a>[Select]</a>&gt;filename.txt ECHO Text<br/>But for some reason It wouldn't work for me. This is excellent though, thanks a bunch.<br/></blockquote> <br/><br/>(1) command prompt<br/> Code: <a>[Select]</a>S:\&gt;&gt;test.txt echo Rasputin<br/><br/>S:\&gt;type test.txt<br/>Rasputin<br/><br/>S:\&gt;<br/><br/>(2) batch file<br/><br/> Code: <a>[Select]</a>echo off<br/>&gt;test.txt echo Rasputin<br/><br/>test.txt...<br/><br/> Code: <a>[Select]</a>Rasputin<br/>Hmm.. It is working, I must have been doing something wrong. Anyways, thanks again for all your help.</body></html>


Discussion

No Comment Found