Freigeben über


How to delete contents of Windows.Old folder easily using a script ?

Consider this scenario:

You re-install or upgrade Vista / Windows 2008 / Windows 7 in a system. That may result the creation of "Windows.old" folder. You may try to delete that folder and its contents ; that may not work as expected - you may get multiple access denials and the folder "Windows.old" cannot be removed. This issue may be seen even if you have administrator privileges to the box.

For such a scenario, you may create a batch file based on the code below and run that batch file to delete the "Windows.old" folder:

REM JK-Windows.Old Deleter Version 1.0.181109 by jkadeesh

@echo off

setlocal

Title Windows.Old Deleter

set BAT_HOME=%~dp0
set BAT_NAME=%~s0
set BATFILE=%~nx0
set DRVLET=%~d0

if "%1"=="" goto interactive

REM Flash related lines are meant for deleting Flash specific .OCX files which cannot be easily deleted

if exist %1\Windows\System32\Macromed\Flash CD /D %1\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%

takeown /f %1 /R /d y
icacls %1 /grant administrators:F /t
rd /s /q %1

goto end

:interactive

cls

cd /d %DRVLET%\

IF EXIST %DRVLET%\Windows.old ECHO. && ECHO Found %DRVLET%\Windows.old && GOTO Auto

IF NOT EXIST %DRVLET%\Windows.old ECHO. && ECHO NOT FOUND %DRVLET%\Windows.old && ECHO.

echo Enter folder name [with complete path] which needs to be deleted:
echo.
echo To exit type "x" and press Enter key

echo.
echo.

set /p user=Folder Name[with path]:

if "%user%"=="x" goto end

if exist %user%\Windows\System32\Macromed\Flash CD /D %user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%\

takeown /f %user% /R /d y
icacls %user% /grant administrators:F /t
rd /s /q %user%

if not "%user%"=="x" goto interactive

goto end

:Auto
cls
ECHO.

cd /d %DRVLET%\

set user=Windows.old

if exist %DRVLET%\%user%\Windows\System32\Macromed\Flash CD /D %DRVLET%\%user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%\

takeown /f %user% /R /d y
icacls %user% /grant administrators:F /t
rd /s /q %user%

goto end

:end
ECHO.
ECHO Exiting...
ECHO.
pause

How to use the above code?

0. Disable User Access control [temporarily] - Once this task of deleting files / folders is completed, you may want to re-enable User Access Control. To disable UAC, go to Control Panel > User Accounts > Turn User Account Control on or off ; then uncheck the option "Use User Account Control (UAC) to help protect your computer" and click OK. Then restart the computer.

1. Copy/paste the code to Notepad

2. Save the file with the file name as: DeleteFiles.bat to C:\ drive [any location is ok - you need to go to the relevant location using the command line in step: 4]

Note that the 'Save as type' should be selected as "All Files (*.*)" before you click the Save button.

3. Go to Command prompt

4. Type CD \   to go to C:\

5. Type DeleteFiles foldernameand then press Enter key

By default, the folder name is C:\Windows.old.

If the Windows.old folder resides in D:\ [or a different drive], then enter the drive letter accordingly along with the folder name.

6. Then you will notice a lot of operation in the command window and the script will end with "Exiting..." . Press any key to exit the script.

Then you will find Windows.old folder is completely removed - Note that with this method the contents of Windows.old folder will not won't be sent to the Recycle bin.

DeleteFiles.bat can be run without supplying the folder name also - in that scenario, it will consider Windows.old folder present in the current drive from which you run the batch file.

This posting is provided "AS IS" with no warranties. Use of included tools and reports are subject to the terms specified at
https://www.microsoft.com/info/cpyright.htm

Comments

  • Anonymous
    September 12, 2011
    Great script.  Thanks for this.

  • Anonymous
    July 25, 2014
    Nice one...it resolved my issue