Computer Hope

Software => Computer programming => Topic started by: Whitebeard1 on June 07, 2013, 02:28:20 AM

Title: How to search a file in the whole drive and take action-batch
Post by: Whitebeard1 on June 07, 2013, 02:28:20 AM
Hey guys i know i asked questions a lot, but im new to batch. So i want to make a batch file find a file anywhere in the drive and take action(eg, delete it, rename it.)
will IF EXIST work?
thanks
Title: Re: How to search a file in the whole drive and take action-batch
Post by: Sidewinder on June 07, 2013, 07:15:22 AM
Except for automation scripts, I am a big fan of batch file prompts. It eliminates remembering command line arguments and which order they are expected.

Code: [Select]
@echo off
setlocal

set /p drive=Enter Drive:
set /p arg=Enter Search String:

if .%arg%==. (
  for /f "tokens=* delims=" %%i in ('dir %drive%:\ /a-d /s /b') do (
  echo %%i
)
) else (
    for /f "tokens=* delims=" %%i in ('dir %drive%:\ /a-d /s /b ^| find /i "%arg%"') do (
      echo %%i
    )
)

User is prompted for both the drive letter and the file name. File names are searched to match the string entered by the user. Partial file names can be used.

 8)
Title: Re: How to search a file in the whole drive and take action-batch
Post by: Whitebeard1 on June 08, 2013, 06:28:13 AM
Thanks alot sidewinder. this is very helpful., its exactly what i was trying to do.