Solved

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

Posted on 2016-10-21
4
54 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now