Batch delete temporary Windows files (system, browsers, cache, etc) for all users

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Is anyone in this community using a particular technique to achieve batch deletion of temporary data (such as Windows temp folders, browsers cache, etc) for all users?

The tricky part seems to be:

  • Use a technique/tool that will be updated over time to meet changes in each product folder structure
  • Allow access to other users folders (run as administrator/maximum privileges)

Scripts/batch files could be a solution, but it would require constant monitoring for each product update, to avoid deletion of older files/folder structures...

Your thoughts?

profilepic.png
manpreet 2 years ago

I had the same question, but motivated by the desire to aid my efforts to finish off malware cleanup operations. Here's the command script I wrote with an eye toward modularization so it could be extended easily for future OSes and temp file locations (I wrote this before learning PowerShell and haven't bothered to update it). Because it accesses every users profile folder on the machine as well as Windows system folders, the script must be run with elevated privileges.

@echo off
Rem Temp File Purging Tool v1.2.0
Rem Written by Twisty.  Created 1/19/2011.  Modified 6/28/2011.
Rem
Rem This script deletes temp files in locations where malware likes to write its initial
Rem files for infection and also where standard users have write permissions.
Rem
Rem This tool isn't likely to be as helpful to clean systems on which users run with 
Rem Admin permissions.  If you let your users run with Admin permissions you by extension
Rem give much of the malware on the Internet permission to do as it pleases on your workstations.



    Rem Identify version of Windows


    SET WinVer=Unknown

    VER | FINDSTR /IL "5.1." > NUL
    IF %ERRORLEVEL% EQU 0 SET WinVer=XP

    rem 5.2 is actually Server 2003, but for our purposes it's the same as XP
    VER | FINDSTR /IL "5.2." > NUL
    IF %ERRORLEVEL% EQU 0 SET WinVer=XP

    VER | FINDSTR /IL "6.0." > NUL
    IF %ERRORLEVEL% EQU 0 SET WinVer=VISTA

    rem 6.1 is actually Windows 7, but for our purposes it's the same as Vista
    VER | FINDSTR /IL "6.1." > NUL
    IF %ERRORLEVEL% EQU 0 SET WinVer=VISTA


    rem Ask user the version if we cannot automatically determine
    If Not "%WinVer%" EQU "Unknown" Goto :SetUserProfPath

    Set /P Response="Select OS  [X]P, [V]ista/7: "
    If /i "%Response%" EQU "X" Set WinVer=XP
    If /i "%Response%" EQU "V" Set WinVer=VISTA
    If "%WinVer%" EQU "" Echo Invalid response. Exiting.&goto :eof


:SetUserProfPath
    If %WinVer% EQU XP (
        Set UserProfileRootPath=C:\Documents and Settings
    ) Else (
        Set UserProfileRootPath=C:\Users
    )

    Call :RemoveSubfoldersAndFiles %SystemRoot%\Temp

    Rem Walk through each user profile folder
    Rem This convoluted command is necessary to ensure we process hidden and system folders too
    for /f "delims=" %%D in ('dir /ad /b "%UserProfileRootPath%"') DO Call :ProcessProfileFolder %UserProfileRootPath%\%%D

    Echo.
    Echo Finished! Press a key to exit...
    Pause>Nul

goto :EOF


:ProcessProfileFolder

    Set FolderName=%*

    Rem Leave if it's not a user profile folder
    If Not Exist "%FolderName%\ntuser.dat" goto :EOF

    Rem Leave it's a profile folder on the exclude list
    If /I "%FolderName%" EQU "%UserProfileRootPath%\Default" goto :EOF
    If /I "%FolderName%" EQU "%UserProfileRootPath%\Default User" goto :EOF
    If /I "%FolderName%" EQU "%UserProfileRootPath%\NetworkService" goto :EOF
    If /I "%FolderName%" EQU "%UserProfileRootPath%\LocalService" goto :EOF

    Set UserProfilePath=%FolderName%

    Rem Clean up these folders
    If %WinVer% EQU XP (
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\Local Settings\Temp
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\Local Settings\Temporary Internet Files
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\Application Data\Sun\Java\Deployment\cache

    ) Else (
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Temp
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\LocalLow\Temp
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\LocalLow\Sun\Java\Deployment\cache
        Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Microsoft\Windows\Temporary Internet Files
    )


goto :EOF


:RemoveSubfoldersAndFiles

    Set FolderRootPath=%*

    Rem Confirm target folder exists
    If Not Exist "%FolderRootPath%" Goto :EOF

    Rem Make the folder to clean current and confirm it exists...
    CD /D %FolderRootPath%

    Rem Confirm we switched directories
    If /I "%CD%" NEQ "%FolderRootPath%" Goto :EOF

    Rem ...so that this command cannot delete the folder, only everything in it
    Echo Purging %CD%
    RD /S /Q . >>nul 2>>&1

goto :EOF

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community