Questo batch cercherà un'unità contenente un file o una cartella "chiave" e #SPOST DRIVE: (Sì, questa è una lettera di unità valida e abbastanza rara da non scontrarsi con nulla) all'unità corretta. Basta usare l'unità #: nel tuo programma. Se il file batch è uscito dal suo lavoro, ErrorLevel sarà 0, altrimenti ErrorLevel sarà 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)