James Trory
James Trory

Reputation: 121

How do specify folder location AND file type in Powershell

I would like to use Powershell to look up folders containing a specific word, and then look inside those for a specific file type. I'm having trouble figuring out how to do this with Powershell.

find . \
-type d -iname '*_OUTPUT' -maxdepth 2 \
-exec find {} -type f -iname '*.psd' -not -path "*.\_*" \; >> /Volumes/Path-to-output-folder/Output.csv

This is the Bash script I wrote that I run in Terminal. It works sometimes but it also crashes the SMB server connection regularly, because I'm running this on a Mac and it doesn't like the SMB server. So I want to try and do the same thing on a Windows machine because it has a reliable connection to the server.

What I'm doing here is going two folders deep of the starting directory -maxdepth 2 and looking there for any folders that end with _OUTPUT -type d -iname '*_OUTPUT'. Then, looking inside those folders for any files with the extension .psd -exec find {} -type f -iname '*.psd', ignoring hidden files which might get caught up in the search -not -path "*.\_*". Any files found are output as the full path to a CSV sheet.

I can look up folders in Powershell using -directory, and look up file types using Get-ChildItem, but I'm having trouble figuring out how to combine them in the way I did above using Bash. Is there a Powershell master out there that could help?

Upvotes: 0

Views: 255

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Let's start by breaking it down:

# Finding folders with names ending in _OUPUT
Get-ChildItem -Directory -Depth 2 -Filter *_OUTPUT

# Finding files in a folder with the .psd extension
Get-ChildItem -Path .\folder\ -File -Filter *.psd

We can combine the two with the pipeline like so:

Get-ChildItem -Directory -Depth 2 -Filter *_OUTPUT |Get-ChildItem -File -Filter *.psd

Finally, use Where-Object to exclude files starting with ._:

Get-ChildItem -Directory -Depth 2 -Filter *_OUTPUT |Get-ChildItem -File -Filter *.psd |Where Name -notlike ._*

Upvotes: 1

Related Questions