سؤال

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

هل كانت مفيدة؟

المحلول

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%.?"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top