Solved

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

Posted on 2016-10-21
4
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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