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:
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.