1

I recently posted a question regarding skipping certain folders when using findstr. (Which, by the way, was my first question here and was met with fantastic answers! Thanks, Stack Overflow community!) Now I would like to skip certain file types as well. That is, I don't want findstr to even attempt to search files with extensions of, for example, .pdf, .r, and .sas. I do, however, want to search .sas7bdat files, so excluding cases where the file name contains ".sas" at all is insufficient. My code as it stands is as follows:

@echo off
setlocal EnableDelayedExpansion
set basedir=N:\folder
set outfile=N:\folder\Projects\list.txt

if exist %outfile% then del %outfile%

findstr /mil frecord "%basedir%\*" > %outfile%

for /f "eol=: delims=" %%g in (
    'dir /a:d-h /b %basedir% ^| findstr /vixl "projects requests temp"'
) do findstr /smil frecord "%basedir%\%%g\*" >> %outfile%

set basedir=
set outfile=

This captures all files in %basedir% which contain the string "frecord" and searches all subfolders of %basedir% except for Projects, Requests, and temp. The results are written to %outfile%.

The %basedir% directory and its subfolders are quite large so I need to make sure that findstr doesn't take up unnecessary time searching file types that are not of interest. However, I'm not sure where the restriction belongs in the above code. The FOR loop is only looping through directories within %basedir%, not specific files. Does this require a second similarly constructed FOR loop? If so, where would it go? Or perhaps there's a simpler approach? I'm new to Windows batch programming so please forgive me if the answer is blatantly obvious.

Any advice or guidance would be much appreciated. Thanks so much!
-Alex

Community
  • 1
  • 1
Alex A.
  • 5,466
  • 4
  • 26
  • 56

1 Answers1

0

This answer was edited and this code is untested:

Remove the line shown here if there are lots of small files, as it will slow the process echo checking file "%%a"

@echo off
setlocal
set "basedir=N:\folder"
set "outfile=N:\folder\Projects\list.txt"

if exist "%outfile%" then del "%outfile%"

for /r "%basedir%" %%a in (*) do >>"%outfile%.1" echo %%a
findstr /i /v /l /c:"%basedir%\projects\" /c:"%basedir%\requests\" /c:"%basedir%\temp\" "%outfile%.1" | findstr /i /v /r /c:"\.pdf$" /c:"\.r$" /c:"\.sas$" >"%outfile%.2"
   for /f "delims=" %%a in (' type "%outfile%.2" ') do (
      echo checking file "%%a"
      echo =========== checking "%%a">>"%outfile%"
      findstr "frecord" "%%a">>"%outfile%"
   )
set basedir=
set outfile=
pause
del "%outfile%.?"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I'd suggest the first `FOR` should be on `DIR /b /ad` (no `/s`) with `findstr /b /e...` and the `\` removed from the target strings. As it stands, any subdir starting `projects` etc would be excluded, not just those at the `basedir` level - but OP needs to clarify intention. The second `FOR`'s `dir` should then acquire `/s` and mask `"%basedir%\%%a"` – Magoo Nov 21 '13 at 06:55
  • @Magoo: To clarify, the Projects subdirectory within `%basedir%` should be skipped but Projects within another folder, say Archive, should not be skipped. Thanks for noticing that! – Alex A. Nov 21 '13 at 15:27
  • @magoo I've changed the code so this doesn't happen. Thanks Magoo. – foxidrive Nov 22 '13 at 08:14