16

I'm using the Boost Filesystem library. I have a path

boost::filesystem::path P("/foo/bar.baz");

I want to modify the stem part of path p to "bar_quz", so path P stays

/foo/bar_quz.baz

Can someone help me? Thanks

ireallydontknow
  • 173
  • 1
  • 1
  • 6
  • Do you want to append "_quz" to the base part of the filename, or do you just want to replace it entirely? – zdan Oct 31 '13 at 17:26

1 Answers1

13
const std::string rndString = "quz";
boost::filesystem::path newPath = P.parent_path() / boost::filesystem::path(P.stem().string() + "_" + rndString + P.extension().string());
Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
  • Not quite - that would end up with "/foo/bar_quz/.baz" – benjymous Oct 31 '13 at 17:10
  • I think it's more "I want to append something to the filename (no matter what it is) while preserving the extension, rather than "I know exactly what I want and add an extension". So taking a whole directory worth of files, and adding a certain thing to their filenames. So imagine the "Base" filename as a variable too. – Kevin Anderson Oct 31 '13 at 17:29
  • Yeah, Kevin is right about that. I want to append some dynamic string. Maybe this could work? boost::filesystem::path newPath = P.parent_path() / (boost::filesystem::path(P.stem().string() + "_" + rndString ).string() + P.extension().string()); – ireallydontknow Oct 31 '13 at 19:29