This can easily be modified to work with any kind of file, but I was only concerned about my video camera files. if you want change the <*.MTS *.m4v> to whatever file types you want!
Here is the code:
#!/usr/bin/perl
# Written by Tyson Bailey
# with date/string creation from: http://www.seto.org/mt- diary/archives/2005/11/using_perl_to_d.html
use File::stat;
use Time::localtime;
@files = <*.MTS *m4v>;
$extension = "";
foreach $file (@files)
{
# get date/time
$date_string = ctime(stat($file)->mtime);
# break it up into an array.
@date = split('\s+',$date_string);
$i = 0;
# figure out file extension
if ($file =~ m/.m4v$/)
{
$extension = "m4v";
}
else
{
$extension = "MTS";
}
# Filename format MAR_05_2011_x.extension
while (-e "@date[1]_@date[2]_@date[4]_$i.$extension")
{
$i++;
}
# just to be safe
$base_name = quotemeta("@date[1]_@date[2]_@date[4]_");
chomp($file);
# if our filename already exists we really don't want to try renaming it.
if ($file !~ m/$base_name/i)
{
print "Didn't Matched: $base_name\n";
print "Renaming: " . $file . "\n";
print "To: @date[1]_@date[2]_@date[4]_$i.$extension\n";
rename("$file", "@date[1]_@date[2]_@date[4]_$i.$extension");
}
else
{
print "Not Renaming: $file\n";
}
}
Big thanks to:
http://formatmysourcecode.blogspot.com/
for formatting my source code!