CMD - 检查正则表达式的文件夹路径

时间:2018-02-04 09:18:07

标签: cmd

我正在搜索具有特定模式的所有文件夹路径。

Ex. 
D:\Testfolder>tree
Folder PATH listing for volume Working
D:.

├───fifth
│   ├───first
│   ├───second
│   └───third
│       └───_dn
│           ├───first
│           └───second
├───first
│   ├───first
│   └───second
│       └───_dn
│           ├───first
│           └───second
├───fourth
│   └───first
│       └───_dn
│           ├───first
│           └───second
├───second
│   ├───first
│   ├───second
│   └───third
│       └───_dn
│           └───first
└───third
    ├───first
    └───second
        └───_dn
            ├───first
            └───second

如果路径中有单词" _dn"然后我需要将它保存在一个文件中。

预期产出:

D:\Testfolder\fifth\third\_dn
D:\Testfolder\fifth\third\_dn\first
D:\Testfolder\fifth\third\_dn\second
D:\Testfolder\first\second\_dn
D:\Testfolder\first\second\_dn\first
D:\Testfolder\first\second\_dn\second
D:\Testfolder\fourth\first\_dn
D:\Testfolder\fourth\first\_dn\first
D:\Testfolder\fourth\first\_dn\second
D:\Testfolder\second\third\_dn
D:\Testfolder\second\third\_dn\first
D:\Testfolder\third\second\_dn
D:\Testfolder\third\second\_dn\first
D:\Testfolder\third\second\_dn\second

更新1

有人发布了几乎正确的答案然后将其删除。我无法追踪他的名字来推荐他。

代码是

(For /R "D:\Testfolder" /D %A In (*_dn*) Do @Echo %A)>"savefile.txt"

返回值为

D:\Testfolder\fifth\third\_dn
D:\Testfolder\first\second\_dn
D:\Testfolder\fourth\first\_dn
D:\Testfolder\second\third\_dn
D:\Testfolder\third\second\_dn

但是,此返回值缺少_dn \,

之后的所有路径
Ex.
D:\Testfolder\fifth\third\_dn\first
D:\Testfolder\fifth\third\_dn\second
D:\Testfolder\first\second\_dn\first
D:\Testfolder\first\second\_dn\second
D:\Testfolder\fourth\first\_dn\first
D:\Testfolder\fourth\first\_dn\second
D:\Testfolder\second\third\_dn\first
D:\Testfolder\third\second\_dn\first
D:\Testfolder\third\second\_dn\second

3 个答案:

答案 0 :(得分:1)

您也许可以尝试Dir使用Find,如下所示:

(Dir /B/S/AD "D:\Testfolder"|Find /I "\_dn\")>"savefile.txt"

答案 1 :(得分:1)

试试这样:

for /f "delims=" %a in ('Dir /ad /s /b D:\Testfolder\ ^| find /I "_dn"') do @echo %a

答案 2 :(得分:0)

我看到你已经有了答案。如果你想进入PowerShell,你可以做这样的事情。总有一天,这可能会被认为更易于维护。

Get-ChildItem -Directory -Recurse -Filter '_dn' -ErrorAction SilentlyContinue |
    ForEach-Object { $_.FullName } >savefile.txt

或者,使用别名和最小参数,它可能是神秘的,不可维护的。

gci -di -rec -filt '_dn' -ea si | % { $_.FullName } >savefile.txt