Fixing A File Extension -By Edward Thomas

So we go to look at some pictures from 2008 and that great time we had with our friends but something is wrong with the pictures.
You know that they are jpeg file but when you click on them….

 

 

 

You get this popup asking you what you want to open the file with.
That is not right, you think. You look at it again and you see for some odd reason some, not all, are missing the file type information.
That the part at the end of the file name that tells Windows what kind of file it is.

 

 

 

 

You rename one of the files with the .jpeg at the end of the file name.

Yep it works!

Okay this is great but there is a lot files to fix in the folder and you do not have all day to muck about with rename and fix this problem.

 

 

 

Enter PowerShell!

One of my favorite tool when work with Windows and do my day job administrating systems.
So what can we do to save the day with PowerShell in one line of code?

 

 

 

Well, let’s break this into step so we know what is going into that one line of code.
First we know that we need to get all the file in our folder and then do something to each item in that folder.

Enter the command-let Get-ChildItem and all it’s power!
Okay what it does grab all the items in the folders and spit that out on the screen as a bunch of information about each file in the folder. (Reminder, if you have space in your folder path add quote marks to each end of the path.) But this is PowerShell and we know we can do things to each of those things, those objects.

 

 

We add a pipe sign | and add the ForEach-Object{} command-let.
So what the pipe does is take all those object we just made and passes those too something else.
In this case we are going to do something to each and every object so we user the ForEach-Object command-let.

 

 

Okay so what are we going to do to each of those file?
Rename them with addition on the .jpeg affixed to the end of each and every one of the files that do not already have .jpeg at the end.

Enter command-let Rename-Item and we need two things in this case.
One the Path of the file and two the NewName. So we add -Path and $_.FullPath to give us the full path of one of the files we are working with.

($_ is used by the ForEach-Object as a place holder for one of the object we past in)

 

The next thing we need to do is add -NewName to tell the command-let what to name the file. We are adding .jpeg to the end of each file, so we add “$_.jpeg” to it. Now if we hit enter PowerShell would do it’s thing and add .jpeg to each file but wait didn’t we say that some of those files already had the .jpeg at the end. Yep!
If we hit enter it would mess-up those file names that already have .jpeg at the end.
The files would work but looks odd. To save us from this we do two things.

Number one, add the -WhatIf to the end of the last command-let so you can see what is going to happen before it happens. Read through the out put and make sure that things are right and then take the -WhatIf off.

 

 

 

Numer two, let’s go back to the first command-let and add something to the command-let Get-ChildItem. At the end of it but before the pipe | add -Exclude “*.jpeg” this will take any file with the .jpeg and we will not be fixing our fix.

 

 

If you have other types of file that you need to exclude this is the place to do it. Just add a comma and the name of the thing you need to exclude. Like all the PDF files in the folder or any other type of files see there that does not need changing.

 

 

 

Now you are ready to go. Press enter.

All the files are awesome once more! And you can get on with remembering that fun day with your friend in 2008.

 

 

 

 

 

 


Get-ChildItem -Path “Enter Path Here” -Exclude “*.jpeg” | ForEach-Object{Rename-Item -Path $_.FullName -NewName “$_.jpeg” -WhatIf}