Results 1 to 2 of 2

Thread: Read a reg key, output to a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    5

    Read a reg key, output to a text file

    Looking to read a registry key, output it to a text file. I will then do some work to cleanup the reg key, read it again, and write the second set of results same txt file so we have a before and after results.
    I have some logic but I don't think its "graceful" enough and currently do not know how to append the after results to the same text file:

    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set ObjReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")

    arrRegPaths = Array( _
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform",_
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\UA Tokens"_
    )

    Const intForReading = 1
    Const intUnicode = -1

    strFileName = "C:\GT_Log\CleanUpUA.txt"

    Set objRegFile = objFSO.CreateTextFile(strFileName, True, True)
    objRegFile.WriteLine "IE User Agent Strings:"

    For Each strRegPath In arrRegPaths
    strCommand = "cmd /c REG EXPORT """ & strRegPath & """ """ & Replace(strRegPath, "\", "_") & ".txt"""
    objShell.Run strCommand, 0, True
    If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".txt") = True Then
    'WScript.Sleep 10000 ' Wait one second to give the file time to close
    Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".txt", intForReading, False, intUnicode)
    If Not objInputFile.AtEndOfStream Then
    objInputFile.SkipLine
    objRegFile.Write objInputFile.ReadAll
    End If
    objInputFile.Close
    Set objInputFile = Nothing
    objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".txt", True
    End If
    Next

    Delete some values....

    Read the key again....

    write the second set of results to the text file created earlier.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    5

    Re: Read a reg key, output to a text file

    Worked this out but, as with VBscript, Im sure there could be any number of ways: Here's what I came up with. The logic is to output the strings in this key to a text file, clean them up by targeting specific strings for deletion, and then append the txt file with the current key.

    on error resume next

    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    const HKEY_LOCAL_MACHINE = &H80000002
    strComputer = "."
    Set ObjReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")

    arrRegPaths = Array( _
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform",_
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\UA Tokens"_
    )

    Const intForReading = 1
    Const intUnicode = -1
    Const intForAppending = 8

    strFileName = "C:\GT_Log\CleanUpUA.txt"

    Set objRegFile = objFSO.CreateTextFile(strFileName)
    objregfile.close
    Set objRegFile = objFSO.opentextfile(strFileName,8)
    objRegFile.WriteLine "IE User Agent Strings Before Clean Up:"

    For Each strRegPath In arrRegPaths
    strCommand = "cmd /c REG EXPORT """ & strRegPath & """ """ & Replace(strRegPath, "\", "_") & ".txt"""
    objShell.Run strCommand, 0, True
    If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".txt") = True Then
    'WScript.Sleep 1000 ' Wait one second to give the temp file time to close
    Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".txt", intForReading, False, intUnicode)
    If Not objInputFile.AtEndOfStream Then
    objInputFile.SkipLine
    objRegFile.Write objInputFile.ReadAll
    End If
    objInputFile.Close
    Set objInputFile = Nothing
    objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".txt", True
    End If
    Next
    objregfile.close
    '****************************************************************

    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform"
    strStringValueName = ".NET CLR 3.0.04506.30"
    ObjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName
    strStringValueName = ".NET CLR 3.0.04506.648"
    ObjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName
    strStringValueName = ".NET CLR 3.5.21022"
    ObjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName

    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\UA Tokens"
    strStringValueName = "MSN 2.5"
    ObjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName
    strStringValueName = "MSN 2.0"
    ObjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName

    '******************************************************************

    strFileName = "C:\GT_Log\CleanUpUA.txt"

    Set objRegFile = objFSO.OpenTextFile(strFileName,8)
    objRegFile.WriteLine "IE User Agent Strings After Clean Up:"

    For Each strRegPath In arrRegPaths
    strCommand = "cmd /c REG EXPORT """ & strRegPath & """ """ & Replace(strRegPath, "\", "_") & ".txt"""
    objShell.Run strCommand, 0, True
    If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".txt") = True Then
    'WScript.Sleep 10000 ' Wait one second to give the file time to close
    Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".txt", intForReading, False, intUnicode)
    If Not objInputFile.AtEndOfStream Then
    objInputFile.SkipLine
    objRegFile.Write objInputFile.ReadAll
    End If
    objInputFile.Close
    Set objInputFile = Nothing
    objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".txt", True
    End If
    Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width