Link to home
Create AccountLog in
Avatar of vodyanoi
vodyanoi

asked on

Powershell script to convert case in filenames

Hello,

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.

examples

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

regards


Spencer





ASKER CERTIFIED SOLUTION
Avatar of KenMcF
KenMcF
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of vodyanoi
vodyanoi

ASKER

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

Avatar of GusGallows
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

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
$source="C:\temp\test"
# ensure the following folder exists
$temp="C:\temp\temp"
# 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 } 
$i=1
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;
$i++;
}

Open in new window