此批处理将搜索包含“密钥”文件或文件夹的驱动器,并将驱动器#:(是的,这是一个有效的驱动器号,并且很少与任何东西冲突)到正确的驱动器。只需在您的程序中使用驱动器#:。如果批处理文件在其工作中退出,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)