A C++ Wrapper and Extension of Windows FileSystemObject Objects

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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:windowsxxx.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.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read