ManicMantis has asked for the wisdom of the Perl Monks concerning the following question:
Ok. I'm brand new to Perl and I'm hoping someone could assist me with what appears to be an overly ambitious project for a beginner. I'm running Windows and I want to automate the task of renaming a mass of log files based on their last modified date, a string from within the file, and a number to prevent duplicates, in the following format:
'string, date, number.log'
I would appreciate any help at all, from a push in the right direction to code snippets to whatever. I want to learn this, but I'm rather stuck at the moment. Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Modified Date, file renaming
by azatoth (Curate) on May 16, 2001 at 15:07 UTC | |
Also, I would look into getting Learning Perl on Win32 Systems, by our very own merlyn. Azatoth a.k.a Captain Whiplash Make Your Die Messages Full of Wisdom! Get YOUR PerlMonks Stagename here! Want to speak like a Londoner? P.S How could I forget this : File Attributes in Win32. Check it out for some tips on manipulating files / directories on Win32 Systems. | [reply] |
|
Re: Modified Date, file renaming
by Mungbeans (Pilgrim) on May 16, 2001 at 16:13 UTC | |
You could use something like:
... the above to move files around. You'll need to rename the $file for the target however. | [reply] [d/l] [select] |
by ChemBoy (Priest) on May 16, 2001 at 18:12 UTC | |
Look out! If you're going to use the "|| die" syntax instead of "or die", you need to put parentheses around the arguments to opendir. and are equivalent: they try to open the dirhandle FOO to the directory listed in $bar, and if opendir fails, they die. But is equivalent to which means that die is only called if $bar is empty, not if opendir fails. Which is unlikely to be what you intended. If God had meant us to fly, he would *never* have give us the railroads. --Michael Flanders | [reply] [d/l] [select] |
by Mungbeans (Pilgrim) on May 16, 2001 at 19:59 UTC | |
$clean_up_code_before_posting || die "red face: $!"; Must remember to clean up before I post (I now use or die)... | [reply] |
|
Re: Modified Date, file renaming
by Anonymous Monk on May 16, 2001 at 18:30 UTC | |
tachyon | [reply] [d/l] |
|
this kinda what you're looking for?
by cforde (Monk) on May 17, 2001 at 00:22 UTC | |
Have fun, Carl Forde | [reply] [d/l] |
by ManicMantis (Initiate) on May 17, 2001 at 10:26 UTC | |
It's great, but I run into a couple of problems. On a Windows 2k box I get an error saying the files can't be renamed because I don't have access (logged on as admin, full control and ownership of all the files in question). And on a Windows ME box it tells me they're unable to be renamed because there is 'No such file or directory.' I wasn't kidding when I said I was brand new to all of this, so attempts to fix it have been less than successful. Here's what I have so far, be gentle:
use FileHandle;
my $directory = 'c:\temp';
my @logfile;
my $handle;
my %number;
my $date;
my $string;
opendir LOGDIR, $directory
or die "Unable to open $directory: $!";
@logfile = grep {/\.log$/} readdir(LOGDIR) # only .log files
or die "unable to read $directory: $!";
foreach my $logfile (@logfile) {
unless ($handle = new FileHandle "<$directory/$logfile") {
print "Unable to open $logfile: $!\n";
next;
}
unless (read $handle, $string, 5, 0 ) { # get first 5 bytes
print "unable to read $logfile: $!\n";
close $handle;
next;
}
$date = (stat($handle))9;
$rdate = (localtime($date));
close $handle;
$number{"$string,$rdate"}++;
print "$logfile --> $string, $rdate " . '(' . $number{"$string,$rdate"} . ')' . ".log\n";
rename "$directory/$logfile", "$directory/$string,$rdate" . $number{"$string,$rdate"} . ".log"
or print "unable to rename $logfile: $!\n";
}
I also want to thank everyone for their help, it's a learning experience and I appreciate everyone's contribution. Just looking forward to the time when I'll be the one offering assistance. | [reply] |