Fun with scripting shells (how to rename multiple files in PowerShell).

I stumbled across this post by Rob Newcater on RootPrompt.org which points to this post on BasicallyTech.com. It shows the various ways that you can rename or manipulate filenames in groups using the bash shell and a smattering of standard UNIX utilities.

The sample given was renaming a directory full of *.mp3 files such that all the spaces were replaced with underscores – here is the script in BASH:

for FILE in *.mp3 ; do NEWFILE=`echo $FILE | sed ‘s/ /_/g’` ; echo “$FILE will be renamed as $NEWFILE” ; done

That is pretty cool, although it is much more concise in PowerShell:

get-childitem *.mp3 | foreach { rename-item $_ $_.Name.Replace(” “, “_”) }

And by concise, I don’t mean shorter. One of the advantages of PowerShell over traditional text-piping shells is that you are dealing with objects which provide a lot more data to the next part of the processing pipeline. If you are into punctuation you could express it as:

gci *.mp3 | % { rename-item $_ $_.Name.Replace(” “, “_”) }

But to be honest, I prefer it the first way.

8 thoughts on “Fun with scripting shells (how to rename multiple files in PowerShell).

  1. /\/\o\/\/

    a bit late but TIMTOWTDI

    dir *.mp3 | ren -n {$_.name -replace ” “,”_”} -whatif

    and -WhatIF beats the Echo line added in the bash example I think😉

    PoSH>dir *.mp3 | ren -n {$_.name -replace ” “,”_”} -whatif
    What if: Performing operation “Rename File” on Target “Item: C:\PowerShell\foo bar.mp3 Destination: C:\PowerShell\foo_bar.mp3”.

    Greetings /\/\o\/\/

  2. xxx yyy

    The bash example is overly complex. The following line does the job as well, is easier to read, and handles all kinds of special characters (unlike PowerShell🙂

    for file in *.mp3; do mv “$file” “${file// /_}”; done

  3. Pnajafi

    Hi All, when I try:

    get-childitem *.html | foreach { rename-item $_ $_.Name.Replace(” zzz“, “ddd”) }

    and press ‘enter’, the console does nothing and goes to the next prompt line but with the symbol ‘>>’ as prompt line. As if this is a special mode of the environment.

    But when I try:
    gci *.html | % { rename-item $_ $_.Name.Replace(” zzz“, “ddd”) }

    It works. Why is that?

  4. Benjamin Blais

    That’s a bad example of renaming files in bash, first of all you are having it print everything you do (the unnecessary echo commands), second the rename command could do the same thing in one line:

    rename \ – *

    This accomplishes everything that the ridiculously long line had as well as your powershell command.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s