Solved

MFC File / Directory Size Class

Posted on 1997-06-24
4
1,271 Views
Last Modified: 2013-11-19
I have been looking for some time, but have been unable to find an MFC class to do directory / file sizing.

Is there such as thing ?

Thanks

Glenn Corbett
0
Comment
Question by:gcorbett
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
rpb earned 50 total points
ID: 1302269
You can use CFile::GetLength() to find the length of a file.

You can also use CFile::GetStatus(CFileStatus &s), which fills in a CFileStatus object (a struct) that contains information about when the file was last modified, file attributes, etc.  Note that there is a static version of this function as well, which allows you to pass the filename in, rather than open the file to start with.

You don't mention what you are trying to do with this information, but you may also want to look at the FindFirstFile() and FindNextFile Win32 functions, which also give size information for files and directories.  You can also call GetFileAttributes() to find if a file is a file or a directory.

Hope this helps
0
 

Author Comment

by:gcorbett
ID: 1302270
Thanks,

What I was looking for is possibly a class (MFC), or library (.LIB) to do the work. I have a directory list calculator I got from one of the Microsoft samples (can't remember where). The problem with this is it requires a fair bit of work to turn it into a class. It basically does a recursive "walk" through the directories calculating the disk space as it goes.

What I am trying to do is have the user of the application select a user, list of users, or a server (which I have done), and then it will calculate the total amount of disk space being used in those directories. The code I have also seems to generate incorrect sizing, it is always above the actual allocation, and in the case of a large directory structure it can be several megabytes larger than Explorer or  DIR /S seems to think it is.

I assume with the CFile:: class that I would have to wrap it in Win32 to do the directory walking part. Is that correct ??

Glenn Corbett
0
 

Expert Comment

by:rpb
ID: 1302271
The CFile class is not 32-bit specific - you can use it in 16-bit code as well.

Remember that the size needed to store a file is larger than the actual number of bytes in a file.  Forgive me if you know this, but it sounds from your comment that you are looking at the total file size in Explorer, which gives the total of the number of bytes in the selected files, whereas the amount of space required on disk is larger than this.  This is because of disk "cluster sizes".  A cluster is the minimum amount of disk space that can be allocated, and all allocations are in multiple of this size.  If you have, say, an 8k cluster size on your disk, then files of size 1 byte, 10 bytes, 100 bytes, 1000 bytes or 8192 (8k) bytes will all take up 8k of disk space.  It could be that your routine is working with allocation sizes, rather than file size, which is either more useful or less, depending on what you are trying to show.

If you wanted to do the directory walking, you may want to use the Win32 functions, as I am not sure CFile supports such functions.  Alternatively you could derive a class from CFile with the added functionality (say a CWalkingFile or something), and add cover functions for the three Win32 functions mentioned above.  The class could then keep track of its own progress in the walking using a member variable, which would keep things nice and encapsulated.
0
 

Author Comment

by:gcorbett
ID: 1302272
Thanks,

I eventually found the Microsoft PDC WIn32 sample which does   the directory walking, and encapsulated it into a C++ class. I removed the code which looks inside the list of found files, and it seems to work pretty well.

Glenn Corbett

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 90
Expand to include initial dialog with two choices. 9 67
has77  challenge 9 71
Line meaning 9 81
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now