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: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds