The Boost.Filesystem library provides facilities to manipulate files and directories, and the paths that identify them.
Questions tagged [boost-filesystem]
353 questions
54
votes
4 answers
how to perform boost::filesystem copy_file with overwrite
The Windows API function CopyFile has an argument BOOL bFailIfExists that allows you to control whether or not you want to overwrite the target file if it exists.
The boost::filesystem copy_file function has no such argument, and will fail if the…

Dani van der Meer
- 6,169
- 3
- 26
- 45
34
votes
7 answers
Get relative path from two absolute paths
I have two absolute filesystem paths (A and B), and I want to generate a third filesystem path that represents "A relative from B".
Use case:
Media player managing a playlist.
User adds file to playlist.
New file path added to playlist relative to…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
34
votes
2 answers
boost directory_iterator example - how to list directory files not recursive
How should I use directory_iterator to list directory files (not recursive)?
Also what header files / libs should I add/link or other settings I should make? I'm using boost in my project but by some reason directory_iterator is "underclared…

Oleg Vazhnev
- 23,239
- 54
- 171
- 305
33
votes
6 answers
boost::filesystem get relative path
What methods of the boost::filesystem library can help me to get a path relative to another path?
I have a path /home/user1/Downloads/Books and a path /home/user1/. Now I want to get a path Downloads/Books.

itun
- 3,439
- 12
- 51
- 75
32
votes
9 answers
expand file names that have environment variables in their path
What's the best way to expand
${MyPath}/filename.txt to /home/user/filename.txt
or
%MyPath%/filename.txt to c:\Documents and settings\user\filename.txt
with out traversing the path string looking for environement variables directly?
I see that…

Dan
- 1,339
- 3
- 13
- 19
31
votes
3 answers
How similar are Boost.Filesystem and the C++ standard filesystem library?
I need a filesystem library for use with a C++11-capable compiler or a C++14-capable one - so it can't be be from C++17.
Now, I know that the filesystem library going into C++17 is based based on Boost::Filesystem; but - are they similar enough…

einpoklum
- 118,144
- 57
- 340
- 684
29
votes
2 answers
What is the C++17 equivalent to boost::filesystem::unique_path()?
std::filesystem on C++17, and std::experimental::filesystem for many pre-C++17 compilers, are based on boost::filesystem and almost all of it is obvious to port to the newer std.
But I see no std::filesystem equivalent to…

Larry Gritz
- 13,331
- 5
- 42
- 42
29
votes
4 answers
C++ BOOST undefined reference to `boost::filesystem::detail::copy_file
I have no clue why boost::filesystem::copy_file is making trouble for me.
undefined reference to `boost::filesystem::detail::copy_file
// g++ -std=c++11 test.cpp -lboost_filesystem -lboost_system -lrt -lboost_wave
#include…

ar2015
- 5,558
- 8
- 53
- 110
28
votes
1 answer
boost::filesystem exists() on directory path fails, but is_directory() is ok
I'm getting path to current directory with boost filesystem, then checking if the directory exists.
is_directory() is ok, but exists() fails on the same path, am I missing something?
Example code (boost 1.35):
#include…

stefanB
- 77,323
- 27
- 116
- 141
21
votes
2 answers
Why is there no boost::filesystem::move_file?
I'm using boost filesystem to replace windows C++ functions like CopyFile and MoveFile to get some kind of portability between windows and linux. I'm using copy_file but I have not been able to find anything that moves files like a 'move_file'…

molholm
- 1,992
- 3
- 25
- 29
19
votes
3 answers
Obtain platform's path separator using Boost.Filesystem
Is there a way to obtain the platform's path separator character using Boost.Filesystem? By path separator, I mean / for Unix and \ for Windows.
I already know I can use boost::filesystem::path::operator/ to concatenate two paths together with the…

Emile Cormier
- 28,391
- 15
- 94
- 122
19
votes
6 answers
Boost.Filesystem how to find out in which directory your executable is?
So I run my app. I need for it to know where its executable is. How to find path to it using Boost.Filesystem?

Rella
- 65,003
- 109
- 363
- 636
19
votes
4 answers
parent_path() with or without trailing slash
As explained in the documentation, the expected output of the following is:
boost::filesystem::path filePath1 = "/home/user/";
cout << filePath1.parent_path() << endl; // outputs "/home/user"
boost::filesystem::path filePath2 = "/home/user";
cout…

David Doria
- 9,873
- 17
- 85
- 147
19
votes
1 answer
How to avoid removing directory on remove_all with Boost Libraries?
I'm using boost::filesystem::remove_all operation to remove the content of a directory.
It removes correctly the content, but, as state by Boost Filesystem Documentation, it also removes the directory itself.
Is there an easy way to stay with the…

Santi Agüero
- 3,143
- 8
- 27
- 39
18
votes
3 answers
Iterate over all files in a directory using BOOST_FOREACH
Can you iterate over all files in a directory using boost::filesystem and BOOST_FOREACH?
I tried
path dirPath = ...
int fileCount = 0;
BOOST_FOREACH(const path& filePath, dirPath)
if(is_regular_file(filePath))
++fileCount;
This code…

Johan Råde
- 20,480
- 21
- 73
- 110