1

I am new in scripting and I'm working with a tool which is exactly like windows cmd. My input text file is :

age=min;age_1D_param_min_64;meas_time =

My batch file reads the lines from this input and splits the lines by “ ; ” then executes a function with the token and age file from the second field and after all parses the output for the third line. in the following you can see my current batch file:

setlocal EnableDelayedExpansion
for /f "tokens=1,2,3* delims=;" %%a in (input.txt) do (
REM create token file
echo.%%a>current.tok

sinoparam -p D:\product\%%b 0x0100001F current.tok> out.txt

for /f %%y in ('findstr /C:"%%c" out.txt ^| sed "s/.*%%c .............. )do SET RESULT=%%y

echo.%%a;%%b;%%c;!RESULT!>>finaloutput.csv
)
GOTO :EOF

Now I have problem with one string in out.txt which is the result of executing my function:

meas_time =31.9999

In my batch file I want to do the following:

  1. Find the value in string dActual_age =31.9999 by findstr/C

  2. If the value is lower than 1000 round it and show the result.

  3. If it's greater than 1000 first divide it by 32 and then round the result.

2 Answers 2

0

Math in batch files are generally performed with set /A, and set doesn't supply a way to round numbers; it also only handles integer math.

You'll need to find a command-line tool that can do the math you want from the batch script, or switch to a more capable scripting language (for example: PowerShell - Round down to nearest whole number, or VBScript).

0
  • Command SET /A can perform division and always rounds down (floor()).
    set /A calculation=10 / 3
    echo %calculation%
    REM will print "3"
  • You can also use variables in the calculations
    set dividend=10
    set divisor=3
    set /A calculation=%dividend% / %divisor%
    echo %calculation%
    REM will print "3"
  • Due to rounding, you must remember to first multiply and then divide
    set /A percentage=%current% / %max% * 100
    echo %percenage%%
    REM will print 0% (unless over 100%)

    set /A percentage=%current% * 100 / %max%
    echo %percenage%%
    REM will print xx%
  • to round up (ceil()) you need to increment the left value by the right value with one removed (e.g. ceil(10 / 3) == floor((10 + (3 - 1)) / 3)
    set dividend=10
    set divisor=3
    set /A calculation=(%dividend% + %divisor% - 1) / %divisor%
    echo %calculation%
    REM will print "4"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .