1.

Solve : Echoing variables into text documents?

Answer»

For some reason, when I run any code along these lines:

Code: [Select]SET Variable=Hello
ECHO %Variable%>filename.txt
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.

Does anybody know why this is?

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 BLANK.

Code: [Select]ECHO OFF

IF EXIST NextFile.txt GOTO Continue
SET FileNumber=1

:Start

Set /p Input=""
ECHO %Input%>%FileNumber%.txt
SET /a FileNumber +=1
ECHO %FileNumber%>NextFile.txt
GOTO Start

:Continue

SET /p FileNumber= <NextFile.txt
GOTO Start Code: [Select]S:\>SET Variable=Hello

S:\>ECHO %Variable%>filename.txt

S:\>type filename.txt
Hello


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 ACTUAL example of some code that does not do what you expected.



Thanks for the REPLY Salmon Trout,

I've changed my code a little bit, and now it just doesn't echo anything into the file.

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 TIME it runs, if it exists, echo user input into filenumber.txt

Code: [Select]ECHO OFF

IF EXIST NextFile.txt GOTO Continue
SET FileNumber=1

:Start

Set /p Input=""
ECHO %Input%>%FileNumber%.txt
SET /a FileNumber +=1
ECHO %FileNumber%>NextFile.txt
GOTO Start

:Continue

SET /p FileNumber= <NextFile.txt
GOTO Start
The problem I'm having is that %FileNumber% isn't being echoed into NextFile.txt
Now it just created the file, and leaves what is in it blank.

Any suggestions?if the file exists the IF EXIST test will jump to the label :Continue and the variable will be undefined.
But it sets FileNumber from NextFile.txt (SET /p FileNumber=
That is not the problem I am having though. The batch file isn't echoing %FileNumber% into NextFile.txt.

If I manually enter a number into NextFile.txt, it works
Code: [Select]ECHO OFF
echo  nextfile.txt
type nextfile.txt
pause

IF EXIST NextFile.txt GOTO Continue
SET FileNumber=1

:Start

Set /p Input=""
ECHO %Input%> %FileNumber%.txt
SET /a FileNumber +=1
ECHO %FileNumber%>NextFile.txt
echo  nextfile.txt
type nextfile.txt

GOTO Start

:Continue

SET /p FileNumber=<NextFile.txt
echo  nextfile.txt
type nextfile.txt
GOTO Start
paw.bat
 nextfile.txt
18
Press any key to continue . . .
 nextfile.txt
18

 nextfile.txt
19

 nextfile.txt
20

 nextfile.txt
21

 nextfile.txt
22

 nextfile.txt
23
^CTerminate batch job (Y/N)? y

C:\test>On Windows 7 I got it to work by escaping %Filenumber% with a caret

ECHO ^%FileNumber%>NextFile.txtahh, thank you Salmon. That worked perfectly. =)SET /p FileNumber=echo Filenumber=%Filenumber%
set newnumber= <NextFile.txt
echo newnumber=%newnumber%
pause

Allow no white space around "=" when the text line is assigned to the variable



paw2.bat
 nextfile.txt
25
Press any key to continue . . .
Filenumber=25
newnumber=
Press any key to continue . . .


paw2.bat
 nextfile.txt
25
Press any key to continue . . .
Filenumber=25
newnumber=
Press any key to continue . . .MattPwns, your original problem arose because:

The command console has two numbered output streams, STDOUT (1) and STDERR (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.

These numbers can be used with > and >> to redirect the streams. We place the number immediately before the redirection symbol like this 1> 2>
When you do this (echo to the default stream, STDOUT)

echo %variable% > filename

you are really doing this

echo %variable% 1> filename

This happens with all single digit numbers

So when you do this when filenumber equals 1

ECHO %FileNumber%>NextFile.txt

you are really doing this

ECHO 1>NextFile.txt

That is echoing [nothing] to NextFile.txt

When you use ECHO with no parameter it announces the ECHO state (ECHO is on or ECHO is off)

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:


EITHER

always put a space before the redirection character

Code: [Select]ECHO %FileNumber% >NextFile.txt
OR

Use the other (transposed) redirection syntax (some people use this method always)

Code: [Select]>NextFile.txt ECHO %FileNumber%
One thing you should learn from this thread is that in cmd (batch) scripting, spaces matter sometimes in ways that you may not expect.










Ohh, thank you Salmon. I completely understand now.

I did however try

Code: [Select]>filename.txt ECHO Text
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

Ohh, thank you Salmon. I completely understand now.

I did however try

Code: [Select]>filename.txt ECHO Text
But for some reason It wouldn't work for me. This is excellent though, thanks a bunch.


(1) command prompt
Code: [Select]S:\>>test.txt echo Rasputin

S:\>type test.txt
Rasputin

S:\>

(2) batch file

Code: [Select]echo off
>test.txt echo Rasputin

test.txt...

Code: [Select]Rasputin
Hmm.. It is working, I must have been doing something wrong. Anyways, thanks again for all your help.


Discussion

No Comment Found