Can anyone see what is wrong with my code? - vbscript

My code is as follows:
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
I know it is basic, but it comes up with the error message: "Error: Expected 'End'"
But as you can see 'end' is at the start of line 17, if it is something I haven't seen some where else in the code that might be causing this. I'm kind of new to this language and was putting things I knew how to do into a semi-useful paten.
I saved it as a .bat file and ran it using cmd.

Here is the original code posted in the question (as I'm writing this):
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
A main error cause seems to be a notion that one can assign a definition to a variable. Well one can, but not in that way. Here is corrected code:
option explicit
dim answer
do
answer = msgbox("Do you like waffles",4+16,"Waffles")
if answer = vbYes then exit do
answer = msgbox("Do you like pancakes",4+16,"Pancakes")
if answer = vbYes then exit do
answer = msgbox("Do you like French toast",4+16,"French toast")
if answer = vbYes then exit do
loop
call msgbox( "good",0+16,"YAY!" )
There are however umpteen zillion ways to prepare food, and so also with code, plus, it's not clear if the above is the intention, but I guess it's pretty close.
At any rate, it's code that works and that you can build further on
Note that VBScript documentation is now only available as a CHM compiled help file, called "script56.chm".
The VBScript documentation is both available as a handy CHM compiled help file, called "script56.chm", as well as online in the MSDN library.
Note: you need to save the source code as a .vbs file.

Related

Stopping an executable running if vbs loop if outside criteria is met

I'm using a VBScript to write into a configuration file config.txt, then run an executable SomeName.exe with the configuration I set.
The excecutable is not mine, I cannot interact with it.
The result is written in another text file Result.txt.
In the end, it looks like this
Set objShell = CreateObject("WScript.Shell")
For MyParameter = mystart to myend
'here I overwrite conf.txt with MyParameter value
Rt = objShell.run(SomeName.exe, 1, True) '--> True means "wait until the end before processing"
Next
What I'd like to do is to check the result.txt file and, if it's ok, stop the .exe and resume the loop with next value of MyParameter.
I already know how to read the file and decide whether the result is good enough or not (basically I read the last line and compare it with something else, very easy stuff) with a second script.
What I don't know is how to make this two scripts work together.
For now the only way I have is to run manually my second script and make it check from time to time (with Sleep function) if the result is good. In that case, I use taskill /im "SomeName.exe". But it's quite ugly and I have to run it with an infinite loop since I don't know how long it will take to SomeName.exe to reach the result (it's a simulation, it can be very very long !).
Have you got any idea on how to do that ?
Thanks a lot in advance for any help you can give me,
Why use WaitOnReturn=True?
Change that to False and then add your script code loop that checks for final line right after it.

What is wrong with this piece of code(trivial)

This is a very trivial function I wrote to read text as a prompt. It doesn't work but doesn't give me errors either. I've tried all modifications I can think if to get it to work, but to no avail.
Code:
def prompt(query="")
print (query) #I also tried 'print"#{query}"
var = gets()
return var
end
name = prompt("Input your name:")
puts"#{name}"
Nothing happens. This has frustrated me, that I couldn't do something so simple, and I've spent hour on it.
There was no problem with the code itself. It was simply that my editor Sublime Text 3 didn't handle accepting user input well.
How to make scripts run from command line:
Create a file ending in ".rb", for example "my_script.rb"
Run ruby my_script.rb
Don't forget ".rb" ;)

Making a Console Application ask something and receive input

I have Programming in Basic in High School and was tasked with a project in which I was to make a program that says hello to the teacher as well as give a paragraph describing why programming is valuable in today's time.
My initial idea was to make a program that greets and asks the teacher a question on whether they want to know why programming is valuable. When the console asks the question there will be two options: Yes or No. Each having their own text output.
REM Period 6 Class Project for (My Name)
Dim input = Console.ReadLine
Dim Yes = input
Dim No = input
Console.WriteLine("Hello, would you like to get an explanation as to why programming is valuable?")
input = Console.ReadLine
If input = Yes Then
Console.WriteLine("Well the reason why...")
End If
Where I'm having trouble is actually making the text appear on the application as well as receiving input on whether the code is correct as well.
See if .. then .. else
input=input.ToLower().Trim()
If input="yes" then
...
ElseIf input="no" then
...
Else
..' unexpected answer
End if

Cannot convert Group Description to String Variable

I have the following code:
Set objHoldGroup = GetObject("LDAP://" & objGroup)
strGroupDesc = (objHoldGroup.Description)
WScript.Echo(strGroupDesc)
The variable strGroupDesc returns nothing when echoed. I can output the description directly but I need it for further processing. Thoughts?
Explanation: Apparently your script sets Option Explicit (good), which you didn't tell us about (bad). This option makes defining variables before you can use them mandatory (good). Normally that would raise an "undefined variable" error, though. Since that doesn't seem to happen with your code, you seem to also have an On Error Resume Next somewhere in your code (very bad), which, again, you chose to keep quiet about (bad).
Next time please don't omit parts of your code that are vital for troubleshooting the problem. And don't use On Error Resume Next.
Issue found: I forgot to Dim strGroupDesc.

Is it possible to retrieve the call stack programmatically in VB6?

When an error occurs in a function, I'd like to know the sequence of events that lead up to it, especially when that function is called from a dozen different places. Is there any way to retrieve the call stack in VB6, or do I have to do it the hard way (e.g., log entries in every function and error handler, etc.)?
You do have to do it the hard way, but it's not really all that hard... Seriously, once you've written the template once, it's a quick copy/paste/modify to match the function name in the Err.Raise statement to the actual function name.
Private Function DoSomething(ByVal Arg as String)
On Error GoTo Handler
Dim ThisVar as String
Dim ThatVar as Long
' Code here to implement DoSomething...
Exit Function
Handler:
Err.Raise Err.Number, , "MiscFunctions.DoSomething: " & Err.Description
End Function
When you have nested calls, this unwinds as each routine hits its Handler and adds its name to the error description. At the top level function, you get a "call stack" showing the list of routines that were called, and the error number and description of the error that actually occurred. It's not perfect, in that you don't get line numbers, but I've found that you don't usually need them to find your way to the problem. (And if you really want line numbers, you can put them in the function and reference them in the Err.Raise statement using the Erl variable. Without line numbers, that just returns 0.)
Also, note that within the function itself, you can raise your own errors with the values of interesting variables in the message like so:
Err.Raise PCLOADLETTER_ERRNUM, , "PC Load Letter error on Printer """ & PrinterName & """"
(The syntax highlighting looks wonky in the preview... I wonder how will it look when posted?)
I'm pretty sure you have to do it the hard way. At a previous job of mine, we had a very elegant error handling process for VB6 with DCOM components. However, it was a lot redundant code that had to be added to every method, so much that we had home-grown tools to insert it all for you.
I can't provide too much insight on its implementation (both because I've forgotten most of it and there's a chance they may consider it a trade secret). One thing that does stand out was that the method name couldn't be derived at run-time so it was added as a string variable (some developers would copy-paste instead of using the tool and it would lead to error stacks that lied...).
HTH
The hard, manual way is pretty much the only way. If you check out this question, someone suggested a tool called MZTools that will do much of the grunt work for you.
As other people said (years ago, I see... but there's so many people still using VB6! :) ), I think it's not possible to programmatically retrieve the Call Stack, unless you use some 3rd-party tool.
But if you need to do that for debugging purposes, you can consider of adding to the called routine an Optional input string variable, were you'll put the caller's name.
Sub MyRoutine
(...) ' Your code here
call DoSomething (Var1, Var2, Var3, "MyRoutine")
' ^
' Present routine's name -----------+
(...) ' Your code here
End Sub
Public DoSomething (DoVar1, DoVar2, DoVar3, Optional Caller as string = "[unknown]")
Debug.Print " DoSomething Routine Called. Caller = " & Caller
... ' (your code here)
End Sub
Not so elegant, maybe, but it worked for me.
Regards,
Max - Italy
Compuware (or was it Numega at the time) DevStudio for Visual Basic 6 used to do this. The way was by adding adding instrumenation to every call that called a very small snippet that added to the code stack. On any error it dumped out that callstack, and then did things like mail or post to a webserver all the debuging information. Adding and removing the instrumentation was a potentially lethal operation (especially back then, when we were using VSS as our source control), but if it worked, it work well.
As Darrel pointed out, you could add something very simlar by using MZTools and setting up a template. It's a lot of working, and is probably more effeort than the reward would be but if you have very difficult to track down bugs, it might help).

Resources