Solved

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

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

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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