带有负变量的 If 语句

带有负变量的 If 语句

我的输入变量仅包含负值,-5.0、-10.0……这将通过 IF 语句进行检查。当变量小于 -5.0 时,例如 -4.9、-3.0……将发送无效值消息。

为了避免涉及负号的比较,我计算了变量的字符数以获取不带符号的变量值,如下所示:

我使用此链接来使用变量中的字符数:
计算具有负值的变量字符数

第一个变量:

set /p _vLUF=LUFS value: -5.0
echo vLUF: %vLUF%
vLUF: -5.0

第二个变量:

set /p _vLUF=LUFS value: -10.0
echo vLUF: %vLUF%
vLUF: -10.0

计算变量的字符数:

setlocal enabledelayedexpansion
for %%i in (v)do set _cnt_%%~i=<nul
for /f delims^=^ eol^= %%G in ('"cmd.exe /u /c echo=!_vLUF!|find/v """')do set /a "_cnt_vLUF+=1+0"

First variable: has 4 characters
Second variable: has 5 characters

从变量中删除符号:

If !_cnt_vLUF!==4 (
 set positive=%_vLUF:~1.3%
)ELSE (
 set positive=%_vLUF:~1.4%
)

echo first variable: !positive!
first variable: 5.0

echo second variable: !positive!
second variable: 10.0

如果语句发送警告信息:

If "!positive!" lss "5.0" (
  SETLOCAL EnableExtensions DisableDelayedExpansion
  echo %ESC%[31mInvalid LUFS Value! Valid values:%ESC%[32m
  -70.0 to -5.0%ESC%[0m%ESC%[31m.%ESC%[0m%ESC%
  [31mReturn? (y=yes/enter=exit^) %ESC%[0m
  SETLOCAL EnableDelayedExpansion
  set /p r=""
  IF /i "!r!"=="s" goto SETLUFSPEAK
  goto notfound
)
endlocal

只要变量的值在 0.0 到 9.9 之间,If 语句就能正常工作,例如:

3.0 invalid value message
4.9 Invalid value message
5.0 process without error message
9.9 process without error message

但是当值达到 10.0 时,IF 语句会认为值 10.0 小于 5.0,并在应该处理该记录时发出错误消息。

答案1

一些基本使用子字符串的建议

@echo off 

Setlocal EnableDelayedExpansion

for %%i in ($E,$H)do for /f skip^=4 %%$ in ('
     echo;prompt;%%~i^|cmd')do set "_%%~i=%%$"

set /p "'=!_$E![0m" <nul & cd /d "%~dp0"

%:^[
set "_var=LUF,len,pos+,int,dig,var"
for %%i in (%_var%)do call set "_%%i="

set /p "_LUF=-!_$H!  !_$E![1;4;32mValor LUFs:!_$e!" <nul 
set /p "_LUF=[0m !_$E![4;1;33m" || goto %:^(
set /p "'=!_$e![0;0m" <nul & color

=;(echo;!_LUF!|findstr /b \0.*
   echo;!_LUF!|findstr /e \.[0-9]
   echo;!_LUF!|findstr /b \-[0-9].*
   echo;!_LUF!|findstr /vr "[a-Z] \+" 
  );= >nul && (set "_pos+=!_LUF:-=!") || goto %:^(

set /a "_cnt=0, _len=0"
set "_int=!_pos+:.%_pos+:*.=%=!"
set "_=%_pos+:*.=" && set "_dig=%"

for /f tokens^=1*^ delims^=. %%i in ('
     echo;!_pos+!')do if 1%%~i%%~j gtr 1700 =;(
         goto %:^();= else if 1%%~i%%~j leq 149 goto %:^(

for /f tokens^=1^ delims^=^ eol^= %%x in =;('
    "cmd.exe /u /c echo;!_LUF!|find/v """
    ')do set /a "_len+=1+0"

set "_len=0!_len!" && call set "_len=!_len:~-2!"
echo\!_$E![0m & echo\!_$H!!_$E![1F!_$E![0J
goto %:^]

%:^(
echo\!_$E![1F!_$E![0J!_$E![0J && color

set /p "'=-!_$H!  !_$E![1;31mInvalid LUFs Value^! Valid values: " <nul 
set /p "'=!_$E![32m-70.0 to -5.0!_$E![1;31m!_$E![32m " <nul  
set /p "'=!_$E![1;33mTry Again?!_$E![7m!_$E![0m " <nul

call & choice 

if "%errorlevel%" == "1" =;(
    echo;!_$E![1F!_$E![0J!_$E![1F!_$E![0J!_$E![1F!_$E![0J & goto %:^[
   );= else endlocal && goto :eof 

%:^]
:: your next command became here... 
echo;!_$E![1F!_$E![0J
set /p "'=-!_$H!  !_$E![1;04m!_$E![1;32mInput vLUF: !_$E![33m!_LUF!!_$E![1;32m ^| " <nul 
set /p "'=!_$E![1;32mPositive: !_$E![33m!_pos+! !_$E![1;32m^| Length: " <nul 
set /p "'=!_$E![33m!_len! !_$E![1;32m^| Value: " <nul 
set /p "'=!_$E![33m!_int!!_dig!!_$E![0m " <nul
 
echo\ & echo;  Obs: I'm in label %%^:^]
endlocal & goto :eof

答案2

工作原理如下:

set /p _vLUF=Valor LUFS:
set _cnt_vLUF=0

我在计数器等于 4 的字符串开头插入字符“0”:

If !_cnt_vLUF!==4 (
 set positivo=%_vLUF:~1,3%
 set positivo=0!positivo!   # inserted the character "0" at the beginning string
)ELSE (
 set positivo=%_vLUF:~1,4%
)

并将固定比较值从“5.0”更改为“05.0”:

If "!positivo!" lss "05.0" (       # changed the fixed comparison value to "05.0" 
 SETLOCAL EnableExtensions DisableDelayedExpansion
 echo %ESC%[31mInvalid LUFS Value! Valid values:%ESC%[32m
 -70.0 to -5.0%ESC%[0m%ESC%[31m.%ESC%[0m%ESC%
 [31mReturn? (y=yes/enter=exit^) %ESC%[0m
 SETLOCAL EnableDelayedExpansion
 set /p r=""
 IF /i "!r!"=="s" goto SETLUFSPEAK 
 goto notfound
)
endlocal

因此 IF 指令可以对值进行正确的比较:

If "01.0" lss "05.0"; If "04.9" lss "05.0"; If "10.0" lss "05.0"...

相关内容