Search code examples
windowsbatch-filewindows-7findstr

Why does findstr also search in files outside specified directory?


There is a simple behavior of findstr that I do not understand on Windows 7.

I run a batch file in administrator mode (if not, they simply do not launch) with the following script in several different locations:

@echo off
set local=%~dp0
echo %local%
REM to check that I am where I think I am
findstr /s /i /c:"stringtofind" %local%*.ext

When I run this script from a location in "C:\Program files\~" or "C:\Program files (x86)\~" the output includes .ext files outside the %local% directory and sub-directories with "stringtofind". Actually all the matching files are on the hard disk.

This behavior does not appear in every other location I could test (anywhere else in a C:\ directory basically) where only the matching file in the directory and sub-directories appear.

Is there something specific to the Program files directories?


Solution

  • Try changing to

    findstr /s /i /c:"stringtofind" "%local%*.ext"
    

    In the cases you mention, the directoryname contains spaces, so findstr is finding multiple arguments. "Using the quotes" groups "strings containing spaces" into one string.