清理winsxs的小工具
因为磁盘空间不够了,所以想起来清理一下系统垃圾文件,主要目标就是臭名昭著的winsxs目录。这个winsxs就是微软为了解决“dll hell”问题,结果是好比在windows系统里安置了一个毫无节制不断增大的“肿瘤”。听说微软研究院现在在研究这个问题,不过我想我的硬盘空间不够大,等不到这个补丁出来的时候,所以只好自己动手了。
winsxs目录下的文件都是系统要用的各种库文件,system32下存放了这些dll的最新的版本,所有老版本的dll都放在winsxs下。所以只要你安装程序或者更新补丁,system32下的文件就会被更新,而同时winsxs就会增加一些旧文件,所以我们的C盘空间就在持续不断地减少,直到磁盘容量不够,被迫重装系统为止,如果你足够幸运,可以直接安装最新的SP的话,或许可以为winsxs节约一点微薄的空间。
winsxs目录下的不同版本文件都存放在特定命名规则的目录下,比如
C:\Windows\winsxs>dir msil_microsoft.transactions.bridge.resources*
驱动器 C 中的卷是 vista
卷的序列号是 989F-EFF3C:\Windows\winsxs 的目录
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6000.16386_zh-cn_1cde5a17d78fb5ec
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6000.16716_zh-cn_1cd75781d79605cf
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6000.20876_zh-cn_060fb27df137fddf
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6001.18000_zh-cn_1cb2dbd3d7e75eb8
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6001.18106_zh-cn_1cb252ffd7e7f8cf
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6001.22221_zh-cn_05e71ebbf18d0b5e
msil_microsoft.transactions.bridge.resources_b03f5f7f11d50a3a_6.0.6002.18005_zh-cn_1c8e610fd838f2cc
0 个文件 0 字节
7 个目录 5,382,139,904 可用字节
这里的各个部分用下划线分割,其中我们关注的是“6.0.6000.16386”部分,它表示旧文件的版本号,之前则是唯一文件标识,之后是语言,最后部分是散列值(防止名字冲突)。
本工具的设计思想就是删除所有的旧文件。所有满足如下全部条件的目录都会被移动到C:\Windows\winsxs_del目录中。
- 存在比自身更新的版本
- 本身不是最新版本
运行工具前的C盘剩余空间:
所列文件总数:
4473 个文件 3,336,376,627 字节
7655 个目录 326,840,320 可用字节C:\Windows\winsxs_del>
运行工具并且执行命令
for /d %v in (%SystemRoot%\winsxs_del\*.*) do rd /s /q %v
删除所有可以删除的无用文件之后的剩余空间:
所列文件总数:
52 个文件 7,555,048 字节
131 个目录 5,383,979,008 可用字节C:\Windows\winsxs_del>
工具源代码如下:请保存为winsxs_clear.bat即可。所有不再需要的文件会移动到c:\windows\winsxs_del目录中,可以直接进行删除。
执行时候,务必请使用“管理员”权限。

2

3

4

5

6

7

8

9

10

11

12


13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52


53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

代码导读有助于大家理解程序和算法,但是基本的批处理语法就不讲了,有几年编程经验的我想也看得懂。以下是大致几个要注意的地方:
- 代码的开头部分是用ver命令获取系统的版本号,并且存放到%winver%变量中,比如我的ver命令返回就是“Microsoft Windows [版本 6.0.6002]”,为了获取这个“6.0.6002”,所以要做一些处理,另外,%ver_prefix%中存放的是类似“6.0.600”,为了比较旧版本号用途。
- 因为批处理无法实现嵌套嵌入功能,比如我想把从目录中分解出来的6.0.6000.16386和%ver_prefix%进行比较,就无法实现了,只好用代码生成大法来处理,在18~24行就是生成代码,该代码在63行和69行调用。26行负责把控制转移到新生成的文件中执行。
- 因为winsxs目录是有特殊权限的,所以先用takeown命令设置当前用户为拥有者,然后用cacls修改目录权限,最后用move指令将目录转移到winsxs_del目录中。如果出现程序无法运行的情况,请手工移动回去即可。

公众号:老翅寒暑
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤