windows清理脚本

闲来无事,试着用python重写之前的cmd脚本,清理下系统

# 清理系统垃圾
import glob
import os
import winreg


def remove_files(rootdir, ext):
    '''
    清理rootdir目录下的符合的文件
    '''
    for i in glob.glob(os.path.join(rootdir, ext)):
        try:
            os.remove(i)
        except Exception as e:
            print(e)


def remove_reg_os(regs):
    '''
    清理系统注册表
    '''
    reg = winreg.CreateKey(
        winreg.HKEY_LOCAL_MACHINE,
        r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace'
    )
    try:
        for name in regs:
            winreg.DeleteKey(reg, name)
    except Exception as e:
        print(e)
    winreg.CloseKey(reg)


def remove_reg_user(regs):
    '''
    清理用户注册表
    '''
    reg = winreg.CreateKey(
        winreg.HKEY_CURRENT_USER,
        r'Software\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace'
    )
    try:
        for name in regs:
            winreg.DeleteKey(reg, name)
    except Exception as e:
        print(e)
    winreg.CloseKey(reg)


if __name__ == "__main__":
    #删除系统库
    regs = [
        r'{E701A357-F43B-42c9-98D1-96B6C11EAD87}',
        r'{f86fa3ab-70d2-4fc7-9c99-fcbf05467f3a}',
        r'{d3162b92-9365-467a-956b-92703aca08af}',
        r'{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}',
        r'{3dfdf296-dbec-4fb4-81d1-6a3438bcf4de}',
        r'{088e3905-0323-4b02-9826-5d99428e115f}',
        r'{24ad3ad4-a569-4530-98e1-ab02f9417aa8}'
    ]
    remove_reg_user(regs)

    #删除默认文件夹(库)
    systemdrive = os.getenv('systemdrive')
    #清除系统盘下临时文件
    remove_files(systemdrive, '*.tmp')
    #清除系统盘下文件
    remove_files(systemdrive, '*._mp')
    #清除系统盘下日志文件
    remove_files(systemdrive, '*.log')
    #清除系统盘下文件
    remove_files(systemdrive, '*.gid')
    #清除系统盘下文件
    remove_files(systemdrive, '*.chk')
    #清除系统盘下旧文件
    remove_files(systemdrive, '*.old')

    windir = os.getenv('windir')
    #清除系统目录下备份文件
    remove_files(windir, '*.bak')
    #清除系统目录下临时文件
    remove_files(windir, '*.tmp')
    #清除系统目录下临时文件夹
    remove_files(os.path.join(windir, 'temp'), '*.*')
    #清除系统目录下文件
    remove_files(os.path.join(windir, 'prefetch'), '*.*')

    systemroot = os.getenv('systemroot')
    #清除系统目录下dump文件夹
    remove_files(os.path.join(systemroot, 'minidump'), '*.*')
    #清除系统目录下文件
    remove_files(os.path.join(systemroot, 'prefetch'), '*.*')

    allusersprofile = os.getenv('allusersprofile')
    #清除软件数据
    # remove_files(os.path.join(allusersprofile, 'Documents'), '*.*')

    temp = os.getenv('temp')
    #清除用户temp目录下文件
    remove_files(temp, '*.*')

    userprofile = os.getenv('userprofile')
    #清除用户最近访问记录
    remove_files(os.path.join(userprofile, 'Recent'), '*.*')
    #清除用户最近office访问记录
    remove_files(
        os.path.join(userprofile,
                     'AppData\\Roaming\\Microsoft\\Office\\Recent'), '*.*')
    #清除用户本地temp
    remove_files(os.path.join(userprofile, 'AppData\\Local\\Temp'), '*.*')

    #清除图标缓存
    #关闭Windows外壳程序explorer
    os.system(r'taskkill /f /im explorer.exe')
    os.system(r'attrib -h -s -r "%userprofile%\AppData\Local\IconCache.db"')
    os.system(r'del /f "%userprofile%\AppData\Local\IconCache.db"')
    os.system(
        r'attrib /s /d -h -s -r "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\*"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_32.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_96.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_102.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_256.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_1024.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_idx.db"'
    )
    os.system(
        r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_sr.db"'
    )
    #清理 系统托盘记忆的图标
    os.system(
        r'echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams'
    )
    os.system(
        r'echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream'
    )
    #重启Windows外壳程序explorer
    os.system(r'start explorer.exe')
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值