我想删除所有以“machine.contoso.com”开头的文件夹(包括所有子文件夹和文件)。我尝试了这个命令:
但我收到错误 :(
cd c:\Program Files\Software\Temp
for /f %i in ('dir /a:d /s /b machine.contoso.com*') do rd /s /q %i
错误:
rd /s /q C:\Program
The system cannot find the file specified.
文件夹结构:
machine.contoso.com_1611940173
machine.contoso.com_1611323243
machine.contoso.com_1611645665
machine.contoso.com_1611940176
machine.contoso.com_1611940177
HOSTNAME.contoso.com_com_1811940177
HOSTNAME.contoso.com_com_1813435435
HOSTNAME.contoso.com_com_1811940456
HOSTNAME.contoso.com_com_1811345463
HOSTNAME.contoso.com_com_1813523454
谢谢,
答案1
您可以更换您的For /F
到For /R /D
cd /d "C:\Program Files\Software\Temp\" && for /r /d %i in (*contoso*)do rmdir /q /s "%~dpnxi"
您可以使用For /D /R
循环,它将遍历所有文件夹,并在循环内的每个文件夹中使用命令RMDir
删除循环中的当前文件夹及其所有文件/子文件夹。
FOR /R - Loop through files (recursively) FOR /D - Loop through several folders/directories
The option /D /R is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames) Note: Source linked to ss64.com
观察 1您可以使用machine.contoso.com*
,m*.contoso.com*
,m*.c*.com.*
,m*.c*.c*.*
等等...在你的循环中..(strings)do...
观察 2但请记住,您可能需要以管理员身份运行才能删除%ProgramFiles%
。
进一步阅读: