[Qt-interest] Qt show folder size

Sean Harmer sean.harmer at maps-technology.com
Fri Sep 18 15:45:08 CEST 2009


On Friday 18 Sep 2009 14:37:34 Andrei Sebastian Cimpean wrote:
> On Friday 18 September 2009 04:15:11 pm Sean Harmer wrote:
> > On Friday 18 Sep 2009 13:17:56 Andrei Sebastian Cimpean wrote:
> > > On Friday 18 September 2009 02:52:28 pm Sean Harmer wrote:
> > > > On Friday 18 Sep 2009 12:09:02 Andrei Sebastian Cimpean wrote:
> > > > > This returns a standard size around 4 kb:
> > > > > QString::number(fileorfolder.size(),10)
> > > > > Does anyone know how to get the sum of all the items inside the
> > > > > folder? Thank you
> > > >
> > > > Yes, iterate through them and sum the sizes. Should be trivial with
> > > > QDirIterator and QFileInfo.
> > > >
> > > > Sean
> > > > _______________________________________________
> > > > Qt-interest mailing list
> > > > Qt-interest at trolltech.com
> > > > http://lists.trolltech.com/mailman/listinfo/qt-interest
> > >
> > > How fast is this way of doing it?
> >
> > Try it and find out - should only take approx 10 lines of code if that.
> > ;-)
> >
> > > I mean for a folder with 100k the number
> > > of subfolders and files wouldn't it take ages?
> >
> > Do you really have a dir with 100k items in it? The last time I cloned my
> > laptop linux install rsync reported that I only had 1.2 million files for
> >  the entire image (Gentoo with lots and lots of unpacked source tarballs
> >  lying around). If you do encounter this type of use case, then you could
> >  always cache results for sub-dirs and just watch for when individual
> >  subdirs change with QFileSystemWatcher for e.g.
> >
> > I know of no other way of finding out the total size of files other than
> > querying the size of each and adding them up. This is all that tools like
> >  du do - plus some additional checking for not counting hardlinked files
> >  multiple times etc.
> >
> > Cheers,
> >
> > Sean
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> Thanks a lot man. You were really helpful. Kudos for that.

Try this then. ;-)

The attached code does what you are after (excuse the lack of command line 
argument checking).

I get the following output with a warm disk cache under Gentoo Linux x86_64 
with a reiserfs filesystem:

./simpledu /home/sean_harmer/development/
Starting dir size operation for "/home/sean_harmer/development/"
Finished processing in 8 seconds
Total size of all 190097 files is 12407 MBi

So, not to shabby for a few lines of code :-)

Of course performance will vary depending upon many factors including 
hardware, disk cache state, system load, file system in use etc.

HTH,

Sean
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 808 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090918/4d9375ca/attachment.bin 


More information about the Qt-interest-old mailing list