giordano
giordano

Reputation: 3152

Veracrypt with batchfile: how can execution be stopped if drive is not mounted?

I have a simple batch-file which starts first veracrypt and after mounting another batch-file:

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 starts a backup using robocopy (Windows 10) which updates/copies files from one drive C to an external encrypted harddrive D respectively L (mount name).

If veracrypt can't mount the drive for any reason (e.g. there is no drive) the batch start_backup.bat will be started without backup since encrypted drive is not accessible. How can I avoid the start of start_backup.bat in case the drive couldn't be mounted?

The batch-file is written with commands of cmd.exe.

Upvotes: 1

Views: 1822

Answers (1)

Io-oI
Io-oI

Reputation: 2565

Simple way to do this is using operator: && and || ...


@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

  • Or...
@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

  • Option to go through three attempts and with a timeout of 30 seconds
@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
   )
) 

  • Option to go through three attempts and with a timeout of 30 seconds by using if !errorlevel! 0/1 else as suggested by @Stephan...
@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
          )
     ) 

Where does goto eof return to

Operator/Syntax Redirection in /

Upvotes: 1

Related Questions