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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds