View Full Version : Get the size of folder contents?
element78
12-04-2003, 01:00 PM
Is there a way to do it without looping through all the files in the folder and checking individual file sizes?
That method would be sufficent I suppose, but very slow for what I need to do.
Any ideas on how this could be done in a different manner?
ex: finding the total size of the program files folder and all of it's contents
Thanks:)
TJ_Tigger
12-04-2003, 01:15 PM
Everything in Program Files and all subdirectories? Are you MAD? Do you want just the size of the directory or the size of the directory and a listbox that contains a list of all the files from all directories in Program Files?
element78
12-04-2003, 01:19 PM
LOL, no I'm not mad ;)
Program Files was just an example. Maybe I wasn't clear enough.. what I'm trying to do is (example):
Listbox:
C:\Program Files (1504 mb)
C:\Games (3440 mb)
C:\Documents And Settings (250 mb)
see what I'm saying? Just the absolute size of each folder I need to have searched.
It's a little difficult to explain but i hope i did it better than before :)
I guess it helps if you actually include the attachment :p
If you want an hourglass to show while the DLL
gets the size, use this one instead of the one in
the apz.
TJ_Tigger
12-04-2003, 04:05 PM
beat out again :(
Here I was trying to do it all within AMS5 . . . then again I don't know how to program .dlls so how else am I going to do it. Here is what I came up with.
folder = Dialog.FolderBrowse("Please Select a Folder to get its current size", _DesktopFolder);
folders = Folder.Find(folder, "*", true, nil);
filesall = {};
for fi,fv in folders do
files = File.Find(fv, "*", true, true, nil);
for filesi,filesv in files do
Table.Insert(filesall, Table.Count(filesall)+1, filesv);
end
end
fsize = 0;
for i,d in filesall do
size = File.GetSize(filesall[i]);
fsize = fsize + size;
end
Paragraph.SetText("Paragraph1", "Your directory " .. folder .. " is " .. fsize .. " bytes in size.");
Too cool Tig!
I was going to try it in AMS, but didn't have the guts to try it. I'm not sure how well AMS would handle recursion to do sub-folders, but it seems it would work. I'd be interested in seeing the speed comparisons from LUA scripting to a DLLs speed. So, if you're bored :D ...
I know from the little bit of testing I've done, the LUA script is rock solid and speedy.
Cool! Another free DLL from Worm. You are the greatest, thanks.
Is there any chance that you could share the proper syntax for the calls in a text file so that those of us still using AMS4 could figure out how to use this DLL? You give so much, I’m sorry to ask for more, but it’s a nifty DLL, and I’d like a chance to use it too.
Thanks again Worm.
TJ_Tigger
12-04-2003, 07:14 PM
Here is some updated code. I ran into a couple of problems where I would get nil values in a table when I did the search. I have added a couple additional lines to detect this event and skip the section if detected.
Worm, I will try some speed tests. With AMS it seems to be fairly speedy, but as the number of folders and files increase it starts to slow down. If you have suggestions on how to tighten up the code, let me know. I am always looking for ways to better my code.
Here is the new code, just cut and paste into the button.
folder = Dialog.FolderBrowse("Please Select a Folder to get its current size", _DesktopFolder);
StatusDlg.Show(MB_ICONNONE, false);
if (folder ~= nil) and (folder ~= "CANCEL") then
folders = Folder.Find(folder, "*", true, nil);
if (folders ~= nil) then
filesall = {};
for fi,fv in folders do
files = File.Find(fv, "*", true, true, nil);
if (files ~= nil) then
for filesi,filesv in files do
Table.Insert(filesall, Table.Count(filesall)+1, filesv);
end
end
end
fsize = 0;
for i,d in filesall do
size = File.GetSize(filesall[i]);
fsize = fsize + size;
end
Paragraph.SetText("Paragraph1", "Your directory " .. folder .. " is " .. fsize .. " bytes in size.");
StatusDlg.Hide();
end
end
element78
12-04-2003, 07:51 PM
Wow, thanks guys! Very helpful, as always :):)
Ill try it out and let you know!
element78
12-04-2003, 08:28 PM
TJ: Your method seemed to work pretty well, but one problem.. the size returns completely inaccurate.. Screenshot attached :(
Worm: I like the DLL, a lot. Although (a huge error on my part), the searching is not supposed to search in a whole tree, just the specified folder. (e.g. if theres 70 folders inside c:\winnt and 200 files, it would only return the size of the combined 200 files). I must have been sleeping when i described my question, very sorry!
element78
12-04-2003, 08:33 PM
Ahhh TJ, i figured out what was wrong.. had to change recurse to false for the file search, it was doubling the search. :)
TJ_Tigger
12-04-2003, 08:43 PM
I realized that after playing with the file size stuff that I was omitting the files in the selected folder. I got all the folders under the selected but forgot the selected one. D'oh.
Here it is for all to test. I have included Worms dll in here as well. I figured out how to make my code smaller. I don't know what I was thinking but it works well now with much smaller code.
tigg
Oops, I should have spent a little more time trying before I asked. With a little experimentation, I was able to figure out the function name and proper parameters for Worm's newest DLL. Thanks for the new tool Worm!
Here's an updated DLL that lets you choose whether you want to recurse the sub-folders or not.
BTW, Tig, You're rockin on the script
Lorne
12-05-2003, 08:34 AM
Tig, assuming you're not using the filesall table somewhere else...you can eliminate one more line:
folder = Dialog.FolderBrowse("Please Select a Folder to get its current size", _DesktopFolder);
StatusDlg.Show(MB_ICONNONE, false);
if (folder ~= nil) and (folder ~= "CANCEL") then
filesall = {};
files = File.Find(folder, "*", true, true, nil);
fsize = 0;
for i,d in files do
size = File.GetSize(files[i]);
fsize = fsize + size;
end
fsize = Math.Floor(fsize/1024000);
Paragraph.SetText("Paragraph1", "Your directory " .. folder .. " is " .. fsize .. " MB in size.");
StatusDlg.Hide();
end
TJ_Tigger
12-05-2003, 09:52 AM
Yes you are correct. I was using it but removed those lines. Still trying to learn the ins and outs. :p
I added a question that asks if you want to recurse the subdirectories.
folder = Dialog.FolderBrowse("Please Select a Folder to get its current size", _DesktopFolder);
StatusDlg.Show(MB_ICONNONE, false);
if (folder ~= nil) and (folder ~= "CANCEL") then
recurseq = Dialog.Message("Recurse?", "Would you like to recurse the selected directory?", MB_YESNO, MB_ICONQUESTION, MB_DEFBUTTON1);
if (recurseq == IDYES) then
recurse = true;
else
recurse = false;
end
files = File.Find(folder, "*", recurse, true, nil);
fsize = 0;
for i,d in files do
size = File.GetSize(files[i]);
fsize = fsize + size;
end
fsize = Math.Floor(fsize/1024000);
Paragraph.SetText("Paragraph1", "Your directory " .. folder .. " is " .. fsize .. " MB in size.");
StatusDlg.Hide();
end
size = File.GetSize(files[i]);
fsize = fsize + size;
..to..
fsize = fsize + File.GetSize(files[i]);
Removes one less calculation.
Just notice a little thing in the help file though, if the GetSize fails it will return -1. So in either instance, if the call fails for some reason, we are subtracting from the fsize.
Too bad the Callback Functions parameter wasn't the name of the Path and filename, then you could use the callback function to do the GetSize and eliminate the For Loop all together. Maybe there is a way to get the Filename in the callback, but I don't see it. Right now, it sends the folder path.
Brett? Lorne? Is that possible?
TJ_Tigger
12-05-2003, 10:38 AM
I like the ability to combine actions into one line of code. I try to do that whenever I can think about it.
SUF6NEWBIE
06-30-2004, 11:45 PM
hey guys...
fsize = Math.Floor(fsize/1024000);
should be:
fsize = Math.Floor(fsize/1048000); --bytes to megabytes
..do a 'real world' check on a file greater than 1 meg..select properties
..have a look at bytes vs meg..(try /1024000 vs 1048000) result ?
..if I'm wrong then my system is lying to me..he he
..unless math.floor based on total size etc returns different
..confirm someone plse
BTW learning how to keep robust code to a minimum is great !
SUF6NEWBIE
01-26-2005, 08:10 PM
I've fiddled with the code for Worms Foldersize .dll
and came up with this 'generic modification' to code offerings
(I found the dll was not reliable at my end for very large folder trees
with recurse set to 1. So hence below stuff. Lemme know if can improve !)
--Globals Funcs -wack these in Global functions in AMS5Pro or SUF7.0 or TUv2(beta):
--QNQ
--GETALL_SIZE_Dll
--SOGETALL_SIZE_Dll
function QNQ(sIn)
return "\""..sIn.."\"";
end
function GETALL_SIZE_Dll()
--QNQ (Quote/End Quote) global func that encloses 'folder variable' in Quotation Markes for Foldersize.DLL
--set the recurse option(done elsewhere as below in 'Generic code');
--(Note my testing so far revealed that worms dll only seems work reliably for very large
--Folder trees or 'sizes', if 'recurse' is set to 0(off) and then cycle through a folder.find table)
--nRecurse=1;
--nRecurse=0;
FSZ_bts = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\Foldersize.DLL", "GetFolderSize", QNQ(sFolder)..","..nRecurse, DLL_RETURN_TYPE_LONG, DLL_RETURN_TYPE_LONG);
return FSZ_bts;
end
--EOGETALL_SIZE_Dll
--some 'Generic' Code to use above Globals -could change to a function if wish to...
--for example purposes target the Os windows folder
sSrce = _WindowsFolder; --for a 'browse folder' table(change code to suit)
local tbAll_folders = Folder.Find(sSrce, "*", true, nil);
--local tbAll_files = File.Find(sSrce, "*.*", true, false, nil, nil);
sFolder = sSrce; --for worms foldersize.dll
TotalOsfsize = 0;
--get root folder first 'sSrce' (covers if contains any binaries !)
nRecurse=0; --for worms foldersize.dll
GETALL_SIZE_Dll(); --call worms dll
Totalfsize = FSZ_bts + Totalfsize; --root folder size done
--now if desired -cycle through Root folder 'sub tree
for nfld, tgtFolder in tbAll_folders do
sFolder = tgtFolder;
nRecurse=0;
GETALL_SIZE_Dll();
Totalfsize = FSZ_bts + Totalfsize;
end
Totalfsize_bytes = Totalfsize; --(changed to test both conversion methods only)
Totalfsize = Math.Round(((Totalfsize_bytes /1024) /1024), 1); --meg to 1 decimal place
Dialog.Message("Debug Only: Folder tree size", Totalfsize.." MB");
--or use ADAM's funky 'bytes to meg' alogorithim(/ 1048576);
Totalfsize = Math.Round((Totalfsize_bytes / 1048576), 1); --meg to 1 decimal place
Dialog.Message("Debug Only: Adams's Funky Folder tree size", Totalfsize.." MB");
SUF6NEWBIE
01-26-2005, 09:05 PM
oops..modifyng my own app code-missed this one..
change the line:
TotalOsfsize = 0;
to:
Totalfsize = 0;
SUF6NEWBIE
01-26-2005, 11:33 PM
WORM has very kindly offerred to have another look
at his 'source code' for this very handy 'free dll',
so if and when he has the time...may not need some of the
LUA stuff...
SUF6NEWBIE
01-29-2005, 12:04 AM
WORM...
Spent more time with your DLL ...(recurse = 1)
..sure seems like a path-depth 'quirk'
BTW ..the speed of DLL return-s is fantastic..even when cycling through
and recalling 100's of times or more...reliable...read a DVD9 source with
12000 binaries 800 folders.. on a PIV ..correct total bytes in under 10 seconds !!
(equivelent on ata100 hdisk storage --2 seconds)
So if ya don't get to it ...cycling through with the latest version and
recurse=0...achieves reliable results at speed TKS once again
jean3167
09-17-2009, 04:21 AM
Quite good directory treeview tool called print directory (http://www.print-directory.com) not a freeware but affortable.
Powered by vBulletin™ Version 4.0.6 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.