Solved

DOS Batch file - Check for existence of a drive letter or directory

Posted on 2016-10-21
4
64 Views
Last Modified: 2016-10-26
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
0
Comment
Question by:TeDeSm
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Tomas Valenta
ID: 41853561
you can add check for existence of drive by:
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
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41853565
That's why you never, ever, do anything in a batch script after a "cd" or a drive change without testing its errorlevel.
The better alternative to "cd" is to work with absolute paths. If you specify the full path, and the path doesn't exist, you'll get an error message and nothing else happens. A "cd Wherever&rd /s /q *.*" will lead to dire consequences if "Wherever" doesn't exist, or is on another drive.
That said, try this. It's in test mode and will only display the harmful commands it would run. Remove the uppercase ECHOs in lines 9 and 12 to run it for real.
@echo off
REM Friday archive deletion using drive letter folder paths
set FolderList="I:\Servers_Backup_Vault" "I:\Exchange_Backup_Vault"
set ExitCode=0
for %%f in (%FolderList%) do (
	echo Processing '%%f' ...
	if exist "%%~f" (
		for /d %%i in ("%%~f\*.*") do (
			ECHO rd /s /q "%%~i"
			if errorlevel 1 set ExitCode=1
		)
		ECHO del /q "%%~f\*.*"
		if errorlevel 1 set ExitCode=1
	) else (
		echo ERROR: folder not found.
		set ExitCode=1
	)
)
exit /b %ExitCode%

Open in new window

1
 

Author Comment

by:TeDeSm
ID: 41858161
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.
0
 

Author Closing Comment

by:TeDeSm
ID: 41859986
Great solution with full explanation of batch file contents.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question