如何使用CMD /批处理文件来删除一个目录中的名称x的所有文件夹 [英] How to remove all folders of name x within a directory using cmd/batch file

查看:544
本文介绍了如何使用CMD /批处理文件来删除一个目录中的名称x的所有文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为x与多个子文件夹和文件的文件夹中。我想删除一个名为Ÿ文件夹,即以x present和它所有的子文件夹。具有要被删除的所述文件夹可以或可以不包含任何文件。我相信我能做到这一点使用CMD或某种批处理文件,但我是一个命令行新的BI,可以真正使用一些帮助。

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.

一个简单的事情将是RD文件夹的名字,这工作,但我相信有比单独删除每个文件夹..像一些循环通过所有的文件夹去更好的方式。

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.

感谢

编辑:只是为了澄清,我有x的内部Y(需要的文件夹被删除),并且它可以在任意的x的子文件夹中,并在任意的深度。另外,我在寻找答案,这可能需要一些时间,我接受任何答案。请多多包涵:)

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 :)

推荐答案

下面是评论描述脚本的每个部分为这个另一种解决方案:

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

您可以在 / P> / code>出从初始变量的前面, = 如果你不想被提示:

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.

这可能是一个有点复杂,但code可以应用到很多其他用途。

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

我测试了它在ç寻找同样FOLDERNAME QWERTY 的多个实例:\\测试

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

和所有剩下是:

C:\Test\
C:\Test\test\
C:\Test\test\test\

这篇关于如何使用CMD /批处理文件来删除一个目录中的名称x的所有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆