views:

387

answers:

6

Hello

I have 1500 files that are named with an incorrectly dateformat. I would like to rename them. Are there a tool that can do that? Otherwise a piece of php code.

File names are: ddmmyyyy.xls (e.g. 15012010 for 15.th Jan 2010)

and I would like: yyyymmdd.xls (e.g. 20100115.xls)

Any clue on how this can be done for 1500 files in one go?

BR. Anders


UPDATE: Also tried the MP3TAG, that is suggested in one of the answers. It is a free tool and also did the job. It took a while to figure out how to use it. If you wanne try do this:

  1. add xls (or other format) to the list of editable files in configuration
  2. choose folder to load files AND mark files in the pane you want to edit
  3. I clicked the "Convert - Quick" button. It is also possible to save schemaes for future use but I could not figure out how.
  4. after clicking "convert - quick" choose "using regex" (only regex option)

And then you just add the info to process the renaming. In my case:

  • field: _FILENAME
  • from: ([0-9]{2})([0-9]{2})([0-9]{4})
  • to: $3-$2-$1

Now all files named 15012010.xls (ddmmyyyy.xls) will be named 2010-01-15.xls

+3  A: 

Here's a start (untested, but you should get the idea).

$files = glob('your/folder/*.xls');

foreach($files as $file) {

    preg_match_all('/^(\d{2})(\d{2})(\d{4})\.xls$/', basename($file), $matches);

    if ( ! $matches) continue;        

    $year = $matches[0][3];
    $month = $matches[0][2];
    $day = $matches[0][1];

    $newFilename = $year . $month . $day . '.xls'; 
    rename  ( $file, dirname($file) . '/' . $newFilename );

}
alex
Regex should be `/^(\d{2})(\d{2})(\d{4})\.xls$/i`, no?
Alix Axel
@Alix Ah yes! Friday here...
alex
Thanks. All files have been renamed succesfully.
Tillebeck
Wouldn't this be dangerous if you had, say, files from 20th September 2001 and 20th January 2009 in the same folder to convert? (i.e. you'd attempt to rename one to a name that already exists and fail/overwrite). Better would probably be to an intermediate extension that isn't used in that folder, then a grand rename at the end...
Damien_The_Unbeliever
@Damien - yeh, you could do all that, but if you knew your input files were good (and weren't going to overwrite anything) you could get away with this. This was meant to be a quick solution for a once off problem!
alex
+1  A: 

If you have a Linux machine with the files... you can use bash to do:

for f in *.xls; do
    mv $f "$(echo $f | cut -c4-8)$(echo $f | cut -c3,4)$(echo $f | cut -c1,2).xls"
done
clee
+1. Also notable, can be done with a Mac running Mac OS or a windows machine running Cygwin.
Eddie Parker
No need for that many externals if using bash.
ghostdog74
A: 

A tool that can perform filename pattern conversion is Mp3tag.

Choose convert and then filename - filename.

I'm sure there's other tools out there too!

(This answer isn't really in the StackOverflow spirit but I think the OP isn't necessarily looking for an automated solution...)

Pool
The goal is not to convert XLS files... to XLS files but rename it.
OcuS
Mp3tag is a free tool for batch renaming files by a file pattern (among other tools) which is what I believe the the goal was.
Pool
Thanks, I tried it in the weekend and it worked just fine. Updated my question with a short quick-start guide.
Tillebeck
A: 

Based on alex function, but this one correctly adds the .xls extension.

foreach (glob('/path/to/your/*.xls') as $file)
{
    rename($file, dirname($file) . '/' . substr(basename($file), 4, 4) . substr(basename($file), 2, 2) . substr(basename($file), 0, 2) . '.xls');
}
Alix Axel
Hey mine does add the .xls extension!
alex
@alex: You edited it: `$newFilename = $year . $month . $day;` you only had this.
Alix Axel
Sorry, yeh I did originally. I made quite a few edits because I forgot things!
alex
A: 

if you have bash

#!/bin/bash

shopt -s nullglob
for xls in [0-9]*.xls
do
    day=${xls:0:2}
    mth=${xls:3:2}
    yr=${xls:4:4}
    echo mv "$xls" "${yr}${mth}${day}.xls"
done

no need external tools.

ghostdog74
A: 

File names are: ddmmyyyy.xls (e.g. 15012010 for 15.th Jan 2010)

and I would like: yyyymmdd.xls (e.g. 20100115.xls)

Use this script.

# Script RenameYYYYMMDD.txt
var str dir, list, file, newname, dd, mm
lf -r -n "*.xls" $dir > $list
while ($list <> "")
do
    lex "1" $list > $file ; stex -p "^/^l[" $file > $newname ; chex "2]" $newname > $dd
    chex "2]" $newname > $mm ; sin "^.^l" ($mm+$dd) $newname > null
    system rename ("\""+$file+"\"") $newname
done

This script is in biterscripting ( http://www.biterscripting.com ). Test the script first in a test folder.

To test, save the script code in file "C:/Scripts/RenameYYYYMMDD.txt", and enter the following command.

script "C:/Scripts/RenameYYYYMMDD.txt" dir("C:/path/to/test folder")

This command will rename all files ddmmyyyy.xls under directory "C:/path/to/test folder" to yyyymmdd.xls.

P M