We help IT Professionals succeed at work.

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.

Vicki05
Vicki05 asked
on
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
Comment
Watch Question

NVITEnd-user support

Commented:

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%

Author

Commented:
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.
NVITEnd-user support

Commented:

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?

NVITEnd-user support

Commented:

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


Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
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.
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
This sounds like what you are describing.  Naturally there are a number of ways to structure the script, this is just a simple approach.

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

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%"
del "%Drive%\%FileMask%"
pause
exit /b

Open in new window


»bp
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

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


Vicki
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome!


»bp

Author

Commented:
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 PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
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.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

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


»bp