在PowerShell中的文件夹和特定子文件夹中获取所有文件

问题描述 投票:0回答:1

我的程序输出有以下文件夹结构:

-Documents
    -Output
        -data
            file1.pdf
            file2.pdf
        -lib_dvs
            data.txt
            error.log
    -Testing
        _repl_dev
            -data
                name.pdf
                foo.pdf
            -lib_dvs
                data.txt
                error.log
        _repl_prod
            -data
                name.pdf
                foo.pdf
            -lib_dvs
                data.txt
                error.log

我的目标是读取“数据”文件夹中的所有PDF文件。在此示例中为:

Documents/Output/data/file1.pdf
Documents/Output/data/file2.pdf
Documents/Testing/_repl_dev/data/name.pdf
Documents/Testing/_repl_dev/data/foo.pdf
Documents/Testing/_repl_prod/data/name.pdf
Documents/Testing/_repl_prod/data/foo.pdf

我已经知道了:

Get-ChildItem -Path "$([Environment]::GetFolderPath('MyDocuments'))/**/data/" -Include *.pdf -Recurse

[输出文件夹中也可能有多个子文件夹,其中包含一个数据文件夹,例如“ Documents / Output / Foo / Bar / data /”

任何想法我如何实现这一目标?

powershell glob
1个回答
2
投票

您可以使用]来完成>

Get-ChildItem -Path 'D:\test' -Filter '*.pdf' -File -Recurse | 
    Where-Object { $_.DirectoryName -like '*\data' } | 
    ForEach-Object {
        # do something with the found files. 
        # for demo, just output the FullNames
        $_.FullName
    }

当然,更改根路径D:\test

© www.soinside.com 2019 - 2024. All rights reserved.