0

All, I was provided the below code as a replacement to part of a batch file which performed some functions and then called powershell to perform arithmetic against two variables. The script outputs the result to the cmd window, but I am trying to figure out how to output to a txt file. I've tried something as simple as > output.txt but that's not working. Any assistance is appreciated.

setlocal EnableDelayedExpansion

( set /P "num1=" & set /P "num2=" ) < test.txt

rem Adjust *two* numbers for given decimals
set "decimals=2"
for %%i in (1 2) do (
   set "num%%i=!num%%i:.=!"
   for /L %%d in (1,1,%decimals%) do if "!num%%i:~0,1!" equ "0" set "num%%i=!num%%i:~1!"
)

set /A "result=num1 - num2"
for /L %%d in (1,1,%decimals%) do if "!result:~%decimals%!" equ "" set "result=0!result!"

echo !result:~0,-%decimals%!.!result:~-%decimals%!
sbagnato
  • 603
  • 3
  • 11
  • 35
  • 1
    Maybe you should specify where you tried to put the output redirection. – Squashman Jul 21 '20 at 21:03
  • Replace `echo..` with `!result:~0,-%decimals%!.!result:~-%decimals%! > test2.txt` – sbagnato Jul 21 '20 at 21:47
  • What was wrong with [using `PowerShell`](https://stackoverflow.com/q/62983420) to subtract `%num2%` from `%num1%`? – Compo Jul 21 '20 at 22:21
  • @Compo I couldn't figure out how to successfully round the result to two decimal places. – sbagnato Jul 21 '20 at 23:20
  • 2
    @sbagnato, I don't use Powershell but I literally could not believe how easy it was too do when I Google searched on how to do it. `[math]::Round($var,2)` – Squashman Jul 22 '20 at 01:16
  • 2
    @sbagnato, are you saying your removed the command `echo` from the line and tried to redirect the output to the text file? Every line in a batch file has to start with some command to execute it. So you need to use echo. – Squashman Jul 22 '20 at 01:19
  • @Squashman I know, I found that too but could not get it to work right. Which is why I posted this question. However, Since I already went away from Powershell and went with the above, using your suggestion of adding 'echo' back in worked. I appreciate your help. – sbagnato Jul 22 '20 at 12:47

1 Answers1

0

As you know that your MySQL result will always return to two decimal places, can you not just do it in three simple commands? Remove those decimal points, perform the subtraction, then add the decimal point to the result.

(Set /P "num1=" & Set /P "num2=") 0< "test.txt"
Set /A res=%num1:.=%-%num2:.=%
Set "res=%res:~,-2%.%res:~-2%"

However it is possible that the numbers you are getting in your variables are actually in the format of ######.##, i.e returned with six whole units and two decimal units. If that is the case, then it is probable that they will have leading 0's, which will no doubt create issues with the Set /A arithmetic.

In that scenario, you could prepend each with a non zero integer, and perform the same function as above.

(Set /P "num1=" & Set /P "num2=") 0< "test.txt"
Set /A res=1%num1:.=%-1%num2:.=%
Set "res=%res:~,-2%.%res:~-2%"
Compo
  • 36,585
  • 5
  • 27
  • 39