3

I want to find out all the files which contain a keyword in a directory and all the subdirectories, and there are some .svn directories, I should ingore these directories.

In Linux system, I can do it easily by:

  grep -r keyword ./ --exclude-dir ".svn"

But when I turn to windows, findstr is a substitute for grep in windows, I try to do it like this:

  findstr /s keyword ./*

The result will return with the .svn directories, which is not what I want.

I read the TN Findstr, on microsoft website

  /v   : Prints only lines that do not contain a match.

But when I try it like below, It still doesn't work.

 findstr /s keyword /v ".svn" ./*

I think there may be some problem with my usage of findstr. Any help would be great, thanks in advance.

        ***************************** update1 *****************************

In fact, I am trying to use grep in GVim, it will call 'findstr`.

4

4 回答 4

2

AFAIK 你不能排除findstr. 但是,在 PowerShell 中,您可以执行以下操作:

$root = '.'
$keyword = 'keyword'

Get-ChildItem $root -Recurse -Force | ? {
  -not $_.PSIsContainer -and $_.FullName -notlike "*\.svn*"
} | Select-String $keyword -AllMatches

或者您可以利用.svn隐藏子文件夹的事实并执行以下操作:

$root = '.'
$keyword = "keyword"

Get-ChildItem $root -Recurse | ? { -not $_.PSIsContainer } |
    Select-String $keyword -AllMatches

hidden但是,如果您要搜索的某些文件具有属性集,这将不起作用。

于 2013-07-29T08:39:40.287 回答
1

假设您的文件没有.svn\在内容中返回,这应该可以工作。

 findstr /s "keyword" * |findstr /v /i /L ".svn\"
于 2013-07-29T12:51:18.987 回答
0

感谢@Christian.K 的帮助,我解决了这个问题:

我设置 grepprg 如下:</p>

 :set grepprg=findstr\ /s\ $*\ \ ^\\\|\ findstr\ /v\ /i\ /L\ \".svn\"

上面的选项只匹配.svn。如果你想匹配.svn\in gvim,你应该:

 :set grepprg=findstr\ /s\ $*\ \ ^\\\|\ findstr\ /v\ /i\ /L\ \".svn\\\\\"
于 2013-10-05T09:10:43.237 回答
0

尝试创建一个包含此文件的 bat 文件 find.bat 并键入find . keyword. 如果需要,您可以使用 adoskey来隐藏必须键入的.内容。

@echo off
SETLOCAL
set curDir=%1
if "%curDir%"=="." set curDir=%cd%
for /f %%i in ('dir /b/a-h-d %curDir% 2^>nul') do findstr /ilmp %2 %curDir%\%%i
for /f %%i in ('dir /b/ad-h %curDir%') do call %0 %curDir%\%%i %2
ENDLOCAL
于 2016-07-22T00:07:39.930 回答