Alex
Alex

Reputation: 1642

Search hard drive for a string and return all result with certain directories ignored

I have a batch file that do the following:
1) Search all the directory under drives (C: D: E: F: G: ) for a "keyword"
2) Return result that the path does NOT contain the word "Users", "Desktop" and "Recyele" (in other word, ignore results under these directories). Here's the code:

@echo off & setLocal EnableDELAYedeXpansion
for %%d in (c d e f g) do if exist %%d: (
for /f "delims=" %%a in ('dir/b/s %%d:\abc.txt 2^>nul ^| findstr /V /C:"Recycle" /C:"Desktop" /C:"Users"') do (
set var1=!var1! %%a,
)
echo %var1%
)
endlocal

the code works fine except I want to return ALL result instead of just the first found. I have searched stackoverflow and there was question doing ignore result, combine result and findstr but not a combination of all. Thanks in advance.

@Edit - Desktop search engine suggested but extra software isn't an option (Thanks Ark)

Upvotes: 2

Views: 1139

Answers (2)

David Ruhmann
David Ruhmann

Reputation: 11367

This will loop through each drive, search for a file, and exclude certain results. Do not call the exit command because that will stop the script. Also recommend piping directly into findstr to speed it up. Use the /V option for findstr.

@echo off & setlocal EnableDelayedExpansion
for %%D in (c d e f g) do if exist %%D: (
    for /f "delims=" %%A in ('dir/b/s %%D:\myfile.exe 2^>nul ^| findstr /V /C:"Recycle" /C:"Desktop" /C:"Users"') do (
        echo %1, %%A,
    )
    echo %1,,
)
endlocal

Upvotes: 2

Matt Williamson
Matt Williamson

Reputation: 7095

The problem here is because you've added EXIT into the loops. Take it out.

@echo off 
setLocal enabledelayedexpansion
for %%d in (c d e f g) do (
  if exist %%d: (
    for /f "delims=" %%a in ('dir/b/s %%d:\myfile.exe 2^>nul') do (
      Echo.%%a | findstr /C:"Recycle" /C:"Desktop" /C:"Users">nul || echo %1, %%a,
    )
  )
)
echo %1,,

Upvotes: 2

Related Questions