Do you have some WordPress media gallery in your windows pc or have some images with different names in your windows pc folder and then know how to delete multiple files after matching specific name patterns through regex using CMD/PowerShell in windows PC with keeping all nester folders search too?
There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.
Table of Contents
Features:
- Windows Power Shell Common.
- Single Line With Full Customization.
- Remove WordPress Formated Extra Images.
Example:
- first-image-name.jpg
- first-image-name-72×72.jpg
- first-image-name-150×150.jpg
- first-image-name-250×250.jpg
- first-image-name-300×300.jpg
- first-image-name-400×400.jpg
- first-image-name-1024×1024.jpg
- second-image-name.png
- second-image-name-72×72.png
- second-image-name-150×150.png
- second-image-name-250×250.png
- second-image-name-300×300.png
- second-image-name-400×400.png
- second-image-name-1024×1024.png
How To Delete Multiple Files After Matching Through REGEX Using PowerShell In Windows?
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.
- Using character class \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:
Customization:
No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
Troubleshooting the Errors:
Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.
Final Words:
That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it, and leave your precious feedback in our comment form below. Happy development, See you in the next article.
Such a nice post is given by you. explained very well with proper image. i got a idea from the post and improve my knowledge and keep tough with us
Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you with more awesome and valuable content from a different mind. Thanks again.