@ECHO OFF SETLOCAL ENABLEEXTENSIONS ECHO. ::______________________________________________________________________________ :: :: SET VARIABLES ::______________________________________________________________________________ :: :: Set desired variable name for results here (without double-quotes) :: This example will replaces the env. variable "HOMEDRIVE" (if exist) SET _TARGETDRIVE=HOMEDRIVE (IF DEFINED %_TARGETDRIVE% SET %_TARGETDRIVE%=) :: Set your unique filename here - relative to the drive letters/unc pathes in the loop below :: Note that you should enclose this string with doublequotes, especially if it contains :: spaces or some other special characters. SET _UniqueFile="downloads\koolsid\form1.frm" ::______________________________________________________________________________ :: :: PROCESS ::______________________________________________________________________________ :: :: Set drive letters only to search for :: UNC pathes are also supported (Do not forget to enable share before) :: I strongly advice you to enclose all UNC pathes with double-quotes. FOR %%a IN (B C D E F G H I J K L M N O P Q R S T U V W X Y Z\ "\\YourNetworkShare") DO ( REM Call sub and pass current parameter CALL:_ACTION "%%~a" REM This line stops looping passed parameters if _TARGETDRIVE-variable has been already set via call-command above. REM Means: if your "_UniqueFile" has been found twice or more times this loop will stop after the first match. REM If you remove this line loop will set the _TARGETDRIVE-Variable for the last match IF DEFINED %_TARGETDRIVE% GOTO:_ExFOR ) ::______________________________________________________________________________ :: :: ExFOR ::______________________________________________________________________________ :: :_ExFOR IF DEFINED %_TARGETDRIVE% (ECHO.&SET %_TARGETDRIVE%) ELSE (ECHO.&ECHO Unique file couldn't be found) (SET _TARGETDRIVE=&SET _UNIQUEFILE=) PAUSE>NUL&ENDLOCAL ::______________________________________________________________________________ :: :: SUB: ACTION ::______________________________________________________________________________ :: :_ACTION :: Set passed parameter (driveletter/UNC path) (SET _MyDrvLetter=%1) :: Check if passed parameter is not an UNC path :: If so add a colon to the _MyDriveLetter-value (enclosed by double-quotes) SET _MyUNCDelimiter="%_MYDRVLETTER:~1,2%" SET _MyUNCDelimiter="%_MYUNCDELIMITER:"=%" IF NOT %_MYUNCDELIMITER%=="\\" (SET _MyDrvLetter="%_MYDRVLETTER:~1,-1%:")&(SET _MyUNCDelimiter=) :: If second char of _MyDrvLetter-value is ":" remove double-quotes SET _MyDRVDelimiter="%_MYDRVLETTER:~2,1%" SET _MyDRVDelimiter="%_MYDRVDELIMITER:"=%" IF %_MYDRVDELIMITER%==":" (SET _MyDrvLetter=%_MYDRVLETTER:~1,-1%)&(SET _MyDRVDelimiter=) :: Map drive/UNC path and change to it :: If successful search for desired unique file :: If successful set predefined variable PUSHD %_MYDRVLETTER%>NUL 2>&1 && DIR /B "%_UNIQUEFILE:~1,-1%">NUL 2>&1 && (SET %_TARGETDRIVE%=%_MYDRVLETTER%) :: Return a message if _TARGETDRIVE-variable is set IF DEFINED %_TARGETDRIVE% ECHO Successfully changed to %_MYDRVLETTER% and found "%_MYDRVLETTER%\%_UNIQUEFILE:~1,-1%" :: Unmap drive/UNC path mapped by pushd popd :: Clear all variable names beginning with "_MY" FOR /F "delims==" %%a IN ('SET _MY') do (SET %%a=) GOTO:EOF