You are not logged in.

#1 03 Apr 2014 05:54

jewel86
New Member
Registered: 03 Apr 2014
Posts: 1

CMD to delete all folders that contain word "temp" in the folder name

I need a command to loop through all sub-folders in a given folder, and remove the folders that contain word "temp" in the names. However, I do not remove folders that say "template" or "attempt".

My current code is like this (removes folders like "temp"), but i don't know how to keep the ones that say "template" and "attempt":

cd /d "C:\Users\jz\Desktop\Orig\"
del "%i">> "C:\Users\jz\Desktop\log.txt")
for /r /d %i in (*temp*) do (echo Delete Folder %CD%\%i >> "C:\Users\jz\Desktop\log.txt"
rd /s /q "%i" >> "C:\Users\jz\Desktop\log.txt")

Thanks in advance!

Offline

#2 03 Apr 2014 06:12

Honguito98
Member
From: Mexico
Registered: 19 Sep 2013
Posts: 57

Re: CMD to delete all folders that contain word "temp" in the folder name

@Echo off
:: Set Remove to 'MyFolderName' for delete it.
Set Remove=Tmp

	:: Set redirect to 'Con' for show in screen results.
	:: Set redirect to 'MyFile.txt' for log to file.
	Set Redirect=Con

	Set/p "Root=Drag and drop a folder to search Temporal folders:"

For /F "Delims=" %%a in ('Dir /s /b %Root%\%Remove%') Do (
	Echo; _
	Echo;/!\ Removing %%a
	Echo;~~~
	Rem Uncomment next line for delete these files:
	Rem Rd /s /q "%%~a" 2>Nul
) >>"%Redirect%"

Echo;&Echo;Finished&Echo;Press any key to exit...
Pause >Nul

Last edited by Honguito98 (03 Apr 2014 06:14)


.::{Honguito98}::.

Offline

#3 03 Apr 2014 06:47

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: CMD to delete all folders that contain word "temp" in the folder name

This should do it - remove the echo when you are happy with the output.

@echo off
for /d /r "c:\base\folder" %%a in (*temp*) do if /i "%%~nxa"=="temp" echo rd /s/q "%%a"
pause

Last edited by foxidrive (03 Apr 2014 06:50)

Offline

#4 03 Apr 2014 08:21

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: CMD to delete all folders that contain word "temp" in the folder name

You could try this:

@echo off
set "log=C:\Users\jz\Desktop\log.txt"
set "root=C:\Users\jz\Desktop\Orig"
set "search_terms=temp tmp"
for %%F in (%search_terms%) do (
  for /f "usebackq tokens=*" %%i in (`dir /b/s/a:d "%root%\%%~F"`) do (
     echo Delete Folder "%%~i" >&2
     echo Delete Folder "%%~i"
     REM remove "echo" below when happy
     echo rd /s /q "%%~i" 1>&2
  ) 2>>"%log%"
)

Exact same idea as Honguito98 but allows for multiple search terms and won't try to remove (and thus error on) files matching your search term(s).

Last edited by bluesxman (03 Apr 2014 08:27)


cmd | *sh | ruby | chef

Offline

#5 03 Apr 2014 14:33

einstein1969
Member
From: Italy
Registered: 29 Dec 2013
Posts: 25

Re: CMD to delete all folders that contain word "temp" in the folder name

For previous answer.

My current code is like this (removes folders like "temp"), but i don't know how to keep the ones that say "template" and "attempt"

I think that the list of folders to delete is like this:

(*temp*.*) AND NOT(*template*.* OR *attempt*.*)

einstein1969

Last edited by einstein1969 (03 Apr 2014 14:36)

Offline

Board footer

Powered by