我想删除具有特定名称的所有目录。例如。 “供应商”
d:\数据\ PROJECT1 \风向\厂商
d:\数据\ Project2的\ AnotherDir \ AnotherDir \厂商
d:\数据\项目3 \厂商
这就是我现在所拥有的。这会列出所有文件夹和子文件夹。
{{1}}
现在我希望在变量中放入一个路径,并检查目录是否等于名称(例如“vendor”)。但我找不到。
你能帮助我吗?
此致 德棉
答案 0 :(得分:1)
DIR命令有一个不幸的限制,它无法递归列出特定的文件夹名称。最简单的解决方案是列出所有文件夹,并使用FINDSTR过滤掉匹配的所有文件夹。
dir /ad /on /s /b | findstr /iec:"\vendor"
然后,您只需使用FOR / F迭代结果并添加RD命令
for /f "eol=: delims=" %%F in
'dir /ad /on /s /b ^| findstr /iec:"\vendor"'
) do rd /s /q "%%F" 2>nul
答案 1 :(得分:1)
如果您安装了RimRaf,这是我的工作解决方案:
@ECHO off
setlocal enableDelayedExpansion
:: Folders where the batch program doesn't need to search.
set skipTheseFolders=Decleir Pinokkio
:: All the folders that need to be deleted
set foldersToDelete=vendor node_modules
for /d /r %%d in (*.*) do (
@ECHO %%d
set folder=%%~nxd
set canI="true"
:: For loop to skip folders
for %%s in (%skipTheseFolders%) do (
:: Need to be implemented
set canI="true"
)
IF !canI! =="true" (
for %%l in (%foldersToDelete%) do (
:: If the folder is one of the folders To Delete
IF "!folder!" == "%%l" (
@echo %%d
cd %%d
cd ..
:: Using Rimraf because when you delete node_modules, normal delete won't work
start /B rimraf !folder!
)
)
)
)
答案 2 :(得分:0)
你可以试试这个:
@Echo OFF
set "Folder=D:\Data"
set "FolderString=vendor"
Setlocal Enabledelayedexpansion
FOR /f "tokens=*" %%F IN ('dir /b /s /ad %Folder%\ ^| find "%FolderString%"') DO (set var="%%F"
ECHO rd /s /q !var! && rd /s /q !var!)
EndLocal
pause