FINDSTR并跳过文件夹 [英] FINDSTR and skipping folders

查看:159
本文介绍了FINDSTR并跳过文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Windows批处理编程和堆栈溢出,所以请原谅我,如果我问什么这是公然明显给你经验丰富,才华横溢的人。我使用Windows批处理(.bat)来查找含有用 FINDSTR 某些字符串的文件。不过,我想一个目录中跳过某些文件夹。

I'm new to Windows batch programming and to Stack Overflow, so please forgive me if I ask anything that's blatantly obvious to you seasoned, talented folks. I'm using Windows batch (.bat) to find files containing a certain string using findstr. However, I'm trying to skip certain folders within a directory.

setlocal EnableDelayedExpansion
set basedir=C:\folder

for /f %%g in ('dir /a:-h /b %basedir% ^| findstr /v "Projects" ^| findstr /v "Archive"') do (
    findstr /i /m /s /c:"request" %basedir%\%%g *.* > %basedir%\Projects\list.txt
)

当我看着LIST.TXT,从 FINDSTR 输出的文件,我发现,我告诉它不要搜索文件夹进行了全面搜查。也就是说,输出看起来是这样的:

When I look in list.txt, the file output from findstr, I find that the folders I told it not to search were searched. That is, the output looks like this:

C:\folder\somefile.rtf
C:\folder\Requests\anotherfile.rtf
C:\folder\Projects\dontsearchme.txt
C:\folder\Archive\dontsearchmeeither.txt
C:\folder\Archive\Projects\dontsearchme.txt

如果它工作正常,只有 C:\\文件夹\\ somefile.rtf C:\\文件夹\\请求\\ anotherfile.rtf 将被列入LIST.TXT。为了测试循环code,我用下面的:

If it had worked correctly, only C:\folder\somefile.rtf and C:\folder\Requests\anotherfile.rtf would have been included in list.txt. To test the looping code, I used the following:

setlocal EnableDelayedExpansion
set basedir=C:\folder

for /f %%g in ('dir /a:-h /b %basedir% ^| findstr /v "Projects" ^| findstr /v "Archive"') do (
    echo %basedir%\%%g
)

这code ++工程,根据需要;它会跳过项目和存档文件夹。我认为这个问题有什么做的,我怎么叫 FINDSTR ,但我一直没能确定我的方式错误。任何有识之士将是非常美联社preciated!

That code works as desired; it skips the Projects and Archive folders. I assume that the problem has something to do with how I'm calling findstr but I haven't been able to identify the error of my ways. Any insight would be much appreciated!

太感谢了!结果
-Alex

Thanks so much!
-Alex

推荐答案

在FINDSTR / S选项是导致它来搜索所有文件夹,从而绕过你的for循环。意图

The FINDSTR /S option is causing it to search all folders, thus bypassing the intent of your FOR loop.

斯蒂芬没有成功诊断了另一个问题与code。使用覆盖的追加模式,而不是对重定向。

Stephan did successfully diagnose another problem with your code regarding redirection using overwrite instead of append mode.

但还有一个更简单的方法来获得你想要的结果。干脆让FINDSTR搜索所有文件夹和管道,结果额外FINDSTR删除包含不需要的文件夹结果。由于没有循环,你可以安全地使用owverwrite模式重定向。

But there is a much simpler method to get your desired result. Simply let FINDSTR search all folders, and pipe the result to an additional FINDSTR to remove results containing the unwanted folders. Since there is no loop, you can safely use owverwrite mode for redirection.

findstr /misl request "%basedir%\*" | findstr /liv "\\projects\\ \\archive\\" >"%basedir%\Projects\list.txt"


修改

以上简单的解决方案会浪费稍后将得到排除时间搜索文件夹。如果这些文件夹是巨大的,这可能浪费宝贵的时间。

The above simple solution will waste time searching folders that will later get excluded. This could waste valuable time if those folders are huge.

下面的脚本不会打扰扫描%BASEDIR%\\项目或%BASEDIR%\\归档文件夹中。

The following script will not bother scanning the "%basedir%\Projects" or "%basedir%\Archive" folders.

@echo off
setlocal EnableDelayedExpansion
set basedir=C:\folder

>"%basedir%\Projects\list.txt" (
  findstr /mil request "%basedir%\*"
  for /f "eol=: delims=" %%F in (
    'dir /a:d-h /b %basedir% ^| findstr /vixl "projects archive"'
  ) do findstr /smil request "%basedir%\%%F\*"
)

如果你想跳过名为项目或归档,无论它们出现在树中,则所有文件夹:

If you want to skip all folders named "Projects" or "Archive", regardless where they appear in the tree, then:

@echo off
setlocal EnableDelayedExpansion
set basedir=C:\folder

>"%basedir%\Projects\list.txt" (
  findstr /mil request "%basedir%\*"
  for /f "eol=: delims=" %%F in (
    'dir /s /a:d-h /b %basedir% ^| findstr /vir "[\\]projects[\\] [\\]archive[\\] [\\]projects$ [\\]archive$"'
  ) do findstr /mil request "%%F\*"
)

这篇关于FINDSTR并跳过文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆