TeDeSm
asked on
DOS Batch file - Check for existence of a drive letter or directory
My backup solution runs a daily batch file to clear the previous weeks backup from a removeable drive that has a drive letter associated with it.
The problem is that if the drive is not inserted the batch file deletes itself.
How would I change the script to check if the drive letter is available and run, or exit if it is not available returning an error.
@ECHO OFF
REM Friday archive deletion using drive letter folder paths
i:
CD Backup_Vaults
REM Delete any subdirectories and included files
FOR /D %%i IN ("Servers_Backup_Vault\*.* ") DO RD /S /Q "%%i"
CD Servers_Backup_Vault
REM Delete any other files in Servers_Backup_Vault
DEL *.* /S /Q CD..
CD Exchange_Backup_Vault
REM Delete any subdirectories and included files
FOR /D %%i IN ("Exchange_Backup_Vault\*. *") DO RD /S /Q "%%i"
REM Delete any other files in Exchange_Backup_Vault
DEL *.* /S /Q CD..
REM Delete any other files in Backup_Vaults
DEL *.* /S /Q CD..
EXIT
The problem is that if the drive is not inserted the batch file deletes itself.
How would I change the script to check if the drive letter is available and run, or exit if it is not available returning an error.
@ECHO OFF
REM Friday archive deletion using drive letter folder paths
i:
CD Backup_Vaults
REM Delete any subdirectories and included files
FOR /D %%i IN ("Servers_Backup_Vault\*.*
CD Servers_Backup_Vault
REM Delete any other files in Servers_Backup_Vault
DEL *.* /S /Q CD..
CD Exchange_Backup_Vault
REM Delete any subdirectories and included files
FOR /D %%i IN ("Exchange_Backup_Vault\*.
REM Delete any other files in Exchange_Backup_Vault
DEL *.* /S /Q CD..
REM Delete any other files in Backup_Vaults
DEL *.* /S /Q CD..
EXIT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the info on the 'bad' points of CD. I'll give your script a run this week and get back with the results.
ASKER
Great solution with full explanation of batch file contents.
dir i:\
if result of this command is successful than %ERRORLEVEL% is 1. If not %ERRORLEVEL% = 0
if ERRORLEVEL 0 GOTO End
there is your code
..
:End