IF conditionnel batch files
Solved
ismail.bensikali
-
ismail.bensikali -
ismail.bensikali -
Hello,
I need to translate the user’s text into English, preserving meaning and tone, with no extra commentary.
Here is the translation:
Hello,
I have a set of files that contain values I need to retrieve and calculate the average from; the problem is that BATCh does not consider decimals, so everything after the comma becomes 0. But all my values are in the form 0.xxx. Also, these files are generated automatically and I cannot multiply them by 10^2. So I tried to retrieve the values after (0.). The problem is that I must first test the retrieved values before summing them: for example
0.1234 => 1234
0.9 => 9 (I must multiply by 1000 to be correct)
So if we sum directly the result will be non-tolerable! Therefore first we must test the values against 10, 100, and 1000.
Here is the test code part (hence the problems):
IF !val! LSS 10(
set /a val=!val!*1000
)
ELSE IF !val! LSS 100 (
set /a val= !val!*100
)
ELSE IF !val! LSS 1000 (
set /a val = !val!*10
)
echo !val!
pause
It has been almost three days that I try to get this code to work but really no results! I think the problem is in the if structure.
If you have ideas, don’t hesitate.
Thank you.
I need to translate the user’s text into English, preserving meaning and tone, with no extra commentary.
Here is the translation:
Hello,
I have a set of files that contain values I need to retrieve and calculate the average from; the problem is that BATCh does not consider decimals, so everything after the comma becomes 0. But all my values are in the form 0.xxx. Also, these files are generated automatically and I cannot multiply them by 10^2. So I tried to retrieve the values after (0.). The problem is that I must first test the retrieved values before summing them: for example
0.1234 => 1234
0.9 => 9 (I must multiply by 1000 to be correct)
So if we sum directly the result will be non-tolerable! Therefore first we must test the values against 10, 100, and 1000.
Here is the test code part (hence the problems):
IF !val! LSS 10(
set /a val=!val!*1000
)
ELSE IF !val! LSS 100 (
set /a val= !val!*100
)
ELSE IF !val! LSS 1000 (
set /a val = !val!*10
)
echo !val!
pause
It has been almost three days that I try to get this code to work but really no results! I think the problem is in the if structure.
If you have ideas, don’t hesitate.
Thank you.
3 answers
-
hello
put a space between 10 and (IF !val! LSS 10 (
-
hello dubcek ;
thanks for your response yes That's it the problem! indeed batch is very strict in the form of commands ; anyway here is what I did (I count 7 digits after the decimal)
FOR /F "tokens=1,2* delims=0." %%i in (xxx.cvs) do (
set /a val=%%j
echo !val!
rem iteration counter
set /a z=!z!+1
if !val!==0 (
rem count zero values
set /a x=!x!+1
) else if !val! LSS 10 (
set /a s=!val!*100000
)else if !val! LSS 100 (
set /a s=!val!*10000
)else if !val! LSS 1000 (
set /a s=!val!*1000
) else if !val! LSS 10000 (
set /a s=!val!*100
) else if !val! LSS 100000 (
set /a s=!val!*10
) else if !val! LSS 1000000 (
set /a s=!val!*1
) else (
set /a s=0
set /a x=!x!+1
)
set /a somme=!somme! + !s!
@echo !somme!
)
set /a d=!z!-!x!
rem d=nbr iterations-nbre val nulles
rem avg=moyenne
set /a avg=!somme!/d
Merci. -
Hi,
to be more correct we must replace:
FOR /F "tokens=1,2* delims=0." %%i in (xxx.cvs) do (
with:
FOR /F "tokens=1,2* delims=." %%i in (xxx.cvs) do (
because if you have values that contain 0 only the part before 0 will be retrieved, for example:
0.123456 => 1234
but
0.120456 => 12
Thank you.