|
Hi I am new to PERL. Infact no experience at all. I am having a problem. Below is the brief description of the problem.
I have a folder C:\ErrorLogs\.
In this folder i have some files. If the size of the file inside this folder exceeds 60KB i should create a new errorlog in some other folder with contents like "<filename> exceeds 60 KB file size". I am not able to write the script for this in PERL since i am new to PERL. Kinldy help me in writing the code for this problem. Hoping your response ASAP.
|
|
|
Hi Shanilr,
I hate posting questions up and then not getting any replies. Hope you've not given up on this one.
I am not a perl expert and there are probably better ways to do this, which somebody else out there might like to tell us but try the following
#!/usr/bin/perl -w
opendir (OPEN, "directory_with_all_your_files_in?"); # possibly C:\ErrorLogs
while ($name=readdir(OPEN)){
print "$name\\";
}
save the above to a file, call it file_1 and run it
perl file_1 > test
#!/usr/bin/perl
$nf=?; #enter number of files here
$max=?; #enter max file size here
#$errorlog=errorlog; #enter name of error log folder here
#mkdir("$errorlog");
open (OPEN, "test");
@dir=split(/\\/,<OPEN>);
close OPEN;
$i=2;
while ($i<$nf+3) {
$filesize=(stat($dir[$i]))[7];
if ($filesize>$max){
print ("$dir[$i] exceeds $max bytes\n");
#rename ("$dir[$i]", "$errorlog/$dir[$i]");
}
$i++;
}
again save the above to a file, call it file_2 and run it
perl file2 > test2
For file_2, You need to know the number of files in the directory your analysing (change $nf accordingly) and you can vary the output by changing $max. I think you wanted to output all files over 60kb, so change $max to 6000. When I run these two scripts on my system test2 contains a list of all to files that exceed $max bytes. You can also move out all the files exceeding $max by uncommenting all the lines that relate to errorlog in file_2.
Hope thats helpful!
|
|
|
Hi Shanilr,
I hate posting questions up and then not getting any replies. Hope you've not given up on this one.
I am not a perl expert and there are probably better ways to do this, which somebody else out there might like to tell us but try the following
#!/usr/bin/perl -w
opendir (OPEN, "directory_with_all_your_files_in?"); # possibly C:\ErrorLogs
while ($name=readdir(OPEN)){
print "$name\\";
}
save the above to a file, call it file_1 and run it
perl file_1 > test
#!/usr/bin/perl
$nf=?; #enter number of files here
$max=?; #enter max file size here
#$errorlog=errorlog; #enter name of error log folder here
#mkdir("$errorlog");
open (OPEN, "test");
@dir=split(/\\/,<OPEN>);
close OPEN;
$i=2;
while ($i<$nf+3) {
$filesize=(stat($dir[$i]))[7];
if ($filesize>$max){
print ("$dir[$i] exceeds $max bytes\n");
#rename ("$dir[$i]", "$errorlog/$dir[$i]");
}
$i++;
}
again save the above to a file, call it file_2 and run it
perl file2 > test2
For file_2, You need to know the number of files in the directory your analysing (change $nf accordingly) and you can vary the output by changing $max. I think you wanted to output all files over 60kb, so change $max to 6000. When I run these two scripts on my system test2 contains a list of all to files that exceed $max bytes. You can also move out all the files exceeding $max by uncommenting all the lines that relate to errorlog in file_2.
Hope thats helpful!
|
|
|
This is one I just make works with the File::Copy module to copy the data to a new file.
At # Clear the main Log file
you can delete the file with this, if you want
unlink("$logfile");
Most of the time its faster to just clear it like i have it below becase you may have to reCHMOD that file to 644 when its remade.
At # Checks file size, is the code to check file size on the server.
my $logfile = "Location/logfile.log"; # The main Log file
my $oldlogdir = "Old_location"; # old log file dircetory location, no / at end!
my $max_size = 60; # Should be 60kb, i think
my $date = time; # Get time for file name
if((-s "$logfile") > ($max_size * 1024)) { # Checks file size
use File::Copy; # Most servers should have this module!
copy("$logfile","$oldlogdir/$date.log"); # The File::Copy module
chmod(0644, "$oldlogdir/$date.log"); # Make the new file secure on Linux servers
# Clear the main Log file
open (LOG,"$logfile");
print LOG "";
close (LOG);
}
Have fun!
|
|
|
|
|
|
|
|