记录磁盘清理VBScript

时间:2014-05-20 13:11:24

标签: vbscript

所以我有一个运行磁盘清理的小脚本,但我想记录下来。有可能通过f.writeline或其他什么?我已经尝试添加一些写行但是没有用,所以我删除了这些......

Option Explicit 
On Error Resume Next 

SetRegKeys 
DoCleanup 


Sub DoCleanup() 
Dim WshShell 
Set WshShell=CreateObject("WScript.Shell") 
WshShell.Run "C:\WINDOWS\SYSTEM32\cleanmgr /sagerun:1" 
End Sub 

Sub SetRegKeys 
Dim strKeyPath 
Dim strComputer 
Dim objReg 
Dim arrSubKeys 
Dim SubKey 
Dim strValueName 
Dim fso
Const HKLM=&H80000002

strKeyPath="SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches" 
strComputer="." 
strValueName="StateFlags0001" 

Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") 
objReg.Enumkey HKLM ,strKeyPath,arrSubKeys 

For Each SubKey In arrSubKeys 

objReg.SetDWORDValue HKLM,strKeyPath & "\" & SubKey,strValueName,2 

Next 

End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

我有一个标准的子程序,我用它来记录输出文件(在脚本顶部指定的路径“logfileName”),它也在每一行上追加时间:

Sub Logwrite (aMsg)
   Dim a
   Const ForAppending = 8

   ' This Appends a line of text to a Logwrite file
   ' creating it if required
   If Len(aMsg) = 0 Then
      If fso.FileExists(logfileName) Then fso.DeleteFile(logfileName)
      Exit Sub
   End If

   Set a = fso.OpenTextFile(logfileName, ForAppending, TRUE, 0)
   a.WriteLine(Now() & " -- " & aMsg)
   a.Close
   Set a = Nothing

End Sub

例如,您可以在For Each循环中调用子例程,并且可以指定记录的消息:

For Each SubKey In arrSubKeys 

   objReg.SetDWORDValue HKLM,strKeyPath & "\" & SubKey,strValueName,2 
   Logwrite "Set value for " & strKeyPath & "\" & SubKey & " to " & strValueName

Next 

请注意,如果我想删除上一个文件(如果存在),那么我将首先调用脚本顶部的子例程,并使用空白消息进行记录 - Logwrite "" - 告诉它删除以前的日志文件。

希望这有帮助!