Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting File or Directory size on server 1

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
Is there a way so that a Perl script can check the size of a file or folder? I am wondering because file and folder size is an integral part of my website.
 
for files you can use stat() or the -s file test operator:

Code:
my $size = -s 'filename.txt';

for folders you have to add up the size of all the files or use an operating system command that will return the total size of the folder contents. There might be some modules that will also do that, I know there is for Windows:

Win32::DirSize

- Kevin, perl coder unexceptional!
 
Thanks Kevin. You are always a great help :).
 
To determine the file size use -s. Take a moment to read all the other operators at the page as they are very useful for file operations:


Code:
my $fileSize = -s $fileName;

To determine the size of a directory, you'll have to come up with your own definition first. Are you looking for the summation of the files immediately inside the directory, or the sum of all files in its directory tree. If the former, I would use the File::Slurp module and the read_dir function. If the later, I would use the File::Find and the find function. In either case, appropriate use of -d and -s will be all you need to come up with your sum.

 
so -s would return a value for the file size?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top