[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MFC File / Directory Size Class

Posted on 1997-06-24
4
Medium Priority
?
1,334 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 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

829 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