Deleting files exactly in windows cmd or .bat

Background

I recently needed to delete a set of files after compiling them using a .bat file script to help with a build process. The previous step in the process generated .jsxbin from jsx files. These are compiled binary versions of plain-text ExtendScript source code. I used the VSCode ExtendScript Debugger extension which has very limited capabilities. It moves recursively through a directory provides the jsxbin files in the same folder as the jsx, without deleting the jsx files. There isn’t an option to do anything different so, I deal with it.

Initial thought

I’ve been in software for quite a while and never thought this problem would be so hard. My initial thought was to use this:

del /S *.jsx

Delete recursively any file that ends in .jsx…

Wrong. It also deleted the jsxbin files. The Windows operating system has been around for a while but apparently it still doesn’t have a quick command to delete exactly what you tell it to.

This isn’t a new problem, I thought to myself. There must be an equally simple way to tell it not to delete files with an extension other than jsx.

Nope. At least, I never found it.

The Solution

So, based on what I found after reading various Stack Overflow posts, I wrote a bat file just for this purpose. It needs some modification to make it more generic but for now, it does what’s necessary.

@echo off
pushd %1
for /f "eol=: delims=" %%F in ('dir /b /s *.jsx^|findstr /lie ".jsx"') do del %%~fF
popd

That’s right. All that is to replace del /S *.jsx.

Saving that in a bat file called delext.bat, I can now enter something like

delext.bat C:\path\to\directory

and it will delete all the jsx files it finds, recursively, without deleting the jsxbin.

If I have the time, I’ll update this post to show a more generic version.