codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Finding File Size and Creating Error log  shanilr at 06:55 on Thursday, July 20, 2006
 

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.

  Re: Finding File Size and Creating Error log  idu-ron at 18:46 on Friday, August 25, 2006
 

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!

  Re: Finding File Size and Creating Error log  idu-ron at 18:47 on Friday, August 25, 2006
 

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!

  Re: Finding File Size and Creating Error log  S_Flex at 11:41 on Saturday, August 26, 2006
 

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!








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  info out of an video example
•  Re: Help: Trouble with z-Index and SELECT lists
•  position of a page inside a pop-up in javascript
•  document.write in IE and Mozilla/Netscape.
•  Ugent Help needed
•  java help questions
•  Re: Get HTML Sourcecode
•  Re: Submitting Iframes
•  Re: Validating multiple passwords


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006