使用goto命令作为批处理文件中的后备

时间:2014-11-02 01:05:08

标签: batch-file cmd

如何使其无法找到文件夹时,代码将使用goto命令?

这是我的代码:

:T
Echo Folder is Already Unlocked

:CODE
if attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" goto T
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" Codes
call :c 09 " Codes Unlocked successfully
goto UNLOCK

1 个答案:

答案 0 :(得分:0)

以下是一些示例 - 作为“答案”给出,因为格式化更容易使用

@ECHO OFF
SETLOCAL
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: Now some multi-line responses
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (
 echo errorlevel is 1 or greater
 echo I say again - errorlevel 1 1 or greater
 echo I say a third time - errorlevel 1 1 or greater
) else (
 echo errorlevel is 0
 echo again errorlevel is 0
 echo once again errorlevel is 0
)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (
 echo Y-errorlevel is 1 or greater
 echo I say again - errorlevel 1 1 or greater for Y
 echo I say a third time - errorlevel 1 1 or greater - Y
) else (
 echo Y-errorlevel is 0
 echo Y-again errorlevel is 0
 echo Y-once again errorlevel is 0
)
::  conditional modification of variable
set /a var=22
echo x|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing x, var is %var%
set /a var=22
echo y|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing y, var is %var%
:: You'll need to change these lines to files/directories that exist or don't exist
set "fileexists=c:\pdoxusrs.net"
set "direxists=c:\users"
set "filemissing=c:\missing.net"
set "dirmissing=c:\nothere"
for %%a in ("%fileexists%" "%direxists%" "%filemissing%" "%dirmissing%") do (
 if exist "%%~a" (echo "%%~a" exists) else (echo "%%a" missing)
)
goto :eof

现在如果errorlevel没有像您期望的那样设置为0 /非零,那么请尝试使用if exist。实际上,这种方式可能更容易。不幸的是“不起作用”根本就没有意义 - 我们需要确切知道发生了什么,我们需要你edit原始帖子来添加你当前的代码(或者至少是代表部分。)