Avatar of Vicki05
Vicki05
Flag for United States of America asked on

Find File Using a Batch File On 2 Different Drives. - If Not found on the first one to check the second one in the root directory.

Search USB Drives. If file is not found on the first one to go to next.


Hi All,

I am trying to use a batch file that is run from local disk C: which looks for a specific file. If file not found on the first one to go to the second one.  It checks the first drive letter but it will not go to the next drive.


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
Windows Batch* batc

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
NVIT

This works for me:


echo  Checking For USB
for %%a in (c e) 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%

Open in new window

Vicki05

ASKER
Hi NVIT,


How would I search for a file in the USB and if it is not there to go to next?  The file is a text file with different name. I need to copy into c drive with a different name.
NVIT

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?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
NVIT

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
)

Open in new window

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%"
)

Open in new window


Bill Prew

Looking at your posted code, I think this small adjustment would get it working...

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

Open in new window


»bp
Vicki05

ASKER
What I am trying to do is

First of all search USB drive, if file is not found then go to the next USB drive and search only on the root folder. Once the file is found, I need to delete file A in folder C:\folder1\folder2\file A.dat. Then copy the file from the USB drive to the folder as file A.dat.

The file on the USB drive is a variable with the same extension always. What I am using, it does not seem to search once the first drive is scanned.

The problem is if it finds the first drive but does not find the file, it closes the application. It does not scan the second drive for the file.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Okay, this adds a new variable with the new name and uses that in the COPY statement.

If it will exist in the destination folder and you don't want to be prompted to overwrite you could add a flag to the COPY to automatically overwrite.

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

Open in new window

EDIT: Corrected typo, changed %FileMash% to %FileMask%

»bp
Vicki05

ASKER
Thanks guys for all the help.  You have helped me a lot to resolve my issue.


Vicki
Bill Prew

Welcome!


»bp
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Vicki05

ASKER
Hi Bill,

I ran into an issue. The file is getting copied but it is getting corrupted. Is there a way around it? Can we copy the file, if the file with the same name exist to delete it. Then rename the new file to the name I need it to be?
Bill Prew

Are there perhaps multiple files that are matching the FileMask variable setting in your script?  If that were the case then the program would concatenate the multiple matching files into the single output file, which would make it different.

Check for multiple files and report back.

Also, check the input file(s) size compared to the output files size and paste those up here.  The exact output of say a DIR command would be best.


»bp
Vicki05

ASKER
Hi Bill,

I think I figured it out. I actually copied the file instead of renaming it while copying. Once copied, I deleted the old file and then renamed the file that was copied from the USB.


Thanks, Just a little modification to your batch file did the trick.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Great, good work, glad you were able to solve the issue and get it working the way you need it.


»bp