2014-05-20 66 views
-1

所以我有一個腳本運行磁盤清理,但我想記錄下來。是否可以通過f.writeline或其他?我已經嘗試添加一些寫行但沒有工作,所以我刪除了這些..日誌磁盤清理VBScript

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 

謝謝!

回答

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 "" - whic h告訴它刪除前一個日誌文件。

希望這會有所幫助!

+0

嘿謝謝你的幫助!到目前爲止,我已將腳本添加到腳本中,但它不會工作,我將'logfilename'更改爲'C:\ Log.txt'。我究竟做錯了什麼? – user3611729

+0

任何人都可以幫助我解決這個問題嗎?親切的問候 – user3611729