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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds