繁体   English   中英

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

[英]Get all files in a folder and in a specific subfolder in PowerShell

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

-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

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

任何想法我怎么能做到这一点?

你可以使用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM