DOS batch for deleting files with directory with regex

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
giridhar276
Posts: 1
Joined: 23 Sep 2015 10:05

DOS batch for deleting files with directory with regex

#1 Post by giridhar276 » 23 Sep 2015 10:23

I am newbie to DOS .... I am aware of all basic commands of DOS and not sure how to build a batch ..

In the below path .. I am looking to delete all files in the below path in c:\users\ which has the appdata\temp directory structure.

* includes all directories.

C:\users\*\appdata\temp\*

Could anyone please help me in deleting those such files and directoreis .

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: DOS batch for deleting files with directory with regex

#2 Post by Squashman » 23 Sep 2015 10:51

Code: Select all

@echo off
pushd C:\users
for /F "delims=" %%G in ('dir /b /ad /s ^|findstr /I /E "appdata\temp"') do rmdir /s /q "%%~G"
popd

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch for deleting files with directory with regex

#3 Post by foxidrive » 23 Sep 2015 17:10

This is based on Squashman's code with a couple of extra bits.

It checks for the location of the USERS folder, and leaves the temp folder in place.
It also removes the expected error messages, that can occur, from the screen.


Code: Select all

@echo off
:: deletes files and folders in all user accountsin "appdata\local\temp" untested

for %%A in ("%USERPROFILE%") do (
   for /
"delims=" %%G in ('dir "%%~dpA" /b /ad /s ^|findstr /I /E "appdata\local\temp"') do (
           echo 
removing files and folders in "%%~G"
           
pushd "%%~G" && (rd //"%%~G" >nul 2>&popd)
  )
 )
pause

Post Reply