开发者

rename many files automatic one time - ddmmyyyy.file to yyyymmdd.file

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 开发者_StackOverflow(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


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 );

}


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


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...)


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');
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜