1

I have a folder of images taken on two different mobile phones. The two phones use slightly different file name formats. I want to change one of them to match the other so I have a neat photo album.

How can I change a file name from 20110809_121158.jpg to 2011-08-09 12.11.58.jpg?

It will be great if the solution comes by using Powershell but any other option is also welcome!

1

1 Answer 1

1

Here's one way to do it, using SubString:

function getNewFileName($fname)
{
    $year = $fname.SubString(0,4)
    $month = $fname.SubString(4,2)
    $day = $fname.SubString(6,2)
    $hours = $fname.SubString(9,2)
    $minutes = $fname.SubString(11,2)
    $seconds = $fname.SubString(13,2)
    $new_fname = [string]::Format( "{0}-{1}-{2} {3}.{4}.{5}.jpg", $year,$month,$day,$hours,$minutes,$seconds) 
    return $new_fname;
}

To use this to rename a selection of file entries in a folder, you can use something like this:

$fileEntries = Get-ChildItem *_* -name 

foreach($fileName in $fileEntries) 
{ 
    $newFileName = getNewFileName($fileName)
    Write-Host -NoNewLine Rename-Item $filename $newFileName 
    Write-Host
}         

As written, this script won't actually change anything - it lets you preview what would change by outputting the proposed rename command to the console. To actually rename the files you'd have to remove the "Write-Host" statements. This script assumes that all you have in the folder are the two types of file names you mentioned.

    $fileEntries = Get-ChildItem *_* -name 

Selects only files containing an underscore so we don't try to rename the other type of file. This script is meant to run in the subject folder; running it elsewhere might do strange things to the wrong files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .