Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MFC File / Directory Size Class

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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