windows - 如何从命令行中删除系统驱动器中的所有文件夹

标签 windows batch-file cmd

我正在尝试编写一个批处理脚本,从系统驱动器中删除所有文件夹和文件(系统文件夹、文件除外),但我卡在了文件夹位上。这是针对 Windows XP 无人值守安装盘的,我集成的其中一个更新在 C:\驱动器上创建了一个随机文件夹,我无法使用脚本将其删除,因为名称是随机的。这是我到目前为止所得到的:

@echo off
color  17
echo del-test
echo.

echo Hiding foders we want to keep
if exist "%systemdrive%\AUTOEXEC.BAT"           attrib +h "%systemdrive%\AUTOEXEC.BAT"
if exist "%systemdrive%\CONFIG.SYS"             attrib +h "%systemdrive%\CONFIG.SYS"
if exist "%systemdrive%\Documents and Settings" attrib +h "%systemdrive%\Documents and Settings"
if exist "%systemdrive%\Program Files"          attrib +h "%systemdrive%\Program Files"
if exist "%systemdrive%\Sysprep"                attrib +h "%systemdrive%\Sysprep"
if exist "%systemdrive%\temp"                   attrib +h "%systemdrive%\temp"
if exist "%systemdrive%\WINDOWS"                attrib +h "%systemdrive%\WINDOWS"
echo Done!
echo.

echo Deleting everything that isn't hidden on the system drive
del /q  %systemdrive%\*.*
dir /b c:| rd
echo Done!
echo.

echo Unhiding Folders
if exist "%systemdrive%\AUTOEXEC.BAT"            attrib -h "%systemdrive%\AUTOEXEC.BAT"
if exist "%systemdrive%\CONFIG.SYS"              attrib -h "%systemdrive%\CONFIG.SYS"
if exist "%systemdrive%\Documents and Settings"  attrib -h "%systemdrive%\Documents and Settings"
if exist "%systemdrive%\Program Files"           attrib -h "%systemdrive%\Program Files"
if exist "%systemdrive%\Sysprep"                 attrib -h "%systemdrive%\Sysprep"
if exist "%systemdrive%\temp"                    attrib -h "%systemdrive%\temp"
if exist "%systemdrive%\WINDOWS"                 attrib -h "%systemdrive%\WINDOWS"
echo Done!
echo.
>nul pause

顺便说一句,我只需要删除一个文件夹。谢谢。

最佳答案

这是另一个想法。基本上,您遍历 %SYSTEMDRIVE% 根目录中的所有文件/文件夹,并使用 IF 语句来保护已知文件/文件夹。这只是一个示例,实际上并没有删除任何内容,您需要修改 ECHO ** Deleting %%i 行。这已经过测试,但不是很严格...请在实际删除之前进行测试。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "usebackq delims==" %%i IN (`DIR %SYSTEMDRIVE%\ /b`) DO (
  IF /I "%%i" EQU "Program Files" SET DO_NOT_DELETE=Y
  IF /I "%%i" EQU "Sysprep" SET DO_NOT_DELETE=Y
  IF /I "%%i" EQU "Temp" SET DO_NOT_DELETE=Y
  IF /I "%%i" EQU "Windows" SET DO_NOT_DELETE=Y
  IF /I "%%i" EQU "junkfile.txt" SET DO_NOT_DELETE=Y
  IF !DO_NOT_DELETE! EQU Y (
    ECHO NOT deleting %%i
  ) ELSE (
    ECHO ** Deleting %%i **
  )
  SET DO_NOT_DELETE=
)

关于windows - 如何从命令行中删除系统驱动器中的所有文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453882/

相关文章:

c++ - 使用 CMake 安装的 PDB 文件

css - 使用 -webkit-backface-visibility 时,Windows Chrome 中的单选按钮背景变为白色。任何解决方法?

c++ - Windows 编程新手,遇到代码块问题

windows - Makefile : 23: the recipe for the target “allmodes” failed

windows - 重定向在 cmd 中有效,但在 .bat 或 .cmd 文件中无效

node.js - Windows : Install node and then in same . ps1 脚本 npm 包出现错误 "npm"无法识别

security - 在批处理脚本中混淆密码

html - 如何使用批处理文件在 HTML 文件中写入 "greater than symbol"

windows - 使用 SC 安装 Windows 服务

windows - DOS 批处理遍历列表中的数字