Tobias Fuchs
Tobias Fuchs

Reputation: 938

Get all files in a folder and in a specific subfolder in PowerShell

I got the following folder structure for the output of my programm:

-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

My goal is it to read all PDF files inside a "data" folder. in this example it would be:

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

I already got this:

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

Its also possible that there is more than one subfolders inside the Output folder containing a data folder like "Documents/Output/Foo/Bar/data/"

Any ideas how I could achieve this?

Upvotes: 0

Views: 227

Answers (1)

Theo
Theo

Reputation: 61028

You could do this using

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
    }

Of course, change the root path D:\test

Upvotes: 3

Related Questions