![](/img/trans.png)
[英]Find last occurence of a string using findstr on windows command line
[英]Windows command line to findstr 1 level of subdirectory down
尋找執行以下操作的命令行(或批處理),
我有3級目錄,需要搜索文本文件中包含的特定字符串,搜索所有文件,向下1級子目錄。
所以我知道這將遞歸搜索字符串
findstr / S“ STRING” *
但是我不希望搜索比僅1個子目錄級別低。
看起來很簡單,但我找不到任何可靠的方法。 我能找到的最接近的東西就是這個線程。
但是,在進行批處理之前,有一種更簡單的方法(命令行)來執行此相當簡單的任務嗎?
謝謝你的時間!
這是一條命令行,用於枚舉當前工作目錄的子目錄dir /ad/b
並在每個目錄中搜索包含搜索字符串的文件。 結果保存到文本文件中。
for /f "delims=" %A in ('dir /ad/b') do @findstr "STRING" "%~A\*" >> results.txt
注意:按照當前配置,將為文件中的每個匹配行輸出結果行。 意味着可以為單個文件輸出多個查找結果。
ChildFolderA\File1.txt:line with STRING
ChildFolderA\File1.txt:another line with STRING in file1
ChildFolderA\File2.txt:another file and line with STRING
ChildFolderB\File5.txt:another folder with a file containing the search STRING
@echo off
setlocal enableextensions
(@for /d %%a in (*) do @for %%b in ("%%~fa\*") do @echo(%%~fb)|findstr /f:/ /c:"string" > file.txt
這將枚舉所有文件向下一級並將此文件列表發送到findstr命令。 findstr會將字符串搜索到指示的列表中並將輸出發送到最終文件
要從命令行使用,請將雙百分號%%
替換為單個%
這應該從命令行執行,並將file.log
寫入當前文件夾
for /d %a in (*) do findstr "STRING" "%a\*">>file.log
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.