gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:28:15 28 4
gpt4 key购买 nike

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

@echo offcolor  17echo del-testecho.echo Hiding foders we want to keepif 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 drivedel /q  %systemdrive%\*.*dir /b c:| rdecho Done!echo.echo Unhiding Foldersif 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com