1

我有一个简单的批处理文件,它首先启动 veracrypt,然后安装另一个批处理文件:

C:
cd C:\Program2\VeraCrypt
veracrypt /v \Device\Harddisk2\Partition1 /l L /a /p 123xyz /q 

cd /D D:\backup
start_backup.bat

start_backup.bat使用 robocopy (Windows 10) 启动备份,它将文件从一个驱动器 C 更新/复制到外部加密硬盘驱动器 D 和 L(安装名称)。

如果 veracrypt 由于任何原因(例如没有驱动器)无法安装驱动器,则批处理start_backup.bat将在没有备份的情况下启动,因为无法访问加密驱动器。start_backup.bat如果无法安装驱动器,如何避免启动?

批处理文件是用 cmd.exe 命令编写的。

4

1 回答 1

1

简单的方法是使用 operator:&&||...


@echo off && setlocal EnableDelayedExpansion

cd /d "C:\Program2\VeraCrypt"
(
.\veracrypt.exe /v \Device\Harddisk2\Partition1 /l L /a /p 123xyz /q 
) && (
cd /D "D:\backup" & call start_backup.bat
) || (
echo/ something really wrong is going on here....
%__APPDIR__%timeout.exe -1
goto :EOF
) 

rem ::  continue with more task here.... or goto :EOF

  • 或者...
@echo off && setlocal EnableDelayedExpansion

cd /d "C:\Program2\VeraCrypt"

.\veracrypt.exe /v \Device\Harddisk2\Partition1 /l L /a /p 123xyz /q  && (
cd /D "D:\backup" & call start_backup.bat ) || (
echo/ Something really wrong is going on here....
%__APPDIR__%timeout.exe -1 & goto :EOF ) 

rem ::  continue with more task here.... or goto :EOF

  • 可选择进行 3 次尝试,超时时间为 30 秒
@echo off && setlocal EnableDelayedExpansion

cd /d "C:\Program2\VeraCrypt"
:loop
set /a "_cnt+=1+0"
(
.\veracrypt.exe /v \Device\Harddisk2\Partition1 /l L /a /p 123xyz /q 
) && (
cd /D "D:\backup" & call start_backup.bat
) || (
echo/ something really wrong is going on here....
if "!_cnt!"=="3" (
     echo/ Some is really wrong here....
     %__APPDIR__%timeout.exe -1 & goto :EOF 
    ) else (
     echo/ Let's try +1 times until 3 [!_cnt!/10]
     %__APPDIR__%timeout.exe 30 
     goto :loop
   )
) 

  • 按照@Stephanif !errorlevel! 0/1 else的建议使用 30 秒的超时时间进行三次尝试的选项...
@echo off && setlocal EnableDelayedExpansion

cd /d "C:\Program2\VeraCrypt"

:loop
set /a "_cnt+=1+0" && type nul>nul

.\veracrypt.exe /v \Device\Harddisk2\Partition1 /l L /a /p 123xyz /q 

if !errorlevel! == 0 (
       cd /D "D:\backup" & call start_backup.bat
     ) else (
       echo/ Something really wrong is going on here....
       if "!_cnt!"=="4" (
            echo/ Some is really wrong here....
            %__APPDIR__%timeout.exe -1 & goto :EOF 
           ) else (
             echo/ Let's try +1 times until 03 [0!_cnt!/03]
             %__APPDIR__%timeout.exe 30 
             goto :loop
          )
     ) 

goto eof 返回到哪里

/ 中的运算符/语法重定向

于 2020-02-16T00:50:39.440 回答