为所有用户批量删除 Windows 临时文件(系统、浏览器、缓存等)

为所有用户批量删除 Windows 临时文件(系统、浏览器、缓存等)

这个社区中是否有人使用特定技术来实现所有用户的临时数据(例如 Windows 临时文件夹、浏览器缓存等)的批量删除?

棘手的部分似乎是:

  • 使用随时间更新的技术/工具来满足每个产品文件夹结构的变化
  • 允许访问其他用户的文件夹(以管理员/最大权限运行)

脚本/批处理文件可能是一种解决方案,但它需要不断监控每个产品更新,以避免删除旧文件/文件夹结构......

你的想法?

答案1

我也有同样的问题,但动机是希望帮助我完成恶意软件清理操作。这是我编写的命令脚本,着眼于模块化,以便它可以轻松扩展到未来的操作系统和临时文件位置(我在学习 PowerShell 之前编写了这个脚本,并且没有费心去更新它)。 因为它访问机器上的每个用户配置文件夹以及 Windows 系统文件夹,所以必须以提升的权限运行该脚本。

@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

扩展脚本的功能

脚本的可扩展性部分体现在其对:RemoveSubfoldersAndFiles过程的使用上。要删除文件夹的内容,只需调用此过程并传递文件夹路径作为唯一参数 (没有您可以使用任何文件或文件夹名称(双引号)来访问它们。该例程将妥善处理不存在的路径、由于任何原因无法访问的文件夹,或者路径下某些文件或文件夹正在使用或拒绝删除的情况。

清理每个用户个人资料中找到的其他文件夹

在部分中Rem Clean up these folders添加对:删除子文件夹和文件子程序。例如,要删除每个用户\AppData\Local\Microsoft\Windows\Temporary Internet Files文件夹中的所有内容,请添加以下行:

Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Microsoft\Windows\Temporary Internet Files

注意使用脚本定义的%UserProfilePath%变量而不是典型%USERPROFILE%变量。脚本的版本随着脚本遍历机器上的每个用户配置文件而动态更新。

清理用户配置文件之外的文件夹

:SetUserProfPath子程序中,再次添加对:删除子文件夹和文件过程。例如:

Call :RemoveSubfoldersAndFiles C:\Temp

相关内容