Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

Powershell Batch Rename

Powershell Batch Rename

  • Comments 8

I keep forcing myself to use PowerShell from time to time as I know it'll pay big dividends if I just get over the initial (steep) learning curve. So, faced with the task of renaming some files in a folder today I turned to PowerShell. About an hour later I was still staring at the same set of files with the same names. I'd have been quicker using some sort of disk sector editing tool at the rate I was going.

I found plenty of help on the web - or at least plenty of suggestions for renaming and batch renaming. But everything I tried gave me an error - either that I was trying to call a method on a null valued expression or that the path didn't exist (oh yes it does).

After much mucking around, it turned out this was due to the presence of [] characters in the filename which also happen to be wildcard characters. And the rename-item cmdlet that I was trying to use sees them as a wildcard and as far as I can see there's no way to escape them. The solution is to use move-item instead which allows you to specify -literal and have square brackets mean just that. I ended up with:

dir *_t*.gif | foreach { move-item -literal $_ $_.Name.Replace("_thumb[1]", "")}

If only I'd known what to search for I could probably have found this post first and saved myself some hair. I like PowerShell but I don't find any of it very intuitive. [Update: And I just realised that post is by Adrian Bateman who works at Microsoft in the UK]

3/10 - Must try harder (for me, not PowerShell).

Technorati Tags:
  • If your are renaming pictures, try creating a folder in the pictures folder on Vista, then play with the powerful renaming abilities, playing around with them, with a few files, you can get very creative renaming for all files in folder. Select all you wish to rename and enter a master renaming on the first and each of the rest of the fold will be renamed with a sequence indicator automatically indicated, just a thought, and yes PowerShell does have a ateep Learning Curve -- Great post as to your final solution

  • Thanks JCH3 - I just tried that and it works a treat. I usually use IrfanView for anything like that as it has very flexible batch capabilities. That said I now know Vista can do the rename too. :-)

    Of course neither of these is as much fun as trying to do it in PowerShell... Mike

  • Poor Mike - perhaps he should have used PowerGUI . I have been catching up with Mike Ormond's blog

  • jQuery MooTools 1.2 Beta 2 Released, Adds New Element Storage jQuery, Facebox and AJAX Agile The magic

  • I was messing about with PowerShell the other day (when I ran into my batch rename problems ) having

  • I was messing about with PowerShell the other day (when I ran into my batch rename problems ) having

  • in reply to dennis' comment on my last post on this little thing ( http://drowningintechnicaldebt

Page 1 of 1 (8 items)