Windows – How to Delete All Folders with ‘tmp’ in Name Using Batch File

batchdirectorytmpwindows

Have tried to google this, but I always seem to get results relating to .tmp files, which isnt really any use.

I have only a basic understanding of batch files, but I think I need something like this:

rmdir "D:\*.tmp"

Windows update seems to be dumping all of it's .tmp folders onto our D drive, as it has the most free space. Fine, but it doesnt then delete them afterwards.

I'm looking to write a batch file, that will go through and delete any folder that has .tmp at the end.

Best Answer

Throw this one liner into a bat file:

FOR /D /R c:\FOLDERLOCATION %%X IN (*.tmp) DO RMDIR /S /Q "%%X"

  • /D - For Directory
  • /R - Recursive

This will remove the folders ending in tmp and anything under it. If you want to get fancy you can run it like this:

@echo off
set dir="c:\FOLDERLOCATION\"
FOR /D /R %dir% %%X IN (*.tmp) DO RMDIR /S /Q "%%X"
pause
exit
Related Topic