Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select files that have no extension using powershell

Tags:

powershell

I have a drive with lots of directories and files, some of which are broken and have no file extension. I would like to delete all of the files that do NOT have an extension. I have tried gci | where {$_.extension -eq ""} and gci | where {$_.extension -eq "."} and variations of those, but end up getting nothing or everything on the drive.

If I look at the results of gci | select Extension the files show nothing for the extension, just like a directory, not sure where to go.

Any help would be appreciated.

like image 986
Slowstuff Avatar asked Feb 12 '14 20:02

Slowstuff


People also ask

How do I find a file without an extension?

Windows Explorer - Advanced Query Syntax You can use a for /f loop iterating the output of a dir command with the /B and /A-D parameters, and then use some conditional if logic to only output files without any extensions using substitutions for the iterated files in the specified directory.

What do I do with a file that has no extension?

To open the files with no extensions, first, you have to identify their extension or type. After identifying their extension, install the right program on your computer to open that file. To know the file extension or file type, switch the view mode in File Explorer to Details.

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.


2 Answers

gci -File -Recurse | ?{!($_.Extension)}
like image 155
TheMadTechnician Avatar answered Sep 28 '22 08:09

TheMadTechnician


The simplest and fastest solution (PSv3+) is to use the -Filter parameter:

Get-ChildItem -File -Filter *.

as explained in this answer.

As for a pipeline-based solution:

PowerShell Core now offers the -Not switch with the simplified comparison statement syntax that itself was introduced to Where-Object (whose built-in alias is ?) in Windows PowerShell v3, which enables the following, more concise variant of TheMadTechnician's helpful answer:

Get-ChildItem -File | Where-Object -Not Extension

As in TheMadTechnician's answer, this uses implicit Boolean logic: any nonempty string evaluates to $True in a Boolean context, so applying -Not means that $True is only returned if the string is empty, i.e., if the file has no extension.


As for what you tried:

gci | where {$_.extension -eq ""} works in principle, except that it also includes directories.

In PSv3+ you can use the -File switch to limit the results to files, as above.

In PSv2 you must add a condition to the script block, based on the .PSISContainer property only returning $True for directories:

gci | where { -not $_.PSIsContainer -and $_.extension -eq "" }
like image 23
mklement0 Avatar answered Sep 28 '22 08:09

mklement0