Move all the files from one directory to another in C++

In this tutorial, we are going to learn how we can move all the files from one directory to another in C++. We will write a C++ program that can do our task.

Here I am going to show you two different ways to move files in C++:

  • Using rename() function (To move files in a new directory)
  • Using system() (To move files to an existing directory)

Using Rename() function

We can use a rename() function in our C++ program to move files from one directory to another directory. It is present in <cstdio> header of C++. The syntax of this function is as follows:

int rename( const char* old_name, const char* new_name)

Here, old_name is a string that specifies the path/name of the directory or file we are going to move to the new one. Whereas, new_name is the path/name of the new directory or file where we are going to move the files. The function returns zero if the file is successfully renamed otherwise it returns a non-zero value.

Few points that must be noted before you run the given example program:

  • The old directory path must be relative to the same directory where the program is saved otherwise you will have to specify the absolute path of the file.
  • The new directory will be created relative to the current directory (the directory where you have saved your program) unless you specify an absolute path/name for the destination. If the new directory name already exists, the compiler will throw an error.
  • After the files have been moved, the old directory/file is removed or deleted.

The example program to move all the files from one directory to another has been given here.

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  string old_name = "f2";
  string new_name = "f1";
  

  if (rename(old_name.c_str(), new_name.c_str()) )
    cout<< "Failure";
  else
    cout << "Success";
  
  return 0;
}

Output:

Success

If the output is “Success” that means files have been moved successfully.

If the old file name does not exist or there is already a file or directory with the new file name or any other error occurs in the system, then the output will be:

Failure

Using system() function

We can also use system() function with proper commands to move files from one directory to another. See the below code for a better understanding. This code is for windows.

#include <iostream>
#include <stdlib.h>

int main()
{
 	system("move \"f1\\*\" \"f2\" ");
 		
 	return 0;
}

The output of the program will be somewhat like this:

C:\Users\Ranjeet Verma\Desktop\New folder\f1\d.rtf
C:\Users\Ranjeet Verma\Desktop\New folder\f1\d2.rtf
        2 file(s) moved.

As you can see, all the files of the f1 directory are moved to f2. If the file specified does not exist, then an error will be thrown and the output will be:

The system cannot find the file specified.

If you are working on Linux then use system() with the command shown in the below code.

#include <iostream>

int main()
{
 	system("mv f1/* f2 ");
 		
 	return 0;
}

Thank you.

Also read: Create, delete, show, rename directory and file in C++

Leave a Reply