An introduction to the TR2 filesystem library in Visual Studio 2012
Visual Studio 2012 includes the filesystem library. It isn’t part of the C++11 standard but it’s one of the proposals for TR2. The proposal is based on the library included in boost.
The library provides a convenient (and cross platform, if supported by compilers) interface for file & directory traversing and manipulation, as well as querying filesystem information such as file sizes and disk space.
The main components of the library are:
- path objects
- directory iterators
- free functions
Path objects encapsulate filesystem paths and provide convenient ways of assembling paths and extracting their components.
Directory iterators allow you to iterate over directory contents, both recursively and non-recursively.
Free functions mainly operate on paths and provide all sorts of operations, from checking whether a file exists, to deleting a directory and its contents.
Let’s dive into some code to see the functionality of the library.
Checking if files exist & creating directories
It’s common that you need to create a directory (or a tree of them) to store logs, the output of your application etc.:
As you can see, you can use the convenient operator/ and operator/= to assemble paths from parts.
create_directories
can create a number of directories along the path chain if they don’t exist (e.g. both logs and current in the example above).
Iterating over directory contents & moving files
The next thing you might want to do is move log files over the size of 100 MB from current directory to archive:
Note that you can’t use the range based for loop to iterate over the contents of a directory. This kind of iteration can only be used to extract the components of a path:
Iterating recursively & filtering files
It’s often necessary to traverse a directory tree to find files in sub-directories. The filesystem library has a really convenient recursive_directory_iterator
for this purpose.
It’s also a common task to look for files of particular type or with a name matching a mask. The filesystem library is of no particular help here, but it’s easy to do this by adding the regex library into the mix.
The example below shows how to do both of these things. It goes through all sub-directories of the kml directory and prints out the file names and last write times for all KML and KMZ files which have the string curletts in their name:
Checking available space
Finally, you may need to check the available space, e.g. when your application produces large output files. It’s easy to do:
Further reading
As you have seen, the filesystem library provides a convenient interface for filesystem related operations, and allows you to handle real world tasks in just a few lines of code.
The library contains many more useful functions not shown in the tutorial. You can find the MSDN documentation for them here: http://msdn.microsoft.com/en-us/library/hh874694.aspx
The current Microsoft implementation is based on a 2006 proposal which is based on the Boost filesystem v.2. You can see this version of the proposal here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1975.html
The proposal has later been updated to reflect the evolution of Boost filesystem to v.3. The latest version of the proposal can be found here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3239.html.
Keen on mastering C++11/14? I've written books focused on C++11/14 features. You can get a VS2013, Clang or a GCC edition.