0

I have the following code to create a file if it does not exist, creating a variable (hi), echo the variable into the file, and then read the text as a variable. If it does exist, it simply reads the text as a variable:

if exist hiscore.txt (
for /f "delims=" %%x in (hiscore.txt) do set /a hi=%%x
) else (
set /a hi=0
echo %hi%>"hiscore.txt"
for /f "delims=" %%x in (hiscore.txt) do set /a hi=%%x
)

if I create the file manually, and type 0 into it manually, it works. If I delete the file, and then run this, it says "Missing Operand" and echos "ECHO is off" into the file. What can I change?

  • 1
    Does this answer your question? [How to echo "2" (no quotes) to a file, from a batch script?](https://stackoverflow.com/questions/7225630/how-to-echo-2-no-quotes-to-a-file-from-a-batch-script) Specifically, move `>"hiscore.txt"` to the start of the line so that it reads `>"hiscore.txt" echo %hi%` – SomethingDark Dec 07 '21 at 00:31
  • 2
    HOWEVER, since you're setting and using `%hi%` inside of the same set of parentheses, you *also* need to read https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set to add `setlocal enabledelayedexpansion` to your script and use `!hi!` instead of `%hi%`. – SomethingDark Dec 07 '21 at 00:33
  • 1
    Do not use `Set /A` to define your variable, use just `Set`. `Set /A` is for performing arithmetic, but you aren't performing any. The thing you should always try to remember is that all variables are string types, there is no such thing as an integer type, so do not think that using `Set /A` is magically defining an integer type. You should therefore use `Set "hi=%%x"` in your code above. – Compo Dec 07 '21 at 00:58
  • @Compo I use /a because it removes extra spaces, whether the characters are numbers or letters – WeeTomatoBall Dec 07 '21 at 01:21
  • There should not be any extra spaces, @WeeTomatoBall. You are supposed to `echo` it to the file like this, `(echo(%hi%) 1>"hiscore.txt"`, like this `1>"highscore.txt" echo(%hi%`, or like this `If Defined hi Set /P "=%hi%" 1>"hiscore.txt"`. In each of those cases there will never be any unwanted spaces, unless you cretaed that variable incorrectly in the first place, e.g. `echo %hi% > hiscore.txt`, which if you'd read my comment in your last question, you'd be aware of by now. – Compo Dec 07 '21 at 01:52
  • @Gerhard, although I like your approach it is not complete as it will not create file `hiscore.txt` with content `0` when it does not exist, unless you run it directly in Command Prompt… – aschipfl Dec 07 '21 at 07:38
  • @aschipfl, yeah, sometimes I forget that this was a `batch-file` question and not `cmd`.. Doh! Thanks. – Gerhard Dec 07 '21 at 08:08
  • 1
    @Gerhard, changing your (meanwhile deleted) approach to incorporate `cmd /D /C` could do it, something like this: `@(< "hiscore.txt" set /P HI="" & set /A "HI+=0") > nul 2>&1 || cmd /D /C set /A "HI" > "hiscore.txt"` – aschipfl Dec 07 '21 at 09:10

1 Answers1

0

Here's a quick example which may help you:

@Echo Off

Set "hi=0"
Set /P "hi=" 0<"hiscore.txt" || (
    (Echo Creating and propagating file . . .) 1>CON
    Echo %hi%) 1>"hiscore.txt"
Compo
  • 36,585
  • 5
  • 27
  • 39