windowsbatch-filetemp

How to delete specific name pattern directories in temp folder


I need to write a windows bat program to delete particular folders with naming pattern scoped_dir45666,scoped_dir45667 ...so on (for example)(including contents) with batch program. For that I am using below code:

@echo off
Taskkill /IM chromedriver.exe /F 
Taskkill /IM chrome.exe /F
cd /D %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

but its deleting everything under %temp% causing system issues...(its screwing some os files also I guess)

I need to delete all directories names starting with scoped_dirxxxxx (scoped_dir*) under %temp% directory of my user can some one advise how to modify above code to delete only folders name starting with scoped_dir in %temp% folder


Solution

  • Just add the prefix in front of the wildcard:

    for /d %%D in (scoped_dir*) do rd /s /q "%%D"
    

    If you only want to delete these directories, you should also get rid of the last line del /f /q * as it will delete all files on the root level of your %TEMP% directory.