• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 60
  • Last Modified:

Batch File to delete files not equal to

I need to create a small batch file to clean up a directory nightly.
I want to delete all files that are not *.DAT
what is the Syntax for not equal to ?

  • 2
1 Solution
This works. Make a .bat file of this code. Open a CMD window in the folder. It is currently in test mode for debug reasons so you can see what would happen. To run it for real, remove the ECHO word:
for %%f in (*) do if /i not %%~xf==.dat ECHO del "%%f"

Open in new window

If you want to include subfolders:
for /r %%f in (*) do if /i not %%~xf==.dat ECHO del "%%f"

Open in new window

Solution given
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now