0
echo off
for /d /r "c:\" %%a in (TemporaryFolder) do if exist "%%a" echo Removing %%a & rmdir /s /q "%%a"

for loop将从 C:\ 驱动器中删除每个名为“TemporaryFolder”的目录。我将如何通过每个已安装的卷(即 A:\ - Z:) 来删除目录“TemporaryFolder”?

编辑(此测试无效):

@echo off
echo Deleting Temporary Folders... Please be patient.
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul & echo Deleting %%B
echo Successfully deleted.
pause
4

1 回答 1

0

根据我的评论,这是一个嵌套For循环的示例,应该按照要求执行:

For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul

外部For循环运行MountVol命令,该命令将安装的驱动器路径返回为%%A. 嵌套For循环对TemporaryFolder位于%%A.

编辑

这是同一例程的多行版本,其中添加了消息:

@Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do (
    Echo Deleting directories named %objFolder% from %%A
    For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%A%objFolder%" 2^>Nul') Do (
        Echo Deleting %%B
        RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
    )
)
Pause

您应该在线修改objFolder名称2


而不是MountVol,这个PowerShell用于获取驱动器,并匹配系统和重新分析点,(考虑到您需要适当的权限才能删除系统目录)

@Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In (
    'PowerShell -NoP "GDr -P FileSystem|?{!$_.Used -Eq ''}|Select -Exp Root"'
) Do (Echo Deleting directories named %objFolder% from %%A
    For /F "Delims=" %%B In ('Dir /B/S/AD "%%A%objFolder%" 2^>Nul') Do (
        Echo Deleting %%B
        RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
    )
)
Pause
于 2018-08-07T08:49:17.507 回答