Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
stackoverflow.com/questions/5574648/… –  crb Mar 26 '13 at 17:03

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.