You are not logged in.
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
@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
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
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
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