Tuesday 17 May 2016

How To Rename Multiple Files with Windows Powershell #TipsTuesdays 28


Greetings and welcome to my blog on this Tuesday. Just recently I have been sorting out my file system, and while Windows is not bad at it, the usual interface is just not powerful enough. Then I found Powershell.

How To Rename Multiple Files with Windows Powershell

Now I suspect many of us look at Powershell, which is a command line interface, and shudder a bit. I have only rarely used such a thing since I finished my PhD more years ago than I care to admit. Let's just say when I started Uni we were using green screen terminals with UNIX and leave it at that :).

We have lots and lots of image files and the like on our NAS box and the filenames were all over the place. Now had I just wanted them to be the same name with a number, Windows could have done it easily, but:
  1. I'd have had to do each directory individually.
  2. I wanted to keep parts of the names as they were.
So I asked Google how to do it and it said Powershell was the answer.

Now it's all well and good to check out the info on Powershell, but it's somewhat complex. I have a degree in Computer Science and some of it still made me go 'huh'. Hence I thought I would just give a few examples.

First, a couple of Provisos

  1. Always check your Filter to make sure you are only editing the names of the files you really want to (this will make sense in a moment :)).
  2. It's a good idea to test your change on a few isolated files before running it on large numbers just to make sure you haven't done something stupid (I managed to rename a whole bunch of files to something unintelligible at least once :)).
  3. If you have mangled a command and want to get rid of it without having to delete it all, using the <esc> key will wipe it from the command line.
  4. If a command is running and you wish to stop it (for example if it is taking too long or you've realised you've made a mistake) then hold down the <ctrl> key and click the letter c.

To Open Powershell

  1. Right click on the start icon.
  2. Choose "command prompt"
  3. Type Powershell

To Setup the Look of Powershell

I found the text too small when I first started Powershell, but this is easy to remedy.
  1. Right click on the bar at the top of the window.
  2. Choose "Properties"
  3. Use the tabs to change whichever elements you wish to.
  4. Click OK

Navigating in Poweshell

If you have multiple drives you may need to use <drive letter>: to change to the correct drive. For example our Photos are on W: as you can see in the image below.

To reach the correct directory (folder) you use the command cd (change directory):

cd temp - will take you to the subdirectory called temp.

If you're not sure of the directory names you can use dir to list everything in your current position.

Or you can do cd then a <space> then use <tab> to cycle through all the sub-directories of the directory you are in.

To go back up a directory type cd ..


Add A Number to the Beginning of Each Filename

This is the one I found most useful. You know when you have a whole directory of files and you need to number them sequentially, but the filename has other information you don't want to lose?

For example, say you have all the episodes of series and part of the filename is the title of the episode, but you also want an episode number so you don't have to use the date to sort them to see them in order.

This is the script you need (and Powershell does allow you to just paste it in):

$originalFiles = Get-ChildItem .\ -Filter *.mp4 | sort -Property LastWriteTime
$x = 1
ForEach ($originalFile in $originalFiles) {
    Rename-Item -Path $originalFile.FullName -NewName (($originalFile.Directory.FullName) + "\" + $x + ' ' + $originalFile.Name)
    $x++
}

This should only be run in the directory where you wish to rename the files.
  • $originalFiles is set to all the files you wish to rename using Get-ChildItem.
  • Change the -Filter *.mp4 to whatever the files you wish to rename are e.g. -Filter *.jpg would bring back all files with the jpg extension, -Filter *.avi as the avis etc.
  • sort -Property LastWriteTime sorts the files in date order - but you can sort them by any of the properties that show up when you use dir, e.g. Length or Name as well.
  • The loop then goes through each file $originalFile of the files you selected into $originalFiles.
  • $x is the counter and $x++ adds one to it each time it goes through the loop.
  • This version adds a number $x and a space ' ' to the beginning of the filename. If you wanted to add more, for example, something to indicate that the number is the episode number you could replace $x + ' ' with 'EP' + $x + ' ' or 'SeriesName EP' + $x + ' ' etc 

Partially Rename All the Files in A Directory

You might wish to change something in a whole string of filenames. For example, say the name of your favourite artist was spelt wrong in a whole bunch of files, you could correct it in one go.

Get-ChildItem .\ *.mp3 | Rename-Item -NewName { $_.name -replace "Adnam Lambert", "Adam Lambert" }

This will go through all the mp3 files in a directory and change Adnam Lambert to Adam Lambert, while leaving the rest of each filename intact.

If you had a whole heap of files in different directories with the same mistake you can go through all of them by sitting in the top directory and running this command:

Get-ChildItem .\ -recurse -include *.mp3 | Rename-Item -NewName { $_.name -replace "Adnam Lambert", "Adam Lambert" }

Powershell is incredibly powerful and there are lots of sites that can tell you way more about it than this little post. Here are a few I used to become familiar with it:

If you have any questions, please feel free to ask. I don't guarantee I can answer them, but I'm willing to try :).



6 comments:

  1. Hi Natasha - way above my head ... but congratulations on being able to comprehend what's going on - and what a great PhD degree to have in this day and age ... cheers Hilary

    ReplyDelete
    Replies
    1. LOL - I think I am glad I really started computers with command line stuff, because it can be so useful at times. Most of the documentation is incomprehensible though - hence I used Google a lot when trying to figure it out.

      Delete
  2. I am very impressed! I'm not sure I follow but that is my fault, not yours!

    @Kathleen01930
    Blog

    ReplyDelete
    Replies
    1. It's probably useless to many people, but I got excited about it and have been using it constantly so had to talk about it ;)

      Delete
  3. Another computer novice, who will probably stick to the single file approach. Unless I am feeling brave. Thanks for the info.

    ReplyDelete
    Replies
    1. No probs - it's a bit obscure, but one of those things you never know if you might need one day :)

      Delete

Thank you so much for reading. I love to hear from people. Please leave your comments below.