Sweeper
Sweeper

Reputation: 477

How to remove all folders with a same name within the folder where is located and subfolders using cmd/batch file

I used a tip on how to do what I want, but I have a difficulty, the folders that begin with "!" ex.: c:\!test and in the middle is "." ex.: c:\test.test are not erased. You can help me?

@Echo OFF
echo.
setlocal enabledelayedexpansion
echo Search...
FOR /R %root% %%A IN (.) DO (
    if '%%A'=='' goto end   
    set dir="%%A"
    set dir=!dir:.=!
    set directory=%%A
    set directory=!directory:.=!
    set directory=!directory::=!
    set directory=!directory:\=;!   
    for /f "tokens=* delims=;" %%P in ("!directory!") do call :loop %%P
)
:end
echo.
echo Finished.
echo Press any key to exit...
pause >nul
endlocal
exit
:loop
if '%1'=='' goto endloop
if '%1'=='history' (
    rd /S /Q !dir!
    echo !dir! was deleted.
)
SHIFT
goto :loop
:endloop

Upvotes: 0

Views: 795

Answers (1)

foxidrive
foxidrive

Reputation: 41257

I think your code checks every folder and if it finds ones called history then they are all deleted.

If that is the task then this should do the same thing.

@echo off
FOR /D /R %root% %%A IN (*) DO if /i "%%~nxA"=="history" if exist "%%A\" rd /s /q "%%A" & echo "%%A" has been deleted

Upvotes: 1

Related Questions