Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C++ Wrapper and Extension of Windows FileSystemObject Objects

0.00/5 (No votes)
21 Feb 2006 2  
A C++ implementation of the Windows FileSystemObject object. It wraps and extends the standard FileSystemObject interfaces (methods/properties). Now, C++ developers can manipulate folders and files without their own Win32 API code. They can take advantage of the well-tested Windows FileSystemObject.

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 the Windows FileSystemObject. It wraps standard interfaces (method/properties) in C++ classes. C++ developers can easily 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. 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

Description

CFsoFileSystemObject Wrapper to the FileSystemObject interface; used to get other object interfaces.
CFsoDrive Wrapper to the Drive interface; used to manipulate drives (e.g., c:\).
CFsoFolder Wrapper to the Folder interface; used to manipulate folders (e.g., c:\windows).
CFsoFile Wrapper to the File interface; used to manipulate files (e.g., c:\windows\xxx.dll).
CFsoTextStream Wrapper to the TextStream interface; used to read/write/append text files (supports both ASCII and Unicode).
CFsoBinaryStream Interface extension to support binary file operations; used to read/write/append binary files.

How To Use Them?

  1. You need to get an instance of FileSystemObject.
  2. CFileSystemObject * fso = new CFileSystemObject();
  3. Once you get the instance, you can use it to access other interfaces and play with the methods/properties 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 got at step 3)
    8. CFsoTextStream * textStream = 
        fileText->OpenAsTextStream(1/*IO Mode*/, 0/*Text Format*/);
    9. Open a binary stream (using the binary file object got at step 3)
    10. CFsoBinaryStream * binaryStream = fileBinary->OpenAsBinaryStream(1/*IO Mode*/);
    11. Enumerate all sub-folders under a specified folder (using the folder object got at step 2)
    12. CFsoList<CFsoFolder> * subFolders = folderWinRoot->GetSubFolders();
      for (int i = 0; i < subFolders->GetCount(); i++)
      {
          CFsoFolder * folder = subFolders->Get(i);
          ...
          ...
          ...
      }
      subFolders->Clear(true);
      // Clear all folder objects in the list and release memory occupied
    13. Enumerate all files under a specified folder (using the folder object got at step 2)
    14. CFsoList<CFsoFile> * files = folderWinRoot->GetFiles();
      for (int i = 0; i < files->GetCount(); i++)
      {
          CFsoFile * file = files->Get(i);
          ...
          ...
          ...
      }
      files->Clear(true);
      // Clear all file objects in the list and release memory occupied

End

  • In order to help you understand these classes, methods, enums, and typedefs, I have written concrete comments in the source code and generated an HTML documentation by using Doxygen. You can easily find out the information you need to play with these classes. When you unzip the downloaded source code file, you can find a .CPP, a .H, plus a folder named HTTML. Please open that 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. In my PC, the default Windows root path is C:\Winnt. It may not be the same case 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here