logo

Batch Renaming Files With Windows PowerShell

Every now and then I need to batch resize a load of photos before FTPing to a website. I normally do this with my photo viewer of choice. At the moment that means using Windows Live Photo Gallery (WLPG).

WLPG has an annoying habit of renaming the resized file though. If you resize "IMG_123.jpg" it will become "IMG_123 (1024x768).jpg". This is understandable when the target folder is the same as the original image, but even when there's no file name conflict it still does it.

When there's a dozen or so images I manually rename the files to remove the size data. Today though there were way too many to do that so I decided to see if Windows PowerShell could do it.

I'd never used PowerShell before but was sure it would be able to do the job. It's like the Command Prompt, but better. Press Start and type "power shell" in to the search to find it. It looks like this:

image

After a bit of digging around I found that I could do what I wanted to using the following command:

ls *.jpg|rename-item -newname {$_.Name -replace " \([0-9]{2,4}x[0-9]{2,4}\)", ""}

Hey presto!

Comments

    • avatar
    • Richard Shergold
    • Thu 28 Apr 2011 10:17 AM

    http://www.irfanview.com/ is worth a look for this sort of thing (and loads of other things). I think it has plenty of file rename (or overwrite) options.

    • avatar
    • Aaron
    • Thu 28 Apr 2011 10:31 AM

    Not bad. I find using a shell faster in most instances rather than using the GUI, at least for Administration tasks.

    Thanks

    • avatar
    • NoAxeToGrind
    • Thu 28 Apr 2011 10:50 AM

    Commands like "ls", windows power shell, let's just call it unix and be done with it =D

  1. We moved our student email to MS Live@Edu last yr. Been using Windows Powershell in conjunction with SSIS (SQL Server Integration Services) for the last year to automate jobs like keeping passwords in sync, updating student information, creating new mailboxes, groups etc.

    Very powerful stuff.

    Dan

    • avatar
    • Jason Boardman
    • Thu 28 Apr 2011 11:09 AM

    There's also this little utility...

    http://www.bulkrenameutility.co.uk/Main_Intro.php

    • avatar
    • Jorge Coelho
    • Thu 28 Apr 2011 11:43 AM

    I had the need for resizing images and the graphics tools just weren't cutting it, so I went looking for a tool about 9 months ago. I found Image Resizer, which adds a right-click option for resizing one or multiple images. You can pick predefined resolutions or enter your own. You can also select to create new copies or overlay the existing. It's a nice addition to my toolset and it's free.

    http://imageresizer.codeplex.com/

  2. PowerShell is a great tool, especially due to its OO capabilities. But using it is not too easy: The syntax is as weird as RegEx and every now and then when I need it I have to squeeze my brain in order to get the result that I am looking for...

    I just found a script of mine which I wrote a year ago. If I only knew what it does ;-)

    1. This was the whole script:

      Get-ChildItem -Path 'C:\01 mte\*\screens\gui*' -Recurse | ft fullName > 'C:\01 mte\guititles.csv'

      It came in very handy in the following situation: Using a tool I was able to download all custom source code from an SAP system to the file system. This was done by creating single files for every aspect of the ABAP code in a complicated file structure. Now I was searching only for a special kind of file in thousands of nested folders. Using the above script did the trick, it wrote all the relevant file names into a single csv file.

      For tasks like these you won't find a special utility easily. Therefore it is worth spending half an hour to code just one line of PowerShell script code! (Even better would be to ask a PowerShell geek who writes down the one liner instantly...)

  3. Have a look at imagemagick - it is made for Linux, but runs on windows too.

    http://www.imagemagick.org

    Samples for converting

    http://www.imagemagick.org/script/convert.php

  4. I have another problem. We would like to invoke Powershell commands från Java (or a Java agent in Lotus). Can't find any thing useful on the Internet. Has anyone done this and if so, How?

  5. So simply,

    For files, Rename-it : https://sourceforge.net/projects/renameit/

    For mp3s, i have definitely choosen : http://www.jtclipper.eu/thegodfather/

    AND think one thing BEFORE upload yours 80Go of photos & videos to your PS3 : you can't sort your files by folder, only files names are allowed in PS3 menus. So sorry :)

  6. Or from the cmd window you could have done:

    ren *.jpg ???????.jpg

    Given the file name you gave in the post of IMG_123.jpg being changed to IMG_123 (1024x768).jpg. I use photoshop to batch create thumbnails and resized images, but use my own HTML for a photo gallery page. Photoshop doesn't rename the files but puts them in separate directories and I need the thumbnails to be *tb.jpg and the images to be *lg.jpg and in the same directory. The images are named CIMG1234.jpg I use the following for the thumbnails.

    ren *.jpg ????????tb.jpg

    And for the images.

    ren *.jpg ????????lg.jpg

    I then move the files to the gallery folder and I'm done. Sometimes old DOS can do the job, though if I was really ambitious, I would rewrite the photoshop scripts.

    • avatar
    • Eric M
    • Fri 29 Apr 2011 05:32 PM

    +1 for IrfanView. I've used it for years and it's fantastic.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 28 Apr 2011

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content