Rename a file using Boost::filesystem

Hi Everyone,

I am new to C++ development and i have written a program to rename a file in a directory, while build no errors and no print messages in the console while execution and the filename was not renamed.

Please find below my code and let me know, if you require any additional information to provide a solution.

#include <iostream>
#include <iterator>
#include <algorithm>
#include "boost/filesystem.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/utility.hpp"

using namespace std;

namespace fs = boost::filesystem;
using boost::filesystem::path;

int main(int argc, char *argv[])
{
string platform(BOOST_PLATFORM);

cout << "There are " << argc << " arguments:" << endl;


for (int nArg=0; nArg < argc; nArg++)
cout << nArg << " " << argv[nArg] << endl;


platform = ( platform == "Win32" || platform == "Win64" || platform == "Cygwin" ) ? "Windows" : "POSIX";
cout << "Platform is " << platform << endl;


fs::path filepath(argv[1]);




string epub_filename = filepath.filename();

string epub_dirname = filepath.parent_path().string();




string zip_filename = filepath.stem() + ".zip";

fs::rename(filepath, epub_dirname + "/" + zip_filename);

cout << epub_filename << endl;
cout << epub_dirname << endl;
cout << zip_filename << endl;


return 0;
}
----

argv[1] Input: D:\epub\test.epub

Build:


10:16:55 **** Rebuild of configuration Debug for project Argc ****
Info: Internal Builder is used for build
g++ "-IC:\\Libraries\\boost_1_42_0" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\Argc.o" "..\\src\\Argc.cpp"
g++ "-LC:\\Libraries\\boost_1_42_0\\stage\\lib" -o Argc.exe "src\\Argc.o" -lboost_system-mgw47-1_42 -lboost_filesystem-mgw47-mt-d-1_42

10:16:58 Build Finished (took 3s.743ms)

console: <terminated> Arc.exe [C/C++ Application] D:\Argc\Debug\Argc.exe

----

Also, i have written a simple program using boost::filesystem to create a new directory - No errors in the program but no new directory in the location.


Thanks in Advance
Elango

Last edited on
Ok, you are using mingw4.7 and boost 1.42 and I'm using mingw 4.6 and boost 1.47 - I had to adjust a couple of lines- but it basically worked for me.

I did make sure that the file to be renamed was in a directory that I had read/write access.
Thank you for your support.

I have fixed the issue by replacing the file boost_system-mgw47-1_42 with boost_system-mgw47-mt-d-1_42 as it is a debug build.

Topic archived. No new replies allowed.