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?
- 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 the methods/properties exposed.
- Drive
CFsoDrive * driveC = fso->GetDrive(L"c:\\");
- Folder
CFsoFolder * folderWinRoot = fso->GetFolder(L"c:\\windows");
- File
CFsoFile * fileText = fso->GetFile(L"c:\\windows\\abc.txt");
CFsoFile * fileBinary = fso->GetFile(L"c:\\windows\\abc.bin");
- Open a text stream (using the text file object got at step 3)
CFsoTextStream * textStream =
fileText->OpenAsTextStream(1, 0);
- Open a binary stream (using the binary file object got at step 3)
CFsoBinaryStream * binaryStream = fileBinary->OpenAsBinaryStream(1);
- Enumerate all sub-folders under a specified folder (using the folder object got at step 2)
CFsoList<CFsoFolder> * subFolders = folderWinRoot->GetSubFolders();
for (int i = 0; i < subFolders->GetCount(); i++)
{
CFsoFolder * folder = subFolders->Get(i);
...
...
...
}
subFolders->Clear(true);
- Enumerate all files under a specified folder (using the folder object got at step 2)
CFsoList<CFsoFile> * files = folderWinRoot->GetFiles();
for (int i = 0; i < files->GetCount(); i++)
{
CFsoFile * file = files->Get(i);
...
...
...
}
files->Clear(true);
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.
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.