批次刪除所有使用者的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

相關內容