Batch Script to Delete Files in a Folder (2023)


Learn the batch script to delete files from a folder. In this batch script we will learn parameters of del command to delete file or files from a specific folder!

A batch file is a script file that stores commands to be executed in a serial order. It helps automate routine tasks! A scripting language is a programming language that is created to interpret and execute one command at a time. Wish to be a Scripter you have visted the right place.

Batch File Playlist:

Important Links:
🔥 Visit Channel :
🔥 To Subscribe :
🔥 Python Videos :
🔥 UiPath Videos :
🔥 Linkedin :
🔥 Fb UiPath Grp :
🔥 Fb Page :

Thanks for watching, please do subscribe.

Link to UiPath Beginners Playlist:

Link to UiPath Excel and DataTable Playlist:

Link to UiPath Key Technique Playlist:

Link to UiPath Email Automation Playlist:

Link to UiPath PDF Automation Playlist:

Link to UiPath ReFramework Playlist:

Link to UiPath Classic and Modern Excel Activities Playlist:

Link to UiPath Regular Expression Playlist:

Link to UiPath Orchestrator Playlist:

Link to Advance UiPath Orchestrator Playlist:

Link to UiPath VB dot Net PlayList:

Link to UiPath Use Case Playlist:

Link to UiPath Selectors Playlist:

Link to UiPath Enhanced Selectors Playlist:

Link to UiPath Computer Vision Playlist:

Link to UiPath Interview Series Playlist:

Link to UiPath Document Undestanding Playlist:

Link to UiPath Action Center Playlist:

Link to UiPath Error and Fix Playlist:

Link to UiPath Activities Playlist:

Link to UiPath Certification Playlist:

Link to UiPath Background Automation Playlist:

Link to UiPath Logs Playlist:

Link to UiPath GitHub Integration Playlist:

Link to UiPath API Integration Playlist:

Link to Python Playlist:

#batchfile #batchscripting #batchfilecommands


Hey guys in this video we are going to learn.

How do you delete files using bascript and there are a couple of parameters that I can pass couple of switches and I can pass I'm going to talk about that in detail.

This is going to be a very helpful video for you.

So let's move on to our screen and let's get started all right.

So the very first thing what we will do.

We are going to create a folder, we'll name it as demo on the desktop.

Now in this folder we are going to create few files.

So let me create few text documents all right.

So now, I have created few text documents.

So how do I delete it? The command is quite simple.

So what we will do, let's start writing the command and see what is the exact command we have to write.

So the command is quite simple: it's del d, e l, Del or also you can write erase.


There are two commands.

You can Google delete, okay, Del now here I have to delete what these files isn't it.

So, let's say what I will do.

I am going to copy the path of the folder where that file exists and, let's say I would like to point out to a specific file.

Okay, so let's let me create one more file and let me name it as ABC okay, ABC have given, and here I am going to write ABC okay now here is it going to work or not? So if I'm going to save it and I'm going to run this specific script, delete files, okay, I run it.

If I go here, the ABC is still there, okay, so sometime if it is hap.

If that happens to you what you do simply write.

The extension of that file, which is a DOT, is a text file, so I'm writing txt.

So here I am deleting a specific targeted file.

Okay, so let me run this script once again and if I go back, you can see the file has been deleted.


What? If, if you see that file was quite simple, it was simply ABC in this particular file.

There are spaces.

You can see new space, then text, then there is a space.

So if you take this particular file, name, for example, if I just copy this and go to the script and I am going to mention something like this okay, is it going to work so I'm going to save it and I'm going to run it now? If I go back, you can see.

The file has not been deleted.

Whenever you have a space remember in basket, whenever you have space all you do put that in a quotation mark okay.

So when you put that in a quotation mark, it will take that as one block of information and it is going to process it.

So let me save it and now, if I process, if I don't execute, you can see, the file has been deleted.

Okay, now let us create I'm going to show you a few more things.

Okay, now, um, there are a couple of what is happening generally.

The hackers will create such a script and they are going to delete all the files.

Without your knowledge, it will just execute and your files are gone now.

Let's say if you like to get a prompt, you know it should prompt.

You before it it can delete so for that um all I will do.

I will pass a simple switch called p.

What is that P4 P4 problem? You can write in capital letter small letter doesn't matter, okay, lower case uppercase doesn't matter so here.

I am passing p p for prompt.

Now, if you see, if I'm going to run this particular script, what has happened? It is prompting me, hey Rakesh.

Would you like to delete this? Yes or no? Only when, if I say yes, then this new text Dot document will get deleted.

If I see why and hit on enter now, if I go back, you can see the file which was there has been deleted, got it so you learned once uh, no one switch.

Now, let me show you couple of things help if you write help Dell.

What is this useful for deletes one or more files? It is not talking about deleting folders.

It is talking about deleting files such as a text file Excel file, PDF file.

It's not talking about the folder, a folder make directory and remove directory.

There is something called Rd.

I am going to show you later, but it is for deleting files.

Remember that, okay, now there are multiple switches, you have learned the first switch, which is p okay.

Now, what is this F F is for force close okay, if you want to force closer Force delete uh right, some other application has occupied that you would like to force delete.

Then you go for f, okay, all you have to Simply.

Instead of you, can you know it doesn't matter how many switches you like to use? You want to use multiple switches.

You can write this way so if I save this first of all create that file, because we had already deleted that.

So let me create a file and if I go here, let me save this, and if I'm going to run this particular script, you can see it has prompted prompted me and I'm going to say yes to it hit on enter.

So this is going to force close.

We cannot see that until unless there is a practical situation like that, but that is the purpose of it.

Okay, now there is another switch called s.

What is that remember s means subdirectory, slash d means directory, so s means subdirectory okay, so it is going to delete any file present inside the subductor.

Okay, let me show you that what what does that mean if I'm going to create some directory? So let me create a folder okay, so the folder got created, let's say in one of the folder: I am going to create a text document.


This also has the same name and also in the main one I'm going to have.

You know both are having same name.

You can see new text document.

This is two, but this one is same and inside this also, you have a similar name.

Now, if I'm writing a script, saying hey, you go and delete new text document wherever it is wherever you know not only this directory wherever they are inside the subdirectories.

Also, then all I have to do.

Okay, I, don't want to prompt it.

I'll remove that and all I have to Simply write, slash S, you can write in uppercase, lowercase doesn't matter okay, whichever way you want so save it.

Now, if I go and run this, what would happen it got executed now, if I go here, you can see here.

There was a text document that got deleted and if I go to the new folder, even there it got deleted.

So you have understood three different parameters.

One is you have understood: P, F and S.

Now? What is this queue? I am going to show you.

What does this queue is all about quite mode.

What is that so generally, without even writing it.

It generally runs in a quiet mode until unless you write a p right.

So what is this quiet mode? I am going to show you, let's say um.

You know, instead of specifying uh um, let me create few files.

Okay, let's say this folder uh the demo folder contains.

Let me go to the demo folder.

So I have cleared everything.

Let's say: I have created a file here, okay, so inside the demo folder, if I am creating few files.

Okay, so I've created three files here inside this demo folder.

If what would happen if I'm going to pass only this rather than writing the txt file? Name: I'm only passing until here I'm going to remove the file name.

So what would happen you are telling hey whatever? Is there inside that folder go and delete it? Anything that you find you go and delete it? That's what your the instruction you are giving right: okay, uh folder! Also you know inside this demo, so I will remove this as okay I'll write very simple one delete whatever you find inside this, you go and delete it.

So what happens by writing this command because we have not specified a specific file, type or file name.

What would happen by this? If I am going to execute this particular script? Okay, it will automatically prompt you.

So that means you are saying star: star means what anything there inside the folder you delete it.

So that is where it is going to prompt you.

This is called Global, prompt.

Okay, you get a global, prompt and the moment only when you say yes, it is going to delete that and if I go back, it has deleted everything.


Now let me show you if you don't want that prompt.

Also, if you don't want to see that prompt okay, this is again a lot of hackers.

Do it but again um, you know, let's say I, don't want to even see that specific uh prompt.

You know where it is getting giving a prompt to the user.

If you want to hide that all you have to do simply use this parameter queue, so how I will do it I'll simply write forward, slash, Q and save it and run it so last time you saw it it had given me a prompt now, look at it what's going to happen if I'm going to run it the moment, I run it see it disappeared.

If I go back to the demo folder, it is gone getting it.

It did not.

Even given you a prompt so by giving just the directory name, somebody can delete it if they have, you know written now.

This is only for a learning purpose or just to be aware that hackers could write such a script, so do not blindly click on any bad script that you get.

Okay, be very, very careful, try to see what has been written.

If you could open it all right, so you can see it has without even prompting it has deleted my files from the demo folder.

Okay, now um, there are also certain things called attribute that we can pass hidden files, not content, and all those things now there will be also questions like is what if I would like to delete.

Only txt files, uh, you know there are there- could be multiple different file.

For example, if I go to my documents, folder, you can see there are multiple different python files, Excel file text files, so many things are there.

What if I would only like to delete only the text files? Okay, it is pretty simple.

All you have to do.

You know.

Let's say I would like to only delete text file, so I'll put a star, the name could be anything dot txt.

That means the name could be anything but only delete the TX.

You know text files, let's say I- want to only delete the Excel files or I want to delete only the PDF file so like that we can write okay or you want to uh.

You know what is the meaning of this star dot.

Star means the name could be anything.

The extension could be anything or simply by writing star.

Also, it will work okay, simply by putting a star.

Also it's going to do the same job getting it.

If a file has a space, then always remember to code it okay.

Ah sometimes the file I said that in the beginning, right, when you have a space, you have to put it in a code.

So these are couple of important facts that you have learned about: delete, um, okay and these are very, very powerful commands.

You know we can do anything with these commands if you know uh DL and there is also similar command called Erase.

Okay, so let me show you it is how that works.

Uh with the same one uh, all I will do uh.

What is the demo? Folder I closed it? Okay, so I went to the demo folder and let me create a text document here called new text document.

Okay, so this text document, let's say I- would like to delete it.

I'll copy this I'll create one more text: document: okay, but I want to only create delete the erase the first one.

So what I will do I'll, just instead of Del I will say: erase okay, erase, okay and uh, not in quiet mode erase and then I'll provide that specific file.

Dot, txt and I have to put a double quote so that, because it has a space right, the file has a space in the name.

So if I do this, and if I'm going to hit on delete and if I go to the demo folder, you can see that got deleted and the explanation also given here clearly: okay um, for example, um if I say, help erase okay.

The definition also says the same thing: okay deletes one or more files getting it, and you have got a lot of examples: how to utilize.

It uh, you know um, and you have learned how this command works.

So thank you very much.

Let's move on to our next uh video before that.

Please do like and do share our videos to other people who like to learn and after completing any of this topic, please say complete it so that I know people are watching and taking you know, benefit from these topics.

Thank you guys.

Let's move on to our next topic.


How to delete all files in a folder using batch script? ›

Batch Script - Deleting Files
  1. Names. Specifies a list of one or more files or directories. ...
  2. /P. Prompts for confirmation before deleting each file.
  3. /F. Force deletes of read-only files.
  4. /S. Deletes specified files from all subdirectories.
  5. /Q. Quiet mode, do not ask if ok to delete on global wildcard.
  6. /A.

How to delete files in a folder cmd script? ›

After reaching the desired folder, use the del command, followed by the file name.
  1. The syntax for deleting a file is: del “filename"
  2. The syntax for force deleting a file is: del /f “filename"
  3. del file1 file2 file3 file4.
Jun 27, 2023

How to delete all files in a folder in shell script? ›

To delete everything in a directory run: rm /path/to/dir/* To remove all sub-directories and files: rm -r /path/to/dir/*

What is the batch command to delete all files in a folder except one? ›

The for/d command deals with all of the directories, except that where the name+extension matches the required name, then the for repeats the action on files in the target directory, deleteing all except that which matches the required filename-to-keep.

How do I delete all files in a folder? ›

To delete all of the files in the current directory, press Y and then press ENTER. To cancel the deletion, press N and then press ENTER. Before you use wildcard characters with the del command, use the same wildcard characters with the dir command to list all the files that will be deleted.

How to batch delete all files in folder recursively? ›

To delete files or folders recursively, you can utilize Windows Command Prompt or Windows PowerShell. In the Command Prompt, use “rmdir” or “rd” commands with the “/s” option is used to remove folders recursively.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated: 14/11/2023

Views: 5261

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.