我有一个名为 x 的文件夹,其中包含许多子文件夹和文件。我想删除 x 中存在的名为 y 的文件夹及其所有子文件夹。必须删除的所述文件夹可能包含也可能不包含任何文件。我相信我可以使用 cmd 或某种批处理文件来完成此操作,但我是一个命令行新人,确实可以使用一些帮助。
一个简单的事情是读取文件夹的名称,这可以工作,但我相信有比单独删除每个文件夹更好的方法..就像遍历所有文件夹的循环一样。
Thanks
编辑:只是为了澄清一下,我在 x 中有 y (需要删除的文件夹),它可以位于 x 的任何子文件夹中并且处于任何深度级别。我也在寻找答案,我可能需要一些时间才能接受任何答案。请多多包涵 :)
这是另一个解决方案,用于描述脚本的每个部分:
@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
从初始变量前面取出,然后在后面输入它们的值=
如果您不想收到提示:
set foldername=
set root=
您还可以删除echo
在循环部分和pause
在最后部分让批处理静默运行。
它可能有点复杂,但代码可以应用于许多其他用途。
我对其进行了测试,查找同一文件夹名称的多个实例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\
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)