이 배치는 "키"파일 또는 폴더가 포함 된 드라이브를 검색하고 Subst Drive #: (예, 이것은 유효한 드라이브 문자이며, 올바른 드라이브에 대한 어떤 것도 충돌하지 않을 정도로 드물다). 프로그램에서 드라이브 #을 사용하십시오. 배치 파일이 작업을 수행 할 때 종료되면 ErrorLevel은 0이됩니다. 그렇지 않으면 ErrorLevel이 1이됩니다.
@echo off
setlocal enableextensions
rem if our special drive is assigned, release it
if exist #:\ subst #: /d >nul
rem if there are problems, exit
if errorlevel 1 goto endSearch
rem search drives for "key" file/folder
set "keyFile=\data\mySpecialFile.csv"
for %%d in (z y x w v u t s r q p o n m l k j i h g f e d c b a) do (
vol %%d: >nul 2>nul && if exist "%%d:%keyFile%" (
rem map our drive
subst #: %%d:\ > nul 2> nul
rem if everything ok end search
if not errorlevel 1 goto endSearch
)
)
:endSearch
rem cleanup and return errorcode as necessary
endlocal && if exist "#:%keyFile%" ( exit /b 0 ) else (exit /b 1)