5

I was trying to recursively list using ls all the *.py files in a directory in Windows PowerShell. It didn't work. I checked up on the man page and then online page that -Recurse flag doesn't accept wildcards so *.py wouldn't work.

I want to know whether there is a simple in-built way to recursively list the files of a particular file extension in a directory using Windows PowerShell 2.0?

I am a newbie in PowerShell and don't want to indulge in Shell scripting at this stage. So please recommend in-built commands, flags, etc. only, if any.

By commands I means the in-built keywords of the PowerShell. I am not sure if that is proper vocabulary for PowerShell.

4 Answers 4

8

Use the Filter parameter:

ls $path -filter *.py -recurse
Sign up to request clarification or add additional context in comments.

Comments

3

This will do the trick for you.

gci -Recurse | ? {$_.name -match ".py"}

or

gci -Recurse -Include *.py

Comments

0

ls is an alias for Get-ChildItem. (So is dir and gci). A simple Google will give you all kinds of examples but the thing to know is -include (or -exclude) are built in parameters that will show (or not show) file types you are looking for. An additional parameter, -filter, can also be used for partial file types. The switch parameter -recurse checks the contents of subfolders in the directory as well. Example01: gci -path \$computername\$directory -Include *.py -recurse Example02: gci -path \$computername\$directory -Filter *.py -recurse

I like to suppress errors so a full example would look like this: gci -path \Svr01\C$ -Include *.py -recurse -erroraction SilentlyContinue

Comments

0

I don't see how anybody would have got this working at any time.

gci -recurse works fine and lists all gci -filter *.txt works fine, lists all .txt files but does not recurse

gci -filter *.txt -recurse returns either only .txt files from root or nothing. It seems to apply the *.txt filter to the directory names therefor seeing no directories and thus does not recurse at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.