I am working on a sub that will get the name and size of each file in a specified directory AND print the total size of all of the files in the directory. The code I will be copying below is a way to print the filenames and their sizes but I am having much difficulty in adding them together correctly for the total directory size.I know that there are other and I am sure better ways of doing what I have coded below but this worked for me. However, I can't figure out how to add all of the file sizes. If anyone would suggest another method which will allow me accomplish both of my goals, i am more then open to suggestions.Code which successfully posts the name and size of each file in a specified directory:

You will notice that I set $directory_size to zero in order to prepare for adding all of the file sizes together. I can't add the file size to $directory_size in the foreach loop because the value would just be reset to zero for each file size that is added.

And another note: this is all in an 'included' module and all required information for this code to work is included in the actual script. What I have above DOES work just fine when the script is run, but I can't figure our (correctly) how to add the file size with my method above.

The reason why I "couldn't" do it that way was basically the = next to the + in adding the $directory_size addition. I had the same thing originally which I described in my post below my code except without the =. When I tried it that way it simply added the first file size to 0 but it then didn't add the second file size to it. For the addition of the second file size it again made $directory_size equal zero and added the size of the second file which negated having ever added the first file size into it.

Now I believe that I not only understand the error of my ways, but will also be able to tackle any problem such as this in the future and I greatly appreciate your help.

The script is printing out all of the text but is not printing any of the filenames or sizes...nor the total directory sizee. I believe that this is because there is no statement 'telling' the script what $file is? In my code, I 'tell' the script what $file is but I do not think the solution does.

Below is what was printed. It definitely seems to me that $file is the culprit. You will notice bellow that the text is printed out 6 times which would suggest that there are 6 files in the directory, but there are only 4. This would lead me to believe that it is taking the "." and ".." into account as the other two files which might suggest that "unless ($file =~ /^\.+$/)" is not working because it is printing even while $file does equal "." and "..".