Welcome to MSDN Blogs Sign in | Join | Help

Mike Ormond's Blog

In my world, things would be simpler than this...

News

  • Add to Technorati Favorites

    These postings are provided "AS IS" with no warranties, and confer no rights. The use of any script / code samples is subject to the terms specified here.

Powershell Batch Rename

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:
Posted: Sunday, January 20, 2008 11:09 PM by MikeOrmond
Filed under:

Comments

JCH3 said:

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

# January 20, 2008 6:53 PM

MikeOrmond said:

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

# January 20, 2008 7:07 PM

Tim Long said:

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

# January 21, 2008 8:03 AM

Jason Haley said:

# January 21, 2008 9:41 AM

Trumpi's blog said:

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

# January 23, 2008 2:03 PM

Mike Ormond's Blog said:

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

# January 25, 2008 2:54 AM

Noticias externas said:

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

# January 25, 2008 3:46 AM

roy ashbrook said:

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

# May 18, 2009 5:34 PM
New Comments to this post are disabled
Page view tracker