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.