-1

I need to write a decimal value assigned to environment variable finalval into a text file as such:

echo.%%finalval>>^C:\TempSetup\id.txt

The problem is that after execution when I go to check the file content the actual value of finalval isn't written into the file, instead it's just written %finalval%.
In previous usages in the same batch file of the same style of echo command, I've had no problems.

I have enabled delayedexpansion both in the first line as well as the loop I am using to write into the file.

What am I doing wrong?

Mofi
  • 38,783
  • 14
  • 62
  • 115
Gecko
  • 11
  • 1
  • Does this answer your question? [Batch File: Output variable to text file](https://stackoverflow.com/questions/25472541/batch-file-output-variable-to-text-file) – T3RR0R Jan 15 '20 at 06:22
  • The correct line for this task is: `>>"C:\TempSetup\id.txt" echo(%finalval%` The redirection is put first on the command line to be able to output also values like `1`, `2`, etc. For a detailed explanation see my answer on [Why does ECHO command print some extra trailing space into the file?](https://stackoverflow.com/a/46972524/3074564) The round bracket between command `echo` and the output value makes it possible to get an empty line written into the file if the variable `finalval` is not defined at all instead of writing the echo state into the file. – Mofi Jan 15 '20 at 07:22
  • Delayed environment variable expansion is only needed if the environment variable `finalval` holds a string which could contain characters having a special meaning for the Windows command processor `cmd.exe` processing the batch file as described by [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/) In this case it would be really necessary to enable [delayed expansion](https://ss64.com/nt/delayedexpansion.html) and use it with `echo(!finalval!>>"C:\TempSetup\id.txt"`. It is safe with delayed expansion to append the redirection. – Mofi Jan 15 '20 at 07:27
  • @Mofi thank you very much for your help, it's appreciated – Gecko Jan 15 '20 at 08:09

2 Answers2

0

There is a method which appears to be little used, but works well when you're certain that your echo string does not begin with a character which is escapable. The method is to escape using the standard escape character, (the caret, ^):

@For %%A In (0 1 2 3.0 4.1 5.2 10 21 32 String0 "String 1" "0 1 2")Do @Echo(^%%~A>>CON

I have used CON in the above code to redirect to the console device, you can obviously change that to a file for your specific task after testing.

The output should look something like this:

0
1
2
3.0
4.1
5.2
10
21
32
String0
String 1
0 1 2

As a result of jeb's comment, just to clarify, the idea can be more simply shown using:

@Echo(^%%finalval>>"C:\TempSetup\id.txt"

Where %%finalval is used to represent, (should be replaced by), the reported variable containing the 'decimal' value.

Compo
  • 30,301
  • 4
  • 20
  • 32
  • It works, .. but it also works without the caret, because the `%%~A` is expanded when the redirection is already parsed. I suppose you wanted to show that something like `echo Helllo ^2>con` works (showing `Hello 2`) – jeb Jan 15 '20 at 14:07
  • Yes @jeb, but I wanted to keep the answer as generic as possible, _as the OP didn't provide any detail only a single snippet which only looked like it 'may' have been `do` output_. I just figured that the OP would understand that `%%~A` is merely a representation of their `%%finalval`, _and the rest is technically irrelevant to their specific issue_. – Compo Jan 15 '20 at 14:15
0

You shouldn't need to enableDelayedExpansion, if you're not expanding the variable later in your script. If you've already set "finalval" earlier:

set finalval=someNumber

This is how you call it later:

echo.%finalval% >>C:\TempSetup\id.txt

Note: When you're redirecting the output to "id.txt", a single carat > will create the file. Double carats >> will append the file. Meaning, if you continue to run the script, and you're expecting a single value of "X" to be in "id.txt", since you're using double carats, your output will reflect "X" as many times as you've ran the script.

TrahanL
  • 19
  • 3