Delete files of a particular type in C++

In this tutorial, we will come to know how to delete files of a particular type in C++. Many times, we need to delete multiple files of the same extension. But deleting so many files by selecting it is quite tedious. So through a simple C++ program, we can do it easily. We will delete all the files of a given extension present in a particular directory. So let’s start learning the commands to delete all files of the same extension.

System command to delete files of a particular type in C++

To delete files of particular extension we need to call system() function. It executes a system command to delete the files of a given extension. The command depends on the operating system which you are using. So, we will learn how to delete files of a particular extension in two systems.

  • Linux operating system
  • Windows operating system

In the Linux operating system, the command to delete all files of particular extension is –

rm *.file_extension

For example – rm *.txt
This command will delete all the text files from the current directory.

In Windows operating system, the command to delete all files of particular extension is –

del *.file_extension

For example – del *.jpeg
This command will delete all the jpeg image files from the current directory.

Command creation using C++ string

So, now we know the command. We will create a command for Linux operating system. So, we have to take the extension from the user. Then we have to concatenate the extension in our command. Then we will pass this command to system() function as an argument. The code snippet to create command is –

char extension[10],cmd[50];
cout<<"\nENTER EXTENSION OF FILES : ";
cin>>extension;
strcpy(cmd,"rm *.");
strcat(cmd,extension);

Program to delete files of particular extension using C++

So, the C++ program which deletes all the files of a particular extension is given below. This program executes a system command of the Linux operating system.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
  char extension[10],cmd[50];
  cout<<"\nENTER EXTENSION OF FILES : ";
  cin>>extension;
  strcpy(cmd,"rm *.");
  strcat(cmd,extension);
  system(cmd);
  return 0;
}

So, this is the program that deletes all the files of extension intended by the user. The system() function executes a system command which deletes the desired files.

C++ program output

After executing this C++ program, the files of the given extension get deleted from the directory where the program is stored. For your better understanding, I will show you the following –

  • the directory files before deletion
  • the output of the terminal
  • the directory files after executing this C++ program

Directory before executing program

Before executing this program the directory files are –

Delete files of a particular type in C++

I have highlighted the text files which are to be deleted after executing the program. Now let’s execute the program.

The output of Linux terminal

After executing this C++ program in Linux operating system you will get the following output –

[email protected]:~/intern$ g++ filedel.cpp
[email protected]:~/intern$ ./a.out

ENTER EXTENSION OF FILES : txt
[email protected]:~/intern$

The user has given the extension ‘txt’. So, all the text files must be deleted from the current directory. Let’s see the directory contents.

Directory after executing program

After executing this program, all the text files are deleted from the directory. Now, the directory contents are –

Delete files of a particular type in C++

So, you can see that there is no text file present in this directory after executing the C++ program.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *