0

I have been searching the internet for 2 hours now and cant find what I am looking for.

Basically, I want to script searching the entire drive for specific files.

Best way I found to do this is by outputting all folders and files to an output file with dir /s. Then, I use findstr to compare my search parameters file against the dir output. The search works...but it only puts out the specific thing it found.

Results I get are

notepad.exe
download.bat

Results I want are

c:\windows\notepad.exe
c:\download.bat

. . .Or if you have code that will just search the drive for said things, then thats perfect. I'd actually want just the path, instead of the whole line found in dir /s.

either way, heres some code I was playing with.

@echo off

set dir_out=c:\dir_output.txt
set search=C:\in.txt
set out=c:\Match_Output.txt

echo delelting old %search% file
if exist  %search% del %search% /f

echo delelting old %out% file
if exist  %out% del %out% /f

echo delelting old %dir_out% file
if exist  %dir_out% del %dir_out% /f

echo creating new %search% file
echo notepad.exe >> %search%
echo download.bat >> %search%

echo changing to root directory
cd\

echo outputing entire c:\ into log file
dir /s >> %dir_out%

echo matching log file
findstr /ixg:%search% %dir_out% >> %out%
Compo
  • 36,585
  • 5
  • 27
  • 39
Joshua Dixon
  • 45
  • 1
  • 1
  • 4
  • probably, `dir /s /b` – wolfrevokcats Feb 28 '18 at 23:12
  • That really help. I found the other issue, too. When I am creating the in.txt file, whitespace is added after both entries, so no results were returned. sigh Thanks for your help – Joshua Dixon Feb 28 '18 at 23:38
  • 2
    `Dir c:\windows\*.exe c:\windows\*.bat /s /b` – ACatInLove Feb 28 '18 at 23:39
  • 2
    You are outputting a space with your code. That was explained to you in your very first question on StackOverFlow. – Squashman Mar 01 '18 at 01:42
  • outputting all files on the drive with `dir /s /b` and filter later is just so inefficient. Why waste your time printing millions or billions of files then reading that huge file again to filter? There are already a lot of questions about finding file in batch, just use it and don't reinvent the wheel – phuclv Mar 01 '18 at 02:35
  • Possible duplicate of [Iterate all files in a directory using a 'for' loop](https://stackoverflow.com/questions/138497/iterate-all-files-in-a-directory-using-a-for-loop) – phuclv Mar 01 '18 at 02:51
  • `Where /R "C:\" notepad.exe download.bat>"C:\Match_Output.txt"` or for paths only from a `for` loop, `For /F "Delims=" %A In ('Where /R "C:\" notepad.exe download.bat') Do @Echo %~dpA>>"C:\Match_Output.txt"`, _(double up the **`%`** characters from a batch file)_. – Compo Mar 01 '18 at 11:06

0 Answers0