Solved

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

18 Experts available now in Live!

Get 1:1 Help Now