Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File extension while searching

I am trying to get a list of all my files with a specific extension.

(...)$_.Extension -eq ".$ext"

I read extension from console to script.

My question is how to find any file with an extension of .*?

Edit:

Here's the rest of the code:

$setOfFolders = (Get-ChildItem -Path D:\ -Directory).name 
Write-host "Set last write date " -ForegroundColor Yellow -NoNewline 
$ostZmiana= read-host $exten = read-host "Set extensions " 

ForEach ($f in $setOfFolders) 
{ 
    $saveTofile = "C:\Users\pziolkowski\Desktop\Outs\$f.txt" 
    Get-ChildItem -Path D:\Urzad\Wspolny\$f -Recurse | ? {$_.LastAccessTime -lt $ostZmiana -and $_.Extension -eq ".$exten"} | % {Get-acl $_.FullName} |sort Owner | ft -AutoSize -Wrap Owner, @{Label="ShortPath"; Expression= $_.Path.Substring(38)}} > $saveToFile 
}
like image 805
pawel__86 Avatar asked Sep 18 '13 11:09

pawel__86


People also ask

How do I search for a file with a specific extension?

For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.

What is file type search?

Google can help you find books, documents, spreadsheets, presentations, Adobe files, and much more with the help of file type search.

How do I search for a file without an extension?

You can check the file extension from the Type column in Windows file explorer. Alternatively, you could right-click on the file and select Properties. You'll see the Type of file in the General tab of file properties. If it says File, you know that the file has no extension.


2 Answers

You can also use the -filter on Get-ChildItem:

Get-ChildItem -Filter "*.txt"

And you can specifiy recursion too:

Get-ChildItem -Recurse -Filter "*.txt"

like image 105
MichaelLake Avatar answered Sep 27 '22 16:09

MichaelLake


The $_.Extension will see the file as, (in the case of a text file) .txt

If $ext is currently a valid extension, then .$ext would look like ..txt echoed out.

The easiest way to get a list of files with a specific extension in PowerShell is one of two, really.

C like syntax:

$myList
Get-ChildItem |`
    Foreach-Object {
    if ($_.Extension -eq ".txt") { $myList += $_}
    }

PowerShell style:

Get-ChildItem | Where-Object {$_.Extension -eq ".txt"} | Do stuff

To get all files, as most files have an extension, just use Get-ChildItem or GCI or ls. (GCI and LS are aliases for Get-ChildItem).

To get all files with an extension, but not a specific extension:

Get-ChildItem | Where-Object {$_.Extension}

This evaluates as a bool, so true or false.

These are off the cuff, but should get you going in the right direction.

like image 23
Austin T French Avatar answered Sep 27 '22 18:09

Austin T French