A C++ Wrapper and Extension of Windows FileSystemObject Objects

Introduction

Windows comes with a FileSystemObject system. It provides easy-to-use folder/file manipulation interfaces. Unfortunately, only scripting language-based developers can take advantage of it. CFileSystemObject is a C++ implementation of Windows FileSystemObject. It wraps standard interfaces (method/properties) in C++ classes. C++ developers easily can use it to interact with Windows folders and files without dealing with Win32 APIs (and their complicated parameters). In addition, I extend the final C++ class further to support binary file operations. With this, most developers are able to finish 90% of their folder/file-related coding work by using these C++ classes.

FileSystemObject Classes at a Glance

Class Name Wrapper to Interface Description
CFsoFileSystemObject FileSystemObject Gets other object interface
CFsoDrive Drive Manipulates drives (for example, c:\)
CFsoFolder Folder Manipulates folders (for example, c:\windows)
CFsoFile File Manipulates files (for example, c:\windows\xxx.dll)
CFsoTextStream TextStream Reads/Writes/Appends text files (supports both ASCII and Unicode)
CFsoBinaryStream   Interface extension to support binary file operation; reads/writes/appends binary files

How to Use Them

You need to get an instance of FileSystemObject:

CFileSystemObject * fso = new CFileSystemObject();

Once you get the instance, you can use it to access other interfaces and play with methods/properties that are exposed.

  1. Drive
  2. CFsoDrive * driveC = fso->GetDrive(L"c:\\");
  3. Folder
  4. CFsoFolder * folderWinRoot = fso->GetFolder(L"c:\\windows");
    
  5. File
  6. CFsoFile * fileText = fso->GetFile(L"c:\\windows\\abc.txt");
    CFsoFile * fileBinary = fso->GetFile(L"c:\\windows\\abc.bin");
    
  7. Open a text stream (using the text file object gotten at Step 3)
  8. CFsoTextStream * textStream =
       fileText->OpenAsTextStream(1/*IO Mode*/,
                                  0/*Text Format*/);
    
  9. Open a binary stream (using the binary file object gotten at Step 3)
  10. CFsoBinaryStream * binaryStream =
       fileBinary->OpenAsBinaryStream(1/*IO Mode*/);
    
  11. Enumerate all sub-folders under a specified folder (using the folder object gotten at Step 2)
  12. CFsoList<CFsoFolder> * subFolders = folderWinRoot->GetSubFolders();
    for (int i = 0; i < subFolders->GetCount(); i++)
    {
       CFsoFolder * folder = subFolders->Get(i);
       ...
       ...
       ...
    }
    // Clear all folder objects in the list and release memory
    // occupied
    subFolders->Clear(true);
    
  13. Enumerate all files under a specified folder (using the folder object gotten at Step 2)
  14. CFsoList<CFsoFile> * files =
       folderWinRoot->GetFiles();
    for (int i = 0; i < files->GetCount(); i++)
    {
       CFsoFile * file = files->Get(i);
       ...
       ...
       ...
    }
    // Clear all file objects in the list and release memory
    // occupied
    files->Clear(true);
    

Conclusion

To help you understand those classes, methods, enums and typedefs, I write concrete comments in the source code and generate HTML documentation by using Doxygen. You can easily find out the information you need to play with these classes. When you unzip the source code file, you can find one .CPP, one .H ,plus one folder named HTML. Please enter the HTML folder and double-click indext.html to open the source code documentation. Enjoy!

Please make sure the file path of scrrun.dll is correct according to your Windows system settings. On my PC, the default Windows root path is C:\Winnt. It may not be the same for your system. If your system root path is C:\Windows, please change line #25 in CFileSystemObject.h accordingly. Otherwise, a compiler error will occur.



About the Author

Skeeter Xu

Software Architect from Intel R&D Center, Shanghai China. Before that, I was working in Singapore for 8 years focusing on all kinds of Windows platform development - from firmware, device drive to application level.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds