如何从 bat 脚本变量中找到的父文件夹中删除子文件夹和文件?

How to delete subfolders and files from parent folders found in the variable of a bat script?

我想做的是在本地C盘找到所有名为"temp"的文件夹,然后删除其中的所有文件和子文件夹,但我不知道如何删除它,我只设法找到所有文件夹并将它们放入变量中,但我不知道要删除的正确代码。帮助。

@echo off
setlocal
setlocal enabledelayedexpansion
@echo off
for /d /r "c:\" %%i in (temp) do (
@if exist "%%i" (
@set _variable=%%i
@echo !_variable!
)
)
endlocal
@echo off && setlocal enabledelayedexpansion

cd /d "C:\" && for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do (
   echo\ Current Folder: "%%~i"
   echo\ RmDir /q /s "%%~i"
   echo\ MkDir "%%~i"
  )

%__APPDIR__%\Timeout.exe /t -1 & endlocal & goto :EOF

1) 前往驱动器:

cd /d "C:\"

2) 使用 For /fdir /s /b /a:d For /D /R

 for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do... 

3)不需要删除目标文件夹中的文件,只需删除此文件夹并重新创建...

RmDir /q /s "%%~i" && MkDir "%%~i\*"

  • 输出如下所示:
Current Folder: "C:\Program Files (x86)\Google\Temp"
rmdir /q /s "C:\Program Files (x86)\Google\Temp"
mkdir "C:\Program Files (x86)\Google\Temp\"
Current Folder: "C:\Program Files (x86)\Steam\steamapps\temp"
rmdir /q /s "C:\Program Files (x86)\Steam\steamapps\temp"
mkdir "C:\Program Files (x86)\Steam\steamapps\temp\"
Current Folder: "C:\ProgramData\Microsoft\Windows\WER\Temp"
rmdir /q /s "C:\ProgramData\Microsoft\Windows\WER\Temp"
mkdir "C:\ProgramData\Microsoft\Windows\WER\Temp\"
Current Folder: "C:\Users\All Users\Microsoft\Windows\WER\Temp"
rmdir /q /s "C:\Users\All Users\Microsoft\Windows\WER\Temp"
mkdir "C:\Users\All Users\Microsoft\Windows\WER\Temp\"
Current Folder: "C:\Users\Default\AppData\Local\Temp"
rmdir /q /s "C:\Users\Default\AppData\Local\Temp"
mkdir "C:\Users\Default\AppData\Local\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Temp"
mkdir "C:\Users\ecker\AppData\Local\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp"
mkdir "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp"
mkdir "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp\"
Current Folder: "C:\Windows\Temp"
rmdir /q /s "C:\Windows\Temp"
mkdir "C:\Windows\Temp\"
Current Folder: "C:\Windows\assembly\temp"
rmdir /q /s "C:\Windows\assembly\temp"
mkdir "C:\Windows\assembly\temp\"
mkdir "C:\Windows\assembly\NativeImages_v4.0.30319_64\Temp\"
Current Folder: "C:\Windows\System32\DriverStore\Temp"
rmdir /q /s "C:\Windows\System32\DriverStore\Temp"
mkdir "C:\Windows\System32\DriverStore\Temp\"
Current Folder: "C:\Windows\WinSxS\Temp"
rmdir /q /s "C:\Windows\WinSxS\Temp"
mkdir "C:\Windows\WinSxS\Temp\"

  • 在测试中验证输出并有效执行,删除 echo in:
@echo off && setlocal enabledelayedexpansion

cd /d "C:\" && for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do (
   echo\ 当前文件夹: "%%~i"
   <strike><strong>echo\</strike></strong> rmDir /q /s "%%~i"
   <strike><strong>echo\</strike></strong> MkDir "%%~i"
  )

%__APPDIR__%\Timeout.exe /t -1 & endlocal & goto :EOF

  • 或者使用这个简短的选项来抑制正在使用的任何文件夹的可能错误消息或一些其他类型的错误
@echo off 

setlocal enabledelayedexpansion && cd /d "C:\"
for /f "tokens=*" %%i in ('dir /s /b /a:d "temp"
')do 2>nul >nul (RmDir /q /s "%%~i" && MkDir "%%~i")
"%__APPDIR__%\Timout.exe" -1 & endlocal & goto :EOF
  • 当然,还有一个稍微小一点的选项...
@echo off 

cd/d "C:\" & setlocal enabledelayedexpansion
for /f tokens^=* %%i in ('dir/s/b/a:d "temp"
')do 2>nul (RmDir /q /s "%%~i" && MD "%%~i")
"%__APPDIR__%\Timeout.exe" /t -1 && endlocal

对于命令行帮助,您可以使用/?:

RmDir /?, For /?, MkDir /?, Endlocal /?, SetLocal /?

在互联网上,您可以获得更多帮助: