Windows

Removing same part of the file in Windows

Once upon the time… All right, it was yesterday. I had to clean up some site, than someone build long time ago. There was an image library, it contains hundreds of images. And it is ok, but all of the images contains “image_” prefix. And all of the images are in /images folder. Unnecessary redundancy I though to myself and I decided to remove “image_” prefix for all image files. And I almost open PHP editor to write short script…

And then I thought – damn, we’ve got shell here, it’s Windows! So I opened Windows PowerShell and wrote something like this:

PS C:\server\sites\old-site\images> get-childitem | foreach { rename-item $_ $_.Name.Replace("image_", "") }

I was sure, that all files in the directory are images. You may use this code to rename only JPG files like that:


PS C:\server\sites\old-site\images> get-childitem *.jpg | foreach { rename-item $_ $_.Name.Replace("image_", "") }

Now it’s up to you, what you do with that code snippet! Be careful! And as always, have fun.

One Comment

Leave a Reply to Andre Cancel reply

Your email address will not be published. Required fields are marked *