0

I want to write variables to multiple text file using batch, but it doesn't work

Set b=5
Set c=13
For /l %%a in (1, 1,100) do (
    Echo title
    Echo lambda 1.5406
    Echo cell %b% %c% 90.00 100.00
    Echo end of path
    b=%b% + 1
    c=%c% + 1
)> path%%a.txt

Plz help

  • Before you go any further, I would ask you to open up a Command Prompt window, enter `set /?`, then read and understand everything it outputs. You may find that using the correct syntax would help you, or subsequently realize that the `set` command will not be able to achieve the actions you require of it. – Compo Feb 21 '20 at 12:55
  • Thank you! I will. As far as I know syntax I used is correct. The problem is that I cannot append the values of these variables to the .txt file. The sixth line just ouptuts "cell 90.00 100.00" – Touseef Para Feb 21 '20 at 13:06
  • To clarify, the syntax you have used is incorrect, this line is wrong `Set b = 5`, as is this, `Set c =13.1` and this, `b = %b% + 0.1`, and this `c = %c% + 0.1` and also this `)> path%%a.txt`. Regardless of those being fixed, floating point math cannot be performed using `set` even with its `/A` option, _without often complex workarounds_. Please, as I've already commented, open up a Command Prompt window and take a look at the usage information for your chosen command! – Compo Feb 21 '20 at 13:23
  • Yes. That is correct. While typing the question here I did type this code on mobile device so there are a lot of errors – Touseef Para Feb 21 '20 at 16:43

2 Answers2

1
  1. In batch, one should always avoid whitespace, not to mention that you used whitespace the wrong way:
    Set b = 5 is setting %b % to  5

  2. The calculation of float is not supported in batch. To get around with it:

    • Pass to powershell and redirect the output to your file
    • Use my method below
  3. DelayedExpansion should be enabled.

Your script should look like:

@echo off
====SETLOCAL EnableDelayedExpansion
set/a"b=50, c=131"


for /L %%N in (1,1,100) do (
    echo(title
    echo(lambda 1.5406
    echo(cell !b:~0,-1!.!b:~-1! !c:~0,-1!.!c:~-1! 90.00 100.00
    echo(end of path
    set/a"b+=1, c+=1"
)>"path%%N.txt"

Shortened using the newline hack:

@echo off
====SETLOCAL EnableDelayedExpansion

( set LF=^
%= EMPTY =%
)
set/a"b=50, c=131"

for /L %%N in (1,1,100) do (
    echo(title!LF!lambda 1.5406!LF!cell !b:~0,-1!.!b:~-1! !c:~0,-1!.!c:~-1! 90.00 100.00!LF!end of path
    set/a"b+=1,c+=1"
)>"path%%N.txt"
ScriptKidd
  • 803
  • 1
  • 5
  • 19
  • This answer only addresses a few of the problems with the script in question. While you did address that batch-files cannot do floating point math, you did not address the other syntax issues with those two lines of code. Nor did you address the issues with the usage of `STDOUT`. – Squashman Feb 21 '20 at 14:08
0

It would seem unlikely that you will come to a successful result using cmd.exe batch script programming. If you are on a supported Windows system, PowerShell will be available.

=== donumbers.ps1

$b = 5
$c = 13.1

(1..100) |
    ForEach-Object {
        "title`nlambda 1.5406`ncell {0} {1} 90.00 100.00" -f @($b, $c) |
            Out-File -FilePath "C:/src/t/path$_.txt" -Encoding ascii
        $b = $b + 0.1
        $c = $c + 0.1
    }

Run it using:

powershell -NoLogo -NoProfile -File .\donumbers.ps1
lit
  • 14,456
  • 10
  • 65
  • 119
  • If I had to get hundred values of b for every value of c, do I use nested foreach or anything else – Touseef Para Feb 21 '20 at 16:38
  • @TouseefPara - It is not clear to me exactly what you are trying to describe. But, yes, you can used nested ForEach-Object or foreach loops. – lit Feb 21 '20 at 16:50
  • I want to get values like this 5.1 13 5.2 13 5.3 13......5.1 13.1 5.2 13.1......5.1 13.2 5.2 13.2....... – Touseef Para Feb 21 '20 at 17:08
  • @TouseefPara - Please try to modify the code to do what you want. If you cannot, please post your code in a new question. Be sure to describe the number of files and their content. This code produces 100 files. It sounds like might be asking for 100 * 100 (10,000) files. – lit Feb 21 '20 at 17:17