Solved

MFC File / Directory Size Class

Posted on 1997-06-24
4
1,278 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
Whole sheet autoscrub still needed 19 50
unable to delete all specified values regedit 38 417
zeroMAx challenge 20 102
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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