Jump to content

VBS Script Error


cmd

Recommended Posts

Set WshShell = WScript.CreateObject("WScript.Shell")
dir = "abc"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath", "a"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name", "b"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name", "c"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization", "d"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register", "e"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number", "f"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation", "g"

WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation"

That code gives an error, Line 10, Char 31, 'Expected end of statement'.

However, this works fine:

Set WshShell = WScript.CreateObject("WScript.Shell")
dir = "abc"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath", "a"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name", "b"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name", "c"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization", "d"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register", "e"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number", "f"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation", "g"

I can't see anything wrong with the first code. Can you? Note that I have changed what is being written to the registry (I don't exactly want to write a,b,c etc.)

Link to comment
Share on other sites


Add parens. That made the error go away for me.

WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation")

Link to comment
Share on other sites

How about adding Next to the empty line

Set WshShell = WScript.CreateObject("WScript.Shell")
dir = "abc"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath", "a"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name", "b"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name", "c"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization", "d"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register", "e"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number", "f"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation", "g"
Next
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number"
WScript.Echo WshShell.RegRead "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation"

It gave me an unexpected 'Next' :(

Set WshShell = WScript.CreateObject("WScript.Shell")
dir = "abc"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath", "a"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name", "b"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name", "c"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization", "d"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register", "e"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number", "f"
WshShell.RegWrite "HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation", "g"

WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Installation\InstallPath")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\First Name")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Last Name")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Organization")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Register")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Serial Number")
WScript.Echo WshShell.RegRead("HKLM\SOFTWARE\Macromedia\" & dir & "\6\Registration\Validation")

That works, although I don't know why. Maybe it's becuase I put a function inside a function so it wanted parentheses?

Thanks :)

Link to comment
Share on other sites

That works, although I don't know why. Maybe it's becuase I put a function inside a function so it wanted parentheses?

I'm not terribly sure myself, though I think that I remember reading somewhere a while back that, in VB.Net at least, if you are calling a function that you expect to collect a return value from, the parameters have to be in quotes. And since you're doing that here (and just echoing what is being returned) that would apply.

That's the best guess as to why that I can figure.

Edited by gosherm
Link to comment
Share on other sites

How about adding Next to the empty line

Yzöwl,

Can you explain why?

I do not see why Next needs to be used. Just an error in judgement maybe?

This is the only info of Next that I have from the Windows Script Helpfile:

For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next
_
Arguments
counter
Numeric variable used as a loop counter. The variable can't be an array element or an element of a user-defined type.
start
Initial value of counter.
end
Final value of counter.
step
Amount counter is changed each time through the loop. If not specified, step defaults to one.
statements
One or more statements between For and Next that are executed the specified number of times.
_

Remarks
Once the loop starts and all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.
_
Note Changing the value of counter while inside a loop can make it more difficult to read and debug your code.
Exit For can only be used within a For Each...Next or For...Next control structure to provide an alternate way to exit. Any number of Exit For statements may be placed anywhere in the loop. Exit For is often used with the evaluation of some condition (for example, If...Then), and transfers control to the statement immediately following Next.
_
You can nest For...Next loops by placing one For...Next loop within another. Give each loop a unique variable name as its counter. The following construction is correct:

For Each element In group
[statements]
[Exit For]
[statements]
Next [element]
_
Arguments
element
Variable used to iterate through the elements of the collection or array. For collections, element can only be a Variant variable, a generic Object variable, or any specific Automation object variable. For arrays, element can only be a Variant variable.
group
Name of an object collection or array.
statements
One or more statements that are executed on each item in group.
Remarks
The For Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. As long as there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.
_
The Exit For can only be used within a For Each...Next or For...Next control structure to provide an alternate way to exit. Any number of Exit For statements may be placed anywhere in the loop. The Exit For is often used with the evaluation of some condition (for example, If...Then), and transfers control to the statement immediately following Next.
_
You can nest For Each...Next loops by placing one For Each...Next loop within another. However, each loop element must be unique.
_
Note If you omit element in a Next statement, execution continues as if you had included it. If a Next statement is encountered before it's corresponding For statement, an error occurs.
The following example illustrates use of the For Each...Next statement:
_
Function ShowFolderList(folderspec)
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & "<BR>"
Next
ShowFolderList = s
End Function

On Error Resume Next
On Error GoTo 0
_
Remarks
If you don't use an On Error Resume Next statement anywhere in your code, any run-time error that occurs can cause an error message to be displayed and code execution stopped. However, the host running the code determines the exact behavior. The host can sometimes opt to handle such errors differently. In some cases, the script debugger may be invoked at the point of the error. In still other cases, there may be no apparent indication that any error occurred because the host does not to notify the user. Again, this is purely a function of how the host handles any errors that occur.
_
Within any particular procedure, an error is not necessarily fatal as long as error-handling is enabled somewhere along the call stack. If local error-handling is not enabled in a procedure and an error occurs, control is passed back through the call stack until a procedure with error-handling enabled is found and the error is handled at that point. If no procedure in the call stack is found to have error-handling enabled, an error message is displayed at that point and execution stops or the host handles the error as appropriate.
_
On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the procedure.
_
An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.
_
Use On Error GoTo 0 to disable error handling if you have previously enabled it using On Error Resume Next.
_
The following example illustrates use of the On Error Resume Next statement.
_
On Error Resume Next
Err.Raise 6 ' Raise an overflow error.
MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description
Err.Clear ' Clear the error.

Link to comment
Share on other sites

@ MHz,

Sorry, it was part of a rather large reply, which went awry, due to server or connection problems yesterday.

I just didn't get time to re-compose it, until after the questioner had already discovered the answer.

Link to comment
Share on other sites

How about going to the horse

http://msdn.microsoft.com/library/?url=/li...MthRegWrite.asp

Example

Dim WshShell, bKey

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY"

WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\MindReader", "Goocher!", "REG_SZ"

bKey = WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\")

WScript.Echo WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\MindReader")

WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\MindReader"

WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\"

WshShell.RegDelete "HKCU\Software\ACME\"

Link to comment
Share on other sites

@ MHz,

Sorry, it was part of a rather large reply, which went awry, due to server or connection problems yesterday.

I just didn't get time to re-compose it, until after the questioner had already discovered the answer.

Thanks for the reply Yzöwl,

No problems. Knew something wasn't right.

Cheers.

Link to comment
Share on other sites

Here is the script using the next statement

Dim Key, WshShell, WriteReg, ReadReg

Set WshShell = WScript.CreateObject("WScript.Shell")

Key = "HKLM\SOFTWARE\Macromedia\abc\6"

WshShell.RegWrite Key & "\Installation\InstallPath", "a"

WshShell.RegWrite Key & "\Registration\First Name", "b"

WshShell.RegWrite Key & "\Registration\Last Name", "c"

WshShell.RegWrite Key & "\Registration\Organization", "d"

WshShell.RegWrite Key & "\Registration\Register", "e"

WshShell.RegWrite Key & "\Registration\Serial Number", "f"

WshShell.RegWrite Key & "\Registration\Validation", "g"

ReadReg = Array((Key & "\Installation\InstallPath"),(Key & "\Registration\First Name"),_

(Key & "\Registration\Last Name"),(Key & "\Registration\Organization"),(Key & "\Registration\Register"),_

(Key & "\Registration\Serial Number"),(Key & "\Registration\Validation"))

For Each strReadReg In ReadReg

WScript.Echo strReadReg

Next

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...