简体   繁体   English

如果在.bat文件中

[英]If in .bat file

@echo off
if "%1"=="" (
:WriteAgain
set x= 
set /p variables=Write your expression 
if "%variables%"=="help" (
echo Use arithmetical operations and numbers without spaces. + for sum, * for multiplication, / for 

devision, - for subtraction 
exit
) else (
set variables=%variables: =%
set /a x=%Variables% 2>Error.txt
if %errorlevel% neq 0 goto ErrorOccured
echo %x% 
pause
exit
)
:ErrorOccured
echo Your expression is not valid 
goto WriteAgain
) else (
set variables=%*
set variables=%variables: =%
set /a x=%variables% 2>Error.txt
if %errorlevel% neq 0 goto ErrorOccured
echo %x%
)

Greetings, It is a simple calc. 问候,这是一个简单的计算。 It could be used right from cmd but first if doesn't work also it can't pass 可以直接从cmd使用它,但是首先, if它不起作用,也无法通过

) else (
set variables=%variables: =%
set /a x=%Variables% 2>Error.txt
if %errorlevel% neq 0 goto ErrorOccured
echo %x%

always ErrorOccured . 始终为ErrorOccured I don't see any problems and it worked without first if 我看不到任何问题, if没有

Perhaps next code could help. 也许下一个代码可能会有所帮助。 Note EnableDelayedExpansion and proper quoting to allow all arithmetic operations seen in set /? 注意EnableDelayedExpansion和正确的引用以允许在set /?看到的所有算术运算set /? .

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
if "%~1"=="" goto NeedHelp
set "variables=%*"

:CommonCalc
if "%variables%"=="" goto ErrorOccured
rem spaces do not matter set "variables=!variables: =!"
set /a "x=!variables!" 2>Error.txt
if %errorlevel% neq 0 goto ErrorOccured
echo !variables!=%x%
exit /B

:WriteAgain
set "x="
set "variables="
set /p "variables=Write your expression: "
if /I "!variables!"=="" goto NeedHelp 
goto CommonCalc

:NeedHelp
echo Use arithmetical operations and numbers without spaces. 
echo + for sum, * for multiplication, / for division, - for subtraction
echo %% for modulus ^(remainder after division^)
goto WriteAgain

:ErrorOccured
echo Your "!variables!" expression is not valid ^(%errorlevel%^)
rem next line: clear errorlevel to 0
(call )
goto NeedHelp

However, proper escaping some operators is needful if used as batch line parameters . 但是, 如果用作批生产线参数,则需要适当地转义某些运算符。 For instance: 例如:

  • negation 32766441.bat ^^^!0 returns !0=1 , 否定32766441.bat ^^^!0返回!0=1
  • logical or 32766441.bat 6^|9 returns 6|9=15 , 逻辑 32766441.bat 6^|9返回6|9=15
  • left shift 32766441.bat 5^<^<3 returns 5<<3=40 etc. 移位 32766441.bat 5^<^<3返回5<<3=40等。

The (call ) trick to clear errorlevel by virtue of this dbenham's answer 借助此dbenham的答案来消除错误errorlevel(call )技巧

I do not know if it fits your expectations and I was not able to run the ErrorOccured label. 我不知道它是否符合您的期望,并且我无法运行ErrorOccured标签。

@echo off
cls
set x= 

:WriteAgain
if "%1"=="" (
    set /p "variables=Write your expression " || cls &goto help
    ) else (
    set "variables=%*"
)

set variables=%variables: =%
set /a x=%variables% 2>Error.txt
:: if %errorlevel% neq 0 goto ErrorOccured
if not ERRORLEVEL 0 goto ErrorOccured
call :result %variables% %x%
exit /b 0

:help
echo(
echo Use arithmetical operations and numbers without spaces.
echo + for sum, * for multiplication, / for devision, - for subtraction
goto WriteAgain

:result
echo formula is %~1 and the result is %~2
goto:EOF

:ErrorOccured
cls
echo(
echo Your expression is not valid 
goto :help

exit /b 0

This must certainly be optimized. 当然必须对此进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM