此批次将搜索包含“键”文件或文件夹的驱动器,然后替换驱动器#:(是的,这是一个有效的驱动器字母,并且很少见,以至于不与任何东西相撞)到正确的驱动器。只需使用驱动器#:在您的程序中。如果批处理文件在完成工作时已退出,则错误级别为0,否则错误级别将为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)