Powershell script to convert case in filenames


I've had a look around and cannot see an easy way to achieve the following.

We need to be able to recursivly search a folder and convert all the filenames to uppercase, and convert the extension to lower case.


From: abc123.jpg   To: ABC123.jpg
From: abc123.JPG   To: ABC123.jpg
From: Abc123.PNG   To: ABC123.png
From: ABC123.GIF   To: ABC123.gif

basic scripts involving ToUppper() and ToLower() fail with a response that
Rename-Item : Source and destination path must be different.

e.g. test script for single file

Get-ChildItem -Path "C:\temp\testfile.jpg" | % {Rename-Item $_.FullName -NewName ([string]$_.Name).ToUpper()};

I'm sure I am missing something obvious but cannot see what



Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you getting this error whne reanming the single file?

You can do this several ways try this

Get-ChildItem "C:\temp\*"  -included *.jpg -recurse | % {Rename-Item $_.FullName -NewName ([string]$_.Name).ToUpper()}

Get-ChildItem "C:\temp\*.jpg" -recurse | % {Rename-Item $_.FullName -NewName ([string]$_.Name).ToUpper()}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vodyanoiAuthor Commented:
Yes they work,

thanks very much
Try this:

dir C:\temp -r | % { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

Open in new window

The problem is, the script is seeing abc123.jpg and ABC123.jpg as the same thing since windows is case insensitive when it comes to file names.

A workaround that might work for you would be to actually do two changes. One that changes the name to something else, and then another that changes it back with the proper case. For instance:
#capture the path to a variable
$path = "C:\temp\"
#capture the old file name
$oldfn = "testfile.jpg"
#capture the full path/filename
$oldfullpath = $path + $fn

#capture the temporary new filename and then full path/filename
$newtempfn = "testfile.jpg1"
$newfullpath = $path + $newtempfn

#break up the filename from it's extension
$split = $oldfn.split(".")

#convert filename to uppercase
$fn = $split[0]
$fn = $fn.ToUpper()
#convert extension to lowercase
$ext = $split[1]
$ext = $ext.ToLower()
#put them back together
$newfn = $fn + "." + $ext

Get-ChildItem -Path $fullpath | % {Rename-Item $_.FullName $newtempfn };
Get-ChildItem -Path $newfullpath | % {Rename-Item $_.FullName $newfn };

Open in new window

vodyanoiAuthor Commented:
Just for completeness, and in case it is of use to anyone else, here is the solution I finally came up with.

The only shortfall is it doesn't work with filenames continaing []  

#script to change case of files - names to upper case, extensions to lower case
# definitions
# folder you wish to work on
# ensure the following folder exists
# begin process - grab list of file names, filtered for pictures in this case, excluding directories
$result = dir -r $source -include *.jpg, *.jpeg, *.bmp, *.png | where { ! $_.PSIsContainer } 
foreach ($name in $result)
$origin = % {$name.FullName};
$path = % {$name.DirectoryName};
$base = [System.IO.Path]::GetFileNamewithoutextension($name); 
$ext = [System.IO.Path]::GetExtension($name); 
$filename = % {$base.ToUpper()}; 
$end = % {$ext.ToLower()};
$middle = $temp + "\" + $base + $ext;
$final = $path + "\" + $filename + $ext;
Move-item $origin $temp;
Move-Item $middle $final;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.