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: powershell
Comments
Anonymous
January 20, 2008
The comment has been removedAnonymous
January 20, 2008
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... MikeAnonymous
January 21, 2008
Poor Mike - perhaps he should have used PowerGUI . I have been catching up with Mike Ormond's blogAnonymous
January 23, 2008
jQuery MooTools 1.2 Beta 2 Released, Adds New Element Storage jQuery, Facebox and AJAX Agile The magicAnonymous
January 24, 2008
I was messing about with PowerShell the other day (when I ran into my batch rename problems ) havingAnonymous
January 24, 2008
I was messing about with PowerShell the other day (when I ran into my batch rename problems ) havingAnonymous
May 18, 2009
in reply to dennis' comment on my last post on this little thing ( http://drowningintechnicaldebt