Please elaborate with examples.
What filename are you looking for?
Are you looking for that same filename on both F: and G: USB drives?
Could filename be anywhere on the usb, including a sub-folder? Or, is it always in a certain location?
This finds all instances of filename.txt.
@echo off
set fn=filename.txt
echo Checking For USB
for %%a in (f g) do if exist %%a:\nul (
set Drive=%%a
echo USB or Extended Drive Found
call :Run
) else (
echo.
echo File Not Found, Please USB Stick
pause
)
exit /b
:Run
echo %Drive%
dir /s /b "%Drive%:\%fn%"
if %errorlevel% equ 0 (
echo !!! Found "%Drive%:\%fn%"
) else (
echo Can't find "%Drive%:\%fn%" anywhere
)
This looks for a file in the root of f: and g:. If found, copies to c:\
Adjust the FNExist and FNNew variables to your needs.
@echo off
set FNExist=existname.txt
set FNNew=newname.txt
echo Checking For USB
for %%a in (f g) do if exist %%a:\nul (
set Drive=%%a
echo USB or Extended Drive Found
call :Run
) else (
echo.
echo File Not Found, Please USB Stick
pause
)
exit /b
:Run
echo %Drive%
if exist "%Drive%:\%FNExist%"
echo Copying "%Drive%:\%FNExist%" to
copy "%Drive%:\%FNExist%" "c:\%FNNew%"
) else (
echo Can't find "%Drive%:\%fn%"
)
echo off
setlocal
echo Checking For USB
for %%a in (f g) do (
if exist %%a:\nul (
set Drive=%%a
echo USB or Extended Drive Found
goto :Run
)
)
echo.
echo File Not Found, Please USB Stick
pause
goto :EndBad
:Run
echo Drive Found = %Drive%
pause
echo off
setlocal
rem Define locations and files for the process
set SearchDrives=f,g,v,z
set DestFolder=C:\folder1\folder2
set FileMask=*.dat
set NewName=hello.dat
rem Search list of drives for file
echo Checking For USB
set Drive=
for %%a in (%SearchDrives%) do (
echo. Checking drive: %%~a
rem If file exists, jump down to process it
if exist "%%a:\%FileMask%" (
set Drive=%%a:
echo. USB or Extended Drive Found
goto :FoundFile
)
)
rem If we get here file was not found, report and exit
echo.
echo File Not Found, Please USB Stick
pause
exit /b
:FoundFile
rem Copy and delete file
echo Drive Found = %Drive%
copy "%Drive%\%FileMask%" "%DestFolder%\%NewName%"
del "%Drive%\%FileMask%"
pause
exit /b
EDIT: Corrected typo, changed %FileMash% to %FileMask%
This works for me:
Open in new window