If statement 批处理文件中带有选项的Errorlevel执行错误案例
有人能告诉我如何使errorlevel停止取1或更大的值吗?我希望它只考虑确切的值。如果我选择2,我希望它选择第二个选项。 现在,如果我选择“1”,它将执行DLL文件而不是日志的选项。我尝试了不同的版本,比如: 如果错误级别为1,尝试将括号与else等一起使用,但均无效。这个代码有什么问题If statement 批处理文件中带有选项的Errorlevel执行错误案例,if-statement,batch-file,cmd,errorlevel,If Statement,Batch File,Cmd,Errorlevel,有人能告诉我如何使errorlevel停止取1或更大的值吗?我希望它只考虑确切的值。如果我选择2,我希望它选择第二个选项。 现在,如果我选择“1”,它将执行DLL文件而不是日志的选项。我尝试了不同的版本,比如: 如果错误级别为1,尝试将括号与else等一起使用,但均无效。这个代码有什么问题 @echo off cls choice /C 12 /M "dll or log?" if %errorlevel%=="2" dir %1\*.dll >&
@echo off
cls
choice /C 12 /M "dll or log?"
if %errorlevel%=="2" dir %1\*.dll >> %2.txt
echo DLL
goto end
if %errorlevel%=="1" dir %1\*.log >> %3.txt
echo LOG
goto end
:end
exit /b
这项决议:
@echo off
cls
rem choice /C 12 /M "dll or log?"
choice /C 12 /M "log or dll?"
if %errorlevel%==2 (dir %1\*.dll >> %2.txt
echo DLL
goto end)
if %errorlevel%==1 (dir %1\*.log >> %3.txt
echo LOG
goto end)
:end
exit /b
您需要使用“(”)”来控制流,并且不需要在错误级别编号周围使用“”
奖金 这是一种更灵活的替代方法:
@echo off
if "%~3"=="" echo lack of parameters & goto :EOF
cls
choice /C 12 /M "log or dll?"
Goto Choice%ErrorLevel%
:Choice2
dir "%~1\*.dll" >> "%~2.txt"
echo DLL in %~1
goto end
:Choice1
dir "%~1\*.log" >> "%~3.txt"
echo LOG in %~1
goto end
:end
exit /b
参数中的~删除最后的排序双引号。然后添加的引号可以防止参数内部有空格。错误级别值始终为数字。可以使用“比较操作”。有关详细信息,请参阅
IF/?
如果路径中有空格或其他特殊字符,则需要引用文件系统路径。如@Gerhard所述,在“%~3”中使用波浪号可删除%3参数周围的任何现有引号字符
顺便说一句,此脚本不检查%1、%2%或%3是否有任何实际值,也不检查其值是否可用
@echo off
cls
SETLOCAL ENABLEEXTENSIONS
choice /C 12 /M "dll or log?"
if %errorlevel% EQU 2 (
dir "%~1\*.dll" >> "%~2.txt"
echo DLL
goto end
)
if %errorlevel% EQU 1 (
dir "%~1\*.log" >> "%~3.txt"
echo LOG
goto end
)
:end
exit /b
引号是比较的一部分,因此必须出现在双方或没有…