Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - unknown error when trying to write to text file

Reply
 
Old 10-14-2008   #1 (permalink)
Tony Logan


 
 

unknown error when trying to write to text file

I'm stumped. I wrote a script that runs locally on a single PC to examine
some registry values. If it finds a value that isn't set to "C:\", it writes
out the reg key, value, and data string to a text file, then changes the
registry value to "C:\".

If all the reg values are set to "C:\", nothing happens. Otherwise, the code
creates a folder and a text file and records any of the above-noted changes.

The code works fine if none of the reg values are C, and it also works for
the first non-C reg value it finds, writing the correct info to the text file.

My problem happens when more than one of the reg values are set to non-C.
When the code gets to the line instructing it to open the text file for
writing, I get the error "Permission denied: the remote server has been
paused or is in the process of being started." Which is a new one to me. The
PC I'm testing on isn't a server, just a normal desktop.

Here's the code. The line that throws the error is in the WriteToReg
subroutine; I'll note it with a comment.

Option Explicit

Dim objShell, objFSO, objTextFile, objComputer, objRegistry, objFolder
Dim strComputer, strComputerName, strKeyPath, strValueName, strValue
Dim bInfoToLog

' set constant for registry hive we want to examine
Const HKEY_LOCAL_MACHINE = &H80000002
' set constant for writing to text file we create
Const ForWriting = 8

' set variable for local PC
strComputer = "."

' connect to the WMI service and the StdRegProv class
Set objRegistry = GetObject("winmgmts:\\" & strComputer &
"\root\default:StdRegProv")

' define the reg keys and check values for anything not "C:\"

strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0FA989645BF32964F92B4D36DCF0D4E6"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0FA989645BF32964F92B4D36DCF0D4E6\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If


strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\4A4860479B387C34E8F64F4AFA7F4119"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\4A4860479B387C34E8F64F4AFA7F4119\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If

strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\1A7686356A1ADD44ABD5A2E599CDADD7"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\1A7686356A1ADD44ABD5A2E599CDADD7\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If

strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\28C3E705BBDE11D498D34073621939FA"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\28C3E705BBDE11D498D34073621939FA\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If

strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0890D7F67CBC50041961B45BDD582B46"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0890D7F67CBC50041961B45BDD582B46\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If

Public Sub WriteToReg (bInfoToLog)
If bInfoToLog = True Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
' check if folder exists. If not, create it.
If Not objFSO.FolderExists("c:\Windows\temp\RegChanges") Then
Set objFolder = objFSO.CreateFolder("c:\Windows\temp\RegChanges")
End If
' check if we've already created the text file. If not,
' create it so we can write the reg values to it
If Not objFSO.FileExists("c:\Windows\temp\RegChanges\regValues.txt") Then
Set objTextFile =
objFSO.CreateTextFile("c:\Windows\temp\RegChanges\regValues.txt")
Else 'otherwise the file exists, so just open it for writing
'NEXT LINE BELOW THROWS THE ERROR
Set objTextFile = objFSO.OpenTextFile
("c:\Windows\temp\RegChanges\regValues.txt", ForWriting)
End If
' write info to the file
objTextFile.WriteLine Now
objTextFile.WriteLine "Key: " & strKeypath
objTextFile.WriteLine "Value: " & strValueName
objTextFile.WriteLine "Data: " & strValue
objTextFile.WriteLine "= = = = = = = = = = = = = = = = = ="
objTextFile.WriteBlankLines(1)
End If
End Sub

WScript.Quit


My System SpecsSystem Spec
Old 10-14-2008   #2 (permalink)
Tony Logan


 
 

RE: unknown error when trying to write to text file

Hi, Salvador. Thanks for the suggestion. I tried it, but the code still
throws the same error as before, at this line:
objFSO.OpenTextFile("c:\Windows\temp\RegChanges\regValues.txt", 8)

"Salvador Manaois III" wrote:
Quote:

> Hi,
>
> You can try to modify the script at the part where the error is occurring
> with these lines:
>
> Set objOut =
> objFSO.CreateTextFile("c:\Windows\temp\RegChanges\regValues.txt",2)
> Else
> Set objTextFile =
> objFSO.OpenTextFile("c:\Windows\temp\RegChanges\regValues.txt", 8)
>
My System SpecsSystem Spec
Old 10-14-2008   #3 (permalink)
Tony Logan


 
 

RE: unknown error when trying to write to text file

Think I answered my own question. Adding this line to the subroutine, after I
write the info to the text file, resolved the issue:
objTextFile.Close

Apparently, leaving the text file open was bad, bad, bad, when the code
cycled through and tried to re-open it while it was already open.

My System SpecsSystem Spec
Old 10-14-2008   #4 (permalink)
James Whitlow


 
 

Re: unknown error when trying to write to text file

"Tony Logan" <TonyLogan@xxxxxx> wrote in message
news:925300D7-3B31-4610-A3FA-4B07D1F5608A@xxxxxx
Quote:

> Think I answered my own question. Adding this line to the subroutine,
> after I
> write the info to the text file, resolved the issue:
> objTextFile.Close
>
> Apparently, leaving the text file open was bad, bad, bad, when the code
> cycled through and tried to re-open it while it was already open.
You might also consider replacing this section:

If Not objFSO.FileExists("c:\Windows\temp\RegChanges\regValues.txt") Then
Set objTextFile =
objFSO.CreateTextFile("c:\Windows\temp\RegChanges\regValues.txt")
Else 'otherwise the file exists, so just open it for writing
Set objTextFile = objFSO.OpenTextFile
("c:\Windows\temp\RegChanges\regValues.txt", ForWriting)
End If

With:

Set objTextFile =
objFSO.OpenTextFile("c:\Windows\temp\RegChanges\regValues.txt", 8, True)

This would replace 5 lines of code with 1 and eliminate the 'If...Then'
test.



My System SpecsSystem Spec
Old 10-15-2008   #5 (permalink)
Al Dunbar


 
 

Re: unknown error when trying to write to text file


"Tony Logan" <TonyLogan@xxxxxx> wrote in message
news:925300D7-3B31-4610-A3FA-4B07D1F5608A@xxxxxx
Quote:

> Think I answered my own question. Adding this line to the subroutine,
> after I
> write the info to the text file, resolved the issue:
> objTextFile.Close
>
> Apparently, leaving the text file open was bad, bad, bad, when the code
> cycled through and tried to re-open it while it was already open.
I would have recommended that you open the file once at the beginning and
close it when the script has completed. constantly opening and closing an
output file has no benefit, and creates more opportunity for error (as you
have already noted).

Same for the creation of the objShell object: do it once at the start.

There are a few other ways you could simplify your script and make it easier
to follow:

1) use indentation to better highlight the block structure, i.e. change
this:

If Not IsNull(strKeyPath) Then ' ...
If strValue <> "C:\" Then
WriteToReg(True)
objShell.RegWrite "HKLM\SOFTWARE\Microsoft\... etc., "C:\", "REG_SZ"
End If
End If

to this:

If Not IsNull(strKeyPath) Then ' ...
If strValue <> "C:\" Then
WriteToReg(True)
objShell.RegWrite "HKLM\SOFTWARE\Microsoft\... etc., "C:\",
"REG_SZ"
End If
End If

2) avoid the use of in-line comments, as they can be more difficult to read
than separate comment lines or blocks - especially when one in-line comment
flows into the next full-line comment. Personally, I think your code is
over-commented, as comments should explain *why* something is done rather
than *what* is being done, unless this is not clear from the actual code.

3) replace successive chunks of virtually identical code with calls to a
subroutine. you have five chunks of code that look like this:

strKeyPath =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0FA989645BF32964F92B4D36DCF0D4E6"
strValueName = "40314C93E12538E4FA096CA6C4515B0F"
objRegistry.GetStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then ' if strKeyPath is not IsNull, then the reg
key exists
' Check value of strValue. If it's not "C:\", write some info to text file
we created above.
If strValue <> "C:\" Then
WriteToReg(True)
' now set the value to "C:\"
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0FA989645BF32964F92B4D36DCF0D4E6\40314C93E12538E4FA096CA6C4515B0F",
"C:\", "REG_SZ"
End If
End If

with the only differences seeming to be the last element in the strkeypath
variable and the kye of the value being written. These five chunks of code
could be replaced by these five lines:

checkKeys "0FA989645BF32964F92B4D36DCF0D4E6"
checkKeys "4A4860479B387C34E8F64F4AFA7F4119"
checkKeys "1A7686356A1ADD44ABD5A2E599CDADD7"
checkKeys "28C3E705BBDE11D498D34073621939FA"
checkKeys "0890D7F67CBC50041961B45BDD582B46"

and a sub such as the following (disclaimer: untested):

sub checkKeys( keytail )

const strValueName = "40314C93E12538E4FA096CA6C4515B0F"

strKeyPath = _
"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\"
_
& keytail

strKeyPath2 = _
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18"
_
& "\Components\" & keytail & "\" & strValueName

objRegistry.GetStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Not IsNull(strKeyPath) Then
If strValue <> "C:\" Then
WriteToReg(True)
objShell.RegWrite strKeyPath2 "C:\", "REG_SZ"
End If
End If

Yes, it is easier to write this way from the beginning than to try to
normalize the code after the fact. But then, think how much easier it would
be to fix an error in one line in the sub rather than fixing each instance
of that error in chunks of repeated code.


/Al


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how to get powershell to write to event log or write a log file? PowerShell
can't write to text file after installing app Vista General
Need help with log ( text ) file - write vs append VB Script
Error saving text file with no extension VB Script
How to read and write a text file? VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 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