0

私はMacOSXLionを使用していますが、仮想マシンにWindows XPがあるので、Windowsのソリューションにも満足しています。

次のように、ファイル名にUNIXタイムスタンプが含まれるファイルが何百もあります。

1341131403_-_db123456.sql.gz
1341390599_-_db123456.sql.gz
1341563401_-_db123456.sql.gz

そして、タイムスタンプを読み取り可能なタイムスタンプに変換し、ファイルの名前をその読み取り可能なタイムスタンプで次のように変更したいと思います。

2012-07-01 08-30-03.sql.gz
2012-07-04 08-29-59.sql.gz
2012-07-06 08-30-01.sql.gz

私はAppleScriptソリューションに数時間を費やしましたが、成功しませんでした。

on open (the_input)
tell application "Finder"
set the_files to every item of the_input
set the_count to count of the_files
repeat with i from 1 to the_count
set current_file to item i of the_files
set old_name to (name of current_file) as string
set old_name to trim_line(old_name, "_-_db123456. sql. gz", 1)
set new_name to (result of uts_convert(old_name)) as string
set the name of current_file to (new_name & file type of current_file)
end repeat
end tell
end open

on uts_convert(input)
set shellcommand1 to "date -r "
set shellcommand2 to " \"+%Y-%m-%d %H-%M\""
set the_output to do shell script (shellcommand1 & input & shellcommand2)
return the_output
end uts_convert

どんな助けでも大歓迎です!それがapplescriptや単純なターミナルコマンドなどで行われるかどうかは気にしません。

前もって感謝します!

4

2 に答える 2

0

試す:

tell application "Finder" to set theFiles to every file of folder ((path to desktop) & "testfolder" as text)
set {TID, text item delimiters} to {text item delimiters, "_-_"}
repeat with aFile in theFiles
    set timestamp to text item 1 of (name of aFile as text)
    set newName to do shell script "date -r " & timestamp & " '+%Y-%m-%d %H-%M-%S'"
    set aFile's name to (newName & ".sql.gz")
end repeat
set text item delimiters to TID
于 2012-07-09T05:03:48.223 に答える
0

簡単な Perl ソリューションを次に示します。

opendir($dir, ".");
while ($f = readdir($dir)) {
    if ($f =~ m/^(\d+)_-_db\d+\.sql\.gz/) {
        ($s, $i, $h, $d, $m, $y) = gmtime($1);
        rename($f, sprintf("%4d-%02d-%02d %02d-%02d-%02d.sql.gz", $y + 1900, $m + 1, $d, $h, $i, $s));
    }
}
closedir($dir);

それを renamethings.pl またはファイルと同じフォルダーに必要なものに保存し、次を実行します。

perl renamethings.pl

何か問題が発生した場合に備えて、最初にファイルのバックアップを作成することをお勧めします。

于 2012-07-08T23:25:45.860 に答える