Click here to Skip to main content
15,065,324 members
Articles / Desktop Programming / MFC
Article
Posted 21 Feb 2006

Stats

71K views
722 downloads
18 bookmarked

C++ Wrapper and Extension of Windows FileSystemObject Objects

Rate me:
Please Sign up or sign in to vote.
1.56/5 (15 votes)
21 Feb 2006CPOL2 min read
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

CFsoFileSystemObjectWrapper to the FileSystemObject interface; used to get other object interfaces.
CFsoDriveWrapper to the Drive interface; used to manipulate drives (e.g., c:\).
CFsoFolderWrapper to the Folder interface; used to manipulate folders (e.g., c:\windows).
CFsoFileWrapper to the File interface; used to manipulate files (e.g., c:\windows\xxx.dll).
CFsoTextStreamWrapper to the TextStream interface; used to read/write/append text files (supports both ASCII and Unicode).
CFsoBinaryStreamInterface 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. C++
    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. C++
      CFsoDrive * driveC = fso->GetDrive(L"c:\\");
    3. Folder
    4. C++
      CFsoFolder * folderWinRoot = fso->GetFolder(L"c:\\windows");
    5. File
    6. C++
      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. C++
      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. C++
      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. C++
      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. C++
      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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Skeeter
Program Manager Microsoft
China China
Graduated from Shanghai Tongji university in 1997 with bachelor degree in computer science, Wenbin got his first job as a system engineer at Bell Alcatel Mobile Shanghai office responsible for telcom-class mobile system development and deployment. Since then, Wenbin has in turn worked for Intel and Microsoft both inside and outside China, first as senior engineer, later project manager and then senior product manager.

With 15-year experience working with the world top IT companies, Wenbin has developed solid skill in C/C++, C#, Java, software engineering, agile development, etc, and multiple talents in product management, public presentation and speech, etc. He has always been an active member at PMI (Project Management Institution) and a regular lecture at Intel Developer Forum, Microsoft TechED conference as well as many other world-class industrial conferences. His wide-ranged industrial practice and high-level personal maturity have made him one of the best in public speech and professional training.

Over the years, Wenbin has cultivated his very own style in public speech, which is considered informative, engaging and refined. Since last year, Wenbin has also taken new adventure in project and product management consulting business and has proven high capacity through his work with many local emergent IT firms. Wenbin’s specialty in management consulting is on project management methodologies and processes, project management tools (e.g. MS Project), and team recruitment, build, and motivation.

In addition, Wenbin has received many professional qualifications including MCSE (Microsoft Certified System Engineer), MCSD (Microsoft Certified System Developer), MCDBA (Microsoft Certified Database Administrator), SCJP 2 (Sun Certified Java Programmer 2), and PMP (PMI Certified Project Management Professional). On top of that, Wenbin has got several on-duty inventions and one of them was successfully patented by United States Patent and Trademark Office.

Comments and Discussions

 
GeneralSuggested change to cFfoFileSystemObject for windows 7 Pin
Dennis Birke10-Mar-11 5:57
MemberDennis Birke10-Mar-11 5:57 
GeneralRe: Suggested change to cFfoFileSystemObject for windows 7 Pin
Member 78093943-Apr-11 14:23
MemberMember 78093943-Apr-11 14:23 
GeneralError when compiling, can't get working Pin
pepinliria8-Jun-10 2:04
Memberpepinliria8-Jun-10 2:04 
GeneralMy vote of 2 Pin
hold28-Mar-10 20:36
Memberhold28-Mar-10 20:36 
AnswerA much easier approach Pin
Mr President24-Apr-09 10:43
MemberMr President24-Apr-09 10:43 
GeneralDetected memory leaks! Pin
Dagal21-Nov-07 20:20
MemberDagal21-Nov-07 20:20 
QuestionFile Count and SubFolder Count zero or negative. Pin
Siby Varghese8-Nov-07 2:08
MemberSiby Varghese8-Nov-07 2:08 
AnswerRe: File Count and SubFolder Count zero or negative. Pin
Siby Varghese8-Nov-07 3:43
MemberSiby Varghese8-Nov-07 3:43 
QuestionDo you have a working project? Pin
Tony Reynolds5-Sep-07 0:54
MemberTony Reynolds5-Sep-07 0:54 
GeneralNice Job Pin
BrianCharles13-Jul-07 12:18
MemberBrianCharles13-Jul-07 12:18 
Question_com_error Pin
Harrison Ford28-Aug-06 1:02
MemberHarrison Ford28-Aug-06 1:02 
Questionnoob question. Pin
hossimo22-Apr-06 4:17
Memberhossimo22-Apr-06 4:17 
AnswerRe: noob question. Pin
sPhinX23-May-06 23:43
MembersPhinX23-May-06 23:43 
QuestionIs there any demo project? Pin
Ricado3-Apr-06 0:36
MemberRicado3-Apr-06 0:36 
QuestionWhere is the source? Pin
Mike Walter23-Feb-06 10:54
MemberMike Walter23-Feb-06 10:54 
AnswerRe: Where is the source? Pin
Skeeter23-Feb-06 11:40
MemberSkeeter23-Feb-06 11:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.