I have a folder including sub-folders in my Windows PC where I have multiple files of images with different dimensions with standard formatted names as shown below.
Now I want to delete all those image files that are of different sizes as shown in their name and should leave the original one only.
For that, I tried many queries as shared below but non of these are working...
Get-ChildItem $Path | Where{$_.Name -Match '.*[0-9]+x[0-9]+.\(jpg\|png\|jpeg\)$'} | Remove-Item
find -type f -regex '.*[0-9]+x[0-9]+.\(jpg\|png\|jpeg\)$' -delete
find -name '.*[0-9]+x[0-9]+.\(jpg\|png\|jpeg\)$' -delete
None of the above is working so let me know what I am doing wrong...??? Please remember I have to use it as recursive as I have many folders inside the main folder too.
The fourth bird has provided the crucial pointer in a comment:
You mistakenly \
-escaped the following regex metacharacters in your regex, which causes them to be matched as literals: (
, )
, and |
. Simply omitting the \
would work.
Conversely, you neglected to escape .
as \.
, given that you want it to be interpreted literally.
However, I suggest the following optimization, which pre-filters the files of interest and then matches only against each pre-filtered file's base name (.BaseName
), i.e. the name without its extension:
Get-ChildItem -Recurse $Path -Include *.jpg, *.jpeg, *.png |
Where-Object { $_.BaseName -match '-[0-9]+x[0-9]+$' } |
Remove-Item -WhatIf
Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
once you're sure the operation will do what you want.
Note:
The regex above needs no .*
prefix, given that PowerShell's -match
operator looks for substrings by default.
\d
in lieu of [0-9]
is an option, although \d
technically also matches digits other than the ASCII-range characters 0
through 9
, namely anything that the Unicode standard classifies as a digit.While use of -Include
, which (unlike -Filter
) conveniently allows you to specify multiple PowerShell wildcard patterns, works as expected in combination with -Recurse
, in the absence of -Recurse
its behavior is counterintuitive: