How to remove all folders of name x within a direc

2019-03-27 00:05发布

I have a folder named x with a number of subfolders and files. I want to delete a folder named y that is present in x and all of it's subfolders. The said folder that has to be deleted may or may not contain any files. I believe i can do this using cmd or some kind of batch file but i am a command line new bi and can really use some help.

A simple thing would be to rd the folder's name, which works but i believe there are better ways than removing each folder individually.. like some loop that goes through all the folders.

Thanks

EDIT: Just to clarify, i have y (the folder that needs to be deleted) inside of x, and it can be in any of x's subfolders and at any level of depth. Also i am looking at answers and it may take some time for me to accept any answer. Please bear with me :)

4条回答
The star\"
2楼-- · 2019-03-27 00:26

A problem common to this type of topics is that if there are instances of the target folder at several levels, most methods cause an error because when an high level folder is deleted, all folders below it disappear. For example:

C:\X\Y\subfolder
C:\X\Y\subfolder\one\Y
C:\X\Y\subfolder\two\Y
C:\X\Y\subfolder\three\Y
C:\X\test
C:\X\test\test

Previous example generate a list of 4 folders named Y that will be deleted, but after the first one is deleted the three remaining names no longer exist, causing an error message when they are tried to delete. I understand this is a possibility in your case.

To solve this problem the folders must be deleted in bottom-up order, that is, the innermost folder must be deleted first and the top level folder must be deleted last. The way to achieve this is via a recursive subroutine:

@echo off
rem Enter into top level folder, process it and go back to original folder
pushd x
call :processFolder
popd
goto :EOF

:processFolder
rem For each folder in this level
for /D %%a in (*) do (
   rem Enter into it, process it and go back to original
   cd %%a
   call :processFolder
   cd ..
   rem If is the target folder, delete it
   if /I "%%a" == "y" (
      rd /S /Q "%%a"
   )
)
exit /B

Although in this particular case the problems caused by other methods are just multiple error messages, there are other cases when this processing order is fundamental.

查看更多
来,给爷笑一个
3楼-- · 2019-03-27 00:45
FOR /D /R %%X IN (fileprefix*) DO RD /S /Q "%%X"

Take care of using that...

for RD command:

/S      Removes all directories and files in the specified directory
        in addition to the directory itself.  Used to remove a directory
        tree.
/Q      Quiet mode, do not ask if ok to remove a directory tree with /S

the FOR command is used to loop through a list of files or variables, the options are very easy to memorize, Directory only Recursively.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-27 00:49

Here is another solution for this commented to describe each part of the script:

@Echo OFF
REM Important that Delayed Expansion is Enabled
setlocal enabledelayedexpansion
REM This sets what folder the batch is looking for and the root in which it starts the search:
set /p foldername=Please enter the foldername you want to delete: 
set /p root=Please enter the root directory (ex: C:\TestFolder)
REM Checks each directory in the given root
FOR /R %root% %%A IN (.) DO (
    if '%%A'=='' goto end   
    REM Correctly parses info for executing the loop and RM functions
    set dir="%%A"
    set dir=!dir:.=!
    set directory=%%A
    set directory=!directory::=!
    set directory=!directory:\=;!   
    REM Checks each directory
    for /f "tokens=* delims=;" %%P in ("!directory!") do call :loop %%P
)
REM After each directory is checked the batch will allow you to see folders deleted.
:end
pause
endlocal
exit
REM This loop checks each folder inside the directory for the specified folder name. This allows you to check multiple nested directories.
:loop
if '%1'=='' goto endloop
if '%1'=='%foldername%' (
    rd /S /Q !dir!
    echo !dir! was deleted.
)
SHIFT
goto :loop
:endloop

You can take the /p out from in front of the initial variables and just enter their values after the = if you don't want to be prompted:

set foldername=
set root=

You can also remove the echo in the loop portion and the pause in the end portion for the batch to run silently.

It might be a little more complicated, but the code can be applied to a lot of other uses.

I tested it looking for multiple instances of the same foldername qwerty in C:\Test:

C:\Test\qwerty
C:\Test\qwerty\subfolder
C:\Test\test\qwerty
C:\Test\test\test\qwerty

and all that was left was:

C:\Test\
C:\Test\test\
C:\Test\test\test\
查看更多
看我几分像从前
5楼-- · 2019-03-27 00:51

Make .bat with following:

del /q "C:\Users\XXX\AppData\Local\Temp\*"

FOR /D %%p IN ("C:\Users\XXX\AppData\Local\Temp\*.*") DO rmdir "%%p" /s /q
查看更多
登录 后发表回答