Quick Tip: Create a Batch File to Delete Files or Folders

In a few recent guides on this site, I have looked at how to use a batch file to backup files and how to essentially create a batch wait command. In this guide, I wanted to take a look at how to use a batch file to delete files or folders.

There are a number of reasons why you may want to do this. Maybe you want to automatically clean out a temporary folder. Or, perhaps you want to automatically delete content you get often such as  podcasts or videos. Let’s take a look at how you can create a batch file to delete these files.

Creating a batch file

Creating a batch file is pretty simple if you haven’t done it before. Simply open up notepad and save the file as a .bat file. For example, if you are using this file to delete temporary files, you could name it, “deltemp.bat” or something like that. The file you just saved is a batch file. Now, let’s add a few commands.

Batch Del Command

You can use the batch del command along with the cd command to delete files or folders. Before we use the del command, let’s use the cd command to navigate to the correct directory. Let’s say that we want to delete files from “c:\dom\temp”. We would first use “cd c:/dom/temp” to navigate to the correct directory. Now, we can use the command “del *.* /Q”. The parameter /Q tells the del command to not ask us for every file if it is OK to delete. Here is what it our batch file now looks like:

cd "C:\dom\temp"

del *.* /Q

You can save this. Now, if you run it, it will delete all files from the c:\dom\temp folder.

Schedule Your Command

If you want to take this one more step, you can have your command run on a schedule. For example, I may want to delete my temporary folder files every week. In newer versions of Windows, just type “task scheduler” into the search bar. In older versions of Windows, you can schedule a task by going to Control Panel->Performance and Maintenance->Scheduled Tasks. Follow the wizard to schedule your batch file to run weekly (or how ever often you want it to run).

(Don't worry, we won't spam you)