0

I am new to batch. I am trying to echo this to txt, but I am getting: ECHO is off. in cmd.

SET /a _rand=(%RANDOM%*5/32768)+1
Set /a zmienna=%zmienna%-%_rand%
set kurczaki=%zmienna%
echo %kurczaki%> bin\DB\smth.txt

I tried to change "zmienna" into other variable, but I have still same problem. Thank you in advance for your help.

Skrampy
  • 29
  • 6
  • *N. B.:* There is no need to define a variable (`kurczaki`) to just store the value of another one (`zmienna`). You do not need to enclose variables in `%%` within `set /A`. Then the second line can even be reduced to `set /A zmienna-=_rand`… – aschipfl Jun 03 '20 at 11:47

2 Answers2

0

You get the ECHO is off/on message if you just run echo with no operands. That means, for some reason, your kurczaki variable is not being set to anything. Hence you should echo each variable after you set it to see where the problem lies:

SET /a _rand=(%RANDOM%*5/32768)+1
echo .%_rand%.
Set /a zmienna=%zmienna%-%_rand%
echo .%zmienna%.
set kurczaki=%zmienna%
echo .%kurczaki%.
echo %kurczaki%> bin\DB\smth.txt

When I do this from the command line, there's a difference when the > redirection is hard up against what you're outputting, when that thing can be used as a file handle:

C:\Pax> set xx=7
C:\Pax> echo %xx%>zz.txt
ECHO is on.
C:\Pax> type zz.txt
<no output>
C:\Pax> echo %xx% >zz.txt
C:\Pax> type zz.txt
7<space>

So I think your solution is just to not do that. What is happening in this case is that the echo %xx%>zz.txt is being converted exactly into:

echo 7>zz.txt

which is not sending the 7 to the file. Instead, it is attaching file handle 7 to that file and then, because your echo is only writing to file handle 1 (standard output), nothing appears in the file.

In addition, because the %xx% has been absorbed for the redirection, there are no longer any arguments to echo and you therefore get the ECHO is off message. Further proof can be found with:

C:\Pax> set xx=1 7
C:\Pax> echo %xx%
1 7
C:\Pax> echo %xx%>zz.txt
1

with the 7 being absorbed for the redirection, leaving only the 1.

But beware that spaces often find their way into output if you don't do it in a certain way. To that end, I would change the line to be:

echo>bin\DB\smth.txt %kurczaki%

This should both ensure no spaces surrounding the variable in the output file, and also the actual writing of the value to said file.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Okay, I am not using `kurczaki` anymore. I am using only `zmienna`. If I echo `zmienna`, then i am getting normal output, like `8`, etc. But when I am trying to output it to txt file, I am still getting: `ECHO is off.` – Skrampy Jun 03 '20 at 10:44
  • @Skrampy although this will not solve your problem, you should always use `ECHO(` instead of `ECHO` EXCEPT `echo off` or `echo on` – ScriptKidd Jun 03 '20 at 10:50
  • btw, why not `set/a"zmienna-=_rand"`? – ScriptKidd Jun 03 '20 at 10:52
  • like I said, I am new into batch. I bet, that I could write whole code better, but I am starting to use .bat :)) – Skrampy Jun 03 '20 at 10:54
0

Your issue is probably caused when your result can also be confused as a redirection handle, i.e. (1>, 2> etc.). This can be easily fixed by the way you echo it.

You could also simplify your four lines to just two:

Set /A kurczaki=zmienna - ((%RANDOM% %% 5) + 1)
(Echo %kurczaki%) > bin\DB\smth.txt

or

Set /A kurczaki=zmienna - ((%RANDOM% %% 5) + 1)
> bin\DB\smth.txt Echo %kurczaki% 
Compo
  • 36,585
  • 5
  • 27
  • 39