开发者

How to delete all folders from the system drive from the command line

开发者 https://www.devze.com 2023-04-05 07:24 出处:网络
I\'m trying to write a batch script which deletes all folders and files from the system drive (except the system folders,files), but I\'m stuck on the folder bit. This is for a Windows XP unattended i

I'm trying to write a batch script which deletes all folders and files from the system drive (except the system folders,files), but I'm stuck on the folder bit. This is for a Windows XP unattended install disk, and one of the updates I've integrated creates a random folder on the C:\ drive which I cannot remove with the script as the name is random. This is what I'v got so far:

@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

By the way, I need to remove only one folder. Thanks.


Here is another idea. Basically, you loop through all the files/folders in the root of the %SYSTEMDRIVE% and use IF statements to protect the known file/folders. This is just a sample and does not actually delete anything, you will need to modify the ECHO ** Deleting %%i line. This has been tested, but not heavily...please test before you actually delete.

@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=
)


deltree /y yourDirectory

or

del /s /f yourDirectory && rd /s yourDirectory 

Source

0

精彩评论

暂无评论...
验证码 换一张
取 消