How to Create a Batch File to Automatically Delete Temp Files (with Pictures)

A computer with screen displaying batch file. How to create a batch file, how to delete temp files (%TEMP%) automatically

To reclaim disk space on your Windows computer, you can eliminate temporary files stored in the %TEMP% folder by creating a batch file.

These files (%TEMP%), generated by various programs, become unnecessary once the programs are closed.

Although you can manually delete them, the process is often tedious and time-consuming.

A more efficient approach is to craft a batch file script that automates the deletion of temporary files.

Note: The batch file script provided herein was only been tested on Windows 10. And it worked. It does not carry any malware; therefor, it is safe to use—but at your own risk.

What is a Batch File?

A batch file (or bat file) is a type of script file that contains a series of commands or instructions to be executed by a command-line interpreter or shell.

Batch files are commonly used in Windows, DOS and OS/2 operating systems to:

  • automate tasks,
  • run programs, or
  • perform operations without user intervention.

How Batch Files Work

A batch file is a plain text file that has a .bat or .cmd extension. The file name can be anything, as long as it ends with one of these extensions.

The commands or instructions inside the batch file are usually written in the same syntax as the command-line interpreter or shell that will execute them.

For example, in Windows, the default command-line interpreter is cmd.exe, which uses the same commands as the MS-DOS Prompt.

In OS/2, the default command-line interpreter is cmd.exe, which uses the same commands as the OS/2 Prompt.

When a batch file is run, the command-line interpreter or shell reads the file line by line.

And executes each command or instruction as if it were typed manually by the user.

Some batch files may use special commands or constructs that are specific to batch files and provide basic programming functionality, such as:

  • variables,
  • conditional statements,
  • loops,
  • labels, and
  • subroutines.

These commands or constructs allow the batch file to perform more complex tasks, such as user input, output redirection, error handling, branching, and looping.

For example:

  • The set command:- to create or modify variables that store values or strings.
  • The if command:- to execute different commands based on a condition.
  • The for command:- to iterate over a set of items or files.
  • The goto command:- to jump to a specific label within the batch file.
  • The call command:- to invoke another batch file as a subroutine.

How to Create and Run Batch Files

To create a batch file, you need a text editor that can save files in plain text format, such as Notepad.

You can use any text editor you prefer, but some text editors may have features that make writing batch files easier, such as syntax highlighting, auto-completion, or error checking.

To write a batch file, enter the commands or instructions that you want to execute in the order that you want them to run.

You can also use special commands or constructs that are specific to batch files, such as variables, conditional statements, loops, labels, and subroutines.

To add notes or explanations to your batch file, start a line with the rem command or a double colon (::).

To save a batch file, give it a name that ends with .bat or .cmd extension and choose a location where you want to store it. Use descriptive names that indicate what the batch file does.


Here is how to create a batch file to automatically delete temp files (older than 5 days) weekly:

Step 1: Open Notepad

Open Notepad by pressing Windows + R keys, typing Notepad and hitting Enter.

Step 2: Copy Code in the Notepad

Copy and paste the following code into Notepad:

This batch file uses the forfiles command to delete files older than 5 days in the %TEMP% folder. You can change the number of days to whatever you want.

The /s switch makes it search the subdirectories as well.

The /m *.* switch specifies that all files should be deleted.

The /d -5 switch specifies that files older than 5 days should be deleted.

The /c "cmd /c del @path" switch specifies the command to execute for each file found.

Step 3: Save the file with .bat extension

Save this code in a text editor with a .bat extension, e.g. Clean.bat or DelTemp.bat, etc.

Select “All Files” as file type and UTF-8 encoding, then click save.

Here is the path to the bat file that I’ve created:

C:\Users\user\Documents\Clean.bat

Important!

Skip step 4 if you want to schedule the process to take place automatically.

Step 4: Run

Run it manually by right-clicking on the file and selecting Run as Administrator.

How to run a batch file manually

You can use the Task Scheduler to schedule the process to run automatically (say weekly). Skip Step 4 and jump to step 5.


Step 5: Go to the Task Scheduler

Open the Start menu and search for Task Scheduler. Click on the top result to open the app.

Task Scheduler, Batch File automation

Step 6: Create a New Folder

Right-click the Task Scheduler Library branch and select the New Folder option. Type a name for the folder, for example, MyTasks.

Create a basic task in the task scheduler

It’s optional to rename your new folder. Click the OK button.

Step 7: Create Basic Task in your Folder

Expand the Task Scheduler Library branch and select the MyTasks folder. Click the Action menu and select the Create Basic Task option.

In the Name setting, type a descriptive name for the task, for example, Delete Temp Files. In the Description setting, create a description for the task (optional). Click the Next button.

Creating a basic task in the task scheduler. How to create a batch file

Step 8: Specify when to run the task

Select the Weekly option. Click the Next button.

Task Scheduler, Create basic task weekly, how to create a batch file

Using the Start settings, specify when the task should run and the time (very important).

Use the Weekly drop-down menu to choose the days of the week you want to run the task.

For example, if you want to run the task every Sunday at 12:00 AM, select Sunday and set the time to 12:00 AM. Click the Next button.

Task scheduler, task setting, how to create a batch file, how to automate deleting of temp files

Step 9: Enter the path to the batch file

Select the Start a program option. Click the Next button.

In the Program/script field, enter the path to the batch file you created earlier. For example, C:\Users\Username\Documents\DeleteTempFiles.bat.

Task scheduler-program/script. How to create a batch file, how to run a batch file

You can browse for path while in the open window.

Alternatively, you may navigate to the bat file and press shift and hold. Right-click and copy the path. Remember to remove the quotation marks on the copied path.

Click the Next button.

Review the task summary and click the Finish button.

Task scheduler final stage. How to create a batch file

That’s it! Your task will now run automatically every week at the specified time and delete temporary files older than 5 days.


Advantages and Limitations of Batch Files

Batch files have several advantages and limitations when compared to other methods of automating tasks, running programs, or performing operations on Windows and other operating systems.

Some of the advantages of batch files are:

  • They are easy to create and edit with any text editor.
  • They are portable and compatible with different versions of Windows and other operating systems.
  • They do not require any additional software or installation to run.
  • They can execute multiple commands or instructions in sequence without user intervention.
  • They can use variables, conditional statements, loops, labels, and subroutines to perform more complex tasks.
  • They can accept user input, redirect output, handle errors, and pass parameters or arguments.

Some of the limitations of batch files are:

  • They are not very secure and can be easily modified or deleted by anyone who has access to them.
  • They are not very efficient and can consume a lot of system resources or cause performance issues when running.
  • They are not very flexible and can only use commands or instructions that are supported by the command-line interpreter or shell that runs them.
  • They are not very user-friendly and can have a steep learning curve for beginners or non-technical users.
  • They are not very robust and can fail or produce unexpected results when encountering errors or exceptions.

Final Thoughts on Batch File

If you want to streamline the deletion of temporary files in the %TEMP% directory, this guide provides a helpful solution.

The advantage lies in the flexibility of the batch file script, which can be easily adjusted to suit your specific needs. Furthermore, you can edit the task scheduler to automate the deletion of %TEMP% files upon startup, login, or at any desired frequency.

However, exercise extreme caution when executing batch files. Unauthorized modifications can occur without your knowledge, potentially causing significant harm to your computer when run.


Discover more from Biztech Lens

Subscribe to get the latest posts to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error

Enjoy this blog? Please spread the word :)

Discover more from Biztech Lens

Subscribe now to keep reading and get access to the full archive.

Continue reading