twoyoung
twoyoung

Reputation: 171

find and move files in C++

I'm new to C++ and I've just read <C++ Primer> 4ed. Now I want to implement a little program to help me manage some mp3 files in my computer.

I have a .txt file which includes all the names(part of the names actually) of the files which I want to move(not copy) to a new folder(in the same column). For example, "word" and "file" in the .txt and I want to move all the .mp3 files whose filename contain "word" or "file" to a new folder. Hope my discription is clear, Opps..

I know how to read the strings in .txt into a set<string> and traverse it, but I have no idea how to search and move a file in a folder. I just want to know what else should I learn so that I can implement this function. I read C++ Primer and still I can't do much thing, that's really sad...

Upvotes: 12

Views: 49204

Answers (5)

Juan Carlos
Juan Carlos

Reputation: 161

Another way to move a file in Windows is using the MoveFile function as it is shown in the following code.

std::wstring oldPath = L"C:\\Users\\user1\\Desktop\\example\\text.txt";
std::wstring newPath = L"C:\\Users\\user1\\Desktop\\example1\\text.txt";

bool result = MoveFile(newPath.c_str(), oldPath.c_str());

if (result)
    printf("File was moved!");
else
    printf("File wasn't moved!");

Upvotes: 0

Akash das
Akash das

Reputation: 61

Use rename() function to move a file

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
	char oldname[] = "C:\\Users\\file_old.txt";
	char newname[] = "C:\\Users\\New Folder\\file_new.txt";
	
	/*	Deletes the file if exists */
	if (rename(oldname, newname) != 0)
		perror("Error moving file");
	else
		cout << "File moved successfully";
	
	return 0;
}

Upvotes: 4

Philipp Cla&#223;en
Philipp Cla&#223;en

Reputation: 43969

To move a file in C++, you do not have to use external libraries like Boost.Filesystem, but you can use standard functionality.

There is the new filesystem API, which has a rename function:

#include <iostream>
#include <filesystem>

int main() {
  try {
    std::filesystem::rename("from.txt", "to.txt");
  } catch (std::filesystem::filesystem_error& e) {
    std::cout << e.what() << '\n';
  }
  return 0;
}

The drawback is to compile it, you need a recent C++17 compiler. (I tested it on gcc 8.0.1, and I also needed to link against -lstdc++fs).

But what should work on any C++ compiler today, is the old C API, which also provides rename (cstdio):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>

int main() {
  if(std::rename("from.txt", "to.txt") < 0) {
    std::cout << strerror(errno) << '\n';
  }
  return 0;
}

But note that in both cases, the rename will fail if the source and destination files are not on the same filesystem. Then you will see an error like this:

filesystem error: cannot rename: Invalid cross-device link [from.txt] [/tmp/to.txt]

In that case, you can only make a copy and then remove the original file:

#include <fstream>
#include <iostream>
#include <ios>
#include <cstdio>

int main() {
  std::ifstream in("from.txt", std::ios::in | std::ios::binary);
  std::ofstream out("to.txt", std::ios::out | std::ios::binary);
  out << in.rdbuf();
  std::remove("from.txt");
}

Or with the new API:

#include <iostream>
#include <filesystem>

int main()
{
  try {
    std::filesystem::copy("from.txt", "to.txt");
    std::filesystem::remove("from.txt");
  } catch (std::filesystem::filesystem_error& e) {
    std::cout << e.what() << '\n';
  }
  return 0;
}

Upvotes: 31

Boris Ivanov
Boris Ivanov

Reputation: 4254

under Windows run system call with batch commands:

system("move *text*.mp3 new_folder/");
system("move *word*.mp3 new_folder/");

Under Unix same with shell syntax.

Upvotes: 0

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 11482

The only way for this to work only using std would be to read the file completely using a std::ifstream and then write it to the new location with a std::ofstream. This will however not remove the old file from disk. So basically you create a copy of the file. Its also much slower than a real move.

The optimal solution is to use OS specific APIs like win32 which e.g provide a MoveFile() function. Poco provides an platform independent abstraction of such APIs. See: http://www.appinf.com/docs/poco/Poco.File.html

Upvotes: 1

Related Questions