Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve recursively any files with a specific extensions in PowerShell?

Tags:

powershell

For a specific folder, I need to list all files with extension .js even if nested in subfolders at any level.

The result for the output console should be a list of file names with no extension line by line to be easily copy and pasted in another application.

At the moment I am trying this, but in output console I get several meta information and not a simple list.

Get-ChildItem -Path C:\xx\x-Recurse -File | sort length –Descending

Could you please provide me some hints?

like image 279
GibboK Avatar asked Jun 25 '15 11:06

GibboK


People also ask

How do I recursively search for a file in PowerShell?

Use dir Cmdlet With -Recurse Switch in PowerShell to Search Files Recursively. The dir cmdlet is an alias for the Get-ChildItem . It also displays a list of files and directories on the specific location.

How do I get the file extension in PowerShell?

Use the Get-ChildItem cmdlet to get the file item and using its Extension property it returns the file name extension. The Get-ChildItem command accepts the file path as input and gets the file item. It then passes the output to the Select command to get file extension using the Extension property.

How do I get a list of files in a directory in PowerShell?

If you want to list files and directories of a specific directory, utilize the “-Path” parameter in the “Get-ChildItem” command. This option will help PowerShell list all the child items of the specified directory. The “-Path” parameter is also utilized to set the paths of one or more locations of files.

Which perform an action that is available to the file name extension of the file specified in PowerShell?

-Verb — Perform an action that is available to the file name extension of the file specified.


5 Answers

If sorting by Length is not a necessity, you can use the -Name parameter to have Get-ChildItem return just the name, then use [System.IO.Path]::GetFileNameWithoutExtension() to remove the path and extension:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File -Name| ForEach-Object {
    [System.IO.Path]::GetFileNameWithoutExtension($_)
}

If sorting by length is desired, drop the -Name parameter and output the BaseName property of each FileInfo object. You can pipe the output (in both examples) to clip, to copy it into the clipboard:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File| Sort-Object Length -Descending | ForEach-Object {
    $_.BaseName
} | clip

If you want the full path, but without the extension, substitute $_.BaseName with:

$_.FullName.Remove($_.FullName.Length - $_.Extension.Length)
like image 195
Mathias R. Jessen Avatar answered Sep 22 '22 11:09

Mathias R. Jessen


The simple option is to use the .Name property of the FileInfo item in the pipeline and then remove the extension:

Get-ChildItem -Path "C:\code\" -Filter *.js -r | % { $_.Name.Replace( ".js","") }
like image 22
Stephen Dunne Avatar answered Sep 23 '22 11:09

Stephen Dunne


There are two methods for filtering files: globbing using an Wildcard, or using a Regular Expression (Regex).

Warning: The globbing method has the drawback that it also matches files which should not be matched, like *.jsx.

# globbing with Wildcard filter 
# the error action prevents the output of errors
# (ie. directory requires admin rights and is inaccessible)
Get-ChildItem -Recurse -Filter '*.js' -ErrorAction 'SilentlyContinue' 

# filter by Regex
Where-Object { $_.Name -Match '.*\.js$' }

You then can sort by name or filesize as needed:

# sort the output 
Sort-Object -PropertyName 'Length'

Format it a simple list of path and filename:

# format output
Format-List -Property ('Path','Name')

To remove the file extension, you can use an select to map the result:

Select-Item { $_.Name.Replace( ".js", "")

Putting it all together, there is also a very short version, which you should not use in scripts, because it's hardly readable:

ls -r | ? { $_.Name -matches '.*\.js' } | sort Length | % { $_.Name.Replace( ".js", "") | fl
like image 44
MovGP0 Avatar answered Sep 21 '22 11:09

MovGP0


If you like brevity, you can remove the ForEach-Object and quotes. -Path defaults to the current directory so you can omit it

(Get-ChildItem -Filter *.js -Recurse).BaseName | Sort length -Descending
like image 39
Jason S Avatar answered Sep 20 '22 11:09

Jason S


The above Answers works fine. However in WIndows there is a alias called ls the same as on linux so another shorter command that works too would be ls -Filter *.exe

like image 36
Pepe is Sad Avatar answered Sep 21 '22 11:09

Pepe is Sad