Batch Scripting Syntax Error - scripting

I'm trying to built a simple dungeon crawler game with batch script, but I keep coming across the error as I'm creating the menu screen. Could anyone please help to point out what I'm going wrong? FIXED cheers guys :)
#echo off
cls
echo LOADING GAME
ping localhost -n 2 >nul
#echo off
color 8a
title Josh's Game Beta
:loop
:menu
cls
echo You get to...
echo.
echo 1) Start
echo.
echo 2) Info
echo.
echo 3) Exit
set /p number =
if not defined number (
cls
goto loop
)
if %number% == 1 goto game
if %number% == 2 goto info
if %number% == 3 exit
:game
cls
echo Who are you?
set /p name =
echo Hello, %name%
echo Do you want to start the game(Y/N)
set /p start =
if %start% == Y goto lvl1
if %start% == y goto lvl1
if %start% == N goto menu
if %start% == n goto menu
if not defined start (
cls
goto menu
)

I haven't looked for other problems, but the following statement is creating a variable named "number " (with a space at the end):
set /p number =
The correct syntax would be
set /p "number="

Related

VBA statements for print only execute in debug mode

strong textI have a simple word form that I want to print. I pass the printer name to a sub routine (myprint) in the variable oprinter. The variable printcomplete will pass back the name of the printer that was successful in printing the data.
This logic works when I am in debug mode but appears to bypass the print commands when not in debug. I have tried adding delays, I have added a MSGBOX after the print statement (prior to
printcomplete = oprinter > Exit Sub). The MSGBOX does display - so it should have executed the print statement. I do not get the msgbox under myprinterr.
Any idea why it won't print out of debug mode?
Here is my code:
'Print MS Word Form to one of three networked printers
Sub MyPrint(oprinter, printcomplete)
Dim sPrinter As String
On Error GoTo myprinterr
Sleep (5000)
With Dialogs(wdDialogFilePrintSetup)
sPrinter = .Printer
.Printer = oprinter
.DoNotSetAsSysDefault = True
.Execute
Sleep (5000)
Application.PrintOut FileName:=""
.Printer = sPrinter
.Execute
End With
Sleep (5000)
MSGBOX "Did it print? "
printcomplete = oprinter
Exit Sub
myprinterr:
MsgBox "oops printer error on: " & oprinter
End Sub
In the code I've noticed several lines of code:
CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:05")) 'delay to try to get print to work when not in debug mode
There is no need to create a new Excel Application instance each time.
If you want to introduce any delay you can use the Sleep Windows API function instead. It suspends the execution of the current thread until the time-out interval elapses.

Handling Recurring VBA Errors Within Subroutine

I am stuck on something that I have a workaround for, but it bugs me I don't have a direct answer for how to address the issue of using On Error Goto for recurring errors. My question is essentially the same as this one, however the answers provided are alternatives to the OP's overall approach rather than how to handle the specific issue.
I've simplified an example below. I am WELL AWARE that there are probably a dozen ways this code could be rewritten to avoid any error -- I am just using this to illustrate the issue of trying to figure out why/how does this code work properly when i = 0, but not when i = 3,6,9?
Sub vbaErrorConfusion()
Dim theArray(9) As Long, i As Long
For i = 0 To 9
On Error GoTo fixingErrors
'next line will intentionally create an error when
'when i = {0} this works as expected, but at i=3 next line throws an error.
theArray(i) = i + 1 / (i Mod 3)
On Error GoTo 0
next_i:
Next i
Exit Sub
'----Error Handling----
'this works as expected when i=0 but not when i = 3,6,9
fixingErrors:
Err.Clear
On Error GoTo 0
'at this point I would expect my error state to be the same as the start of procedure?
theArray(i) = -1
GoTo next_i
End Sub
What my Err variable shows after first instance of 0.
After I run Err.Clear I would expect the behavior for i=3 to be the same as when i=0, however my procedure stops with the below VBA error one would expect WITHOUT any error catching.
I presume there's some way to reset the Error variable or handle this type of situation without a workaround? Any quality responses will get upvoted. Thanks.
To tell VBA that an you have dealt with the error you need a Resume statement. Therefore, replacing the line GoTo next_i with Resume next_i you give you the outcome you expect.
You do not need Err.Clear. Also, On Error GoTo 0 will disable the error handler. However, neither of these lines will tell VBA that you have dealt with an error.
In your code at i=0 the error handler is activated but VBA is not told that the error has been dealt with (i.e. no Resume statement). At i=3 another error occurs while a previous error hadn't been dealt with. In this case the current procedure/function/property cannot deal with the second error, which is therefore, fatal.
You should also take the On Error GoTo fixingErrors line outside the loop.
Sub vbaErrorConfusion()
On Error GoTo fixingErrors
Dim theArray(9) As Long, i As Long
For i = 0 To 9
'*On Error GoTo fixingErrors
'next line will intentionally create an error when
'when i = {0} this works as expected, but at i=3 next line throws an error.
theArray(i) = i + 1 / (i Mod 3)
'*On Error GoTo 0
next_i:
Next i
Exit Sub
'----Error Handling----
'this works as expected when i=0 but not when i = 3,6,9
fixingErrors:
'*Err.Clear
'*On Error GoTo 0
'at this point I would expect my error state to be the same as the start of procedure?
theArray(i) = -1
Resume next_i
End Sub
Please take some time to read up on how Error handling in VBA works.
Sub vbaErrorConfusion()
Dim theArray(9) As Long, i As Long
For i = 0 To 9
On Error GoTo fixingErrors
'next line will intentionally create an error when
'when i = {0} this works as expected, but at i=3 next line throws an error.
theArray(i) = i + 1 / (i Mod 3)
'On Error GoTo 0
'next_i:
Next i
Exit Sub
'----Error Handling----
'this works as expected when i=0 but not when i = 3,6,9
fixingErrors:
Err.Clear
Debug.Print "An error was generated for i= ", i
'On Error GoTo 0
'at this point I would expect my error state to be the same as the start of procedure?
theArray(i) = -1
Resume Next
End Sub

VBA SAP occasional error Control ID Not Found

In the below code I am extracting data from SAP. In a few SAP reports I am running, occasionally data is not available, in which a pop up box appears, displaying
No Data Exists For Chosen Selection
I've implemented the below error handling strategy, which works fine when debugging and stepping into the code, but running in totality I receive this error on the asterik denoted line:
The Control Could Not Be Found By ID
session.findById("wnd[0]/tbar[1]/btn[8]").press
On Error GoTo ResumeInterCompany
**If session.findById("wnd[1]/tbar[0]").Text = "No data exists for chosen selection" Then**
GoTo TroubleShootInterCompany
End If
Any suggestions. I can provide more relevant code if need be.
My workaround is as follows:
session.findById("wnd[0]/tbar[1]/btn[8]").press
On Error Resume next
myText = "x"
err.clear
myText = session.findById("wnd[1]/tbar[0]").Text
if err.number <> 0 then myText = ""
on error goto 0
If myText = "No data exists for chosen selection" Then
GoTo TroubleShootInterCompany
End If
Regards,
ScriptMan

Getting Unspecified error in QTP

I'm trying to execute the below script in QTP which opens F12 developer option on a webpage and checks the network data for 404 error by verifying the Network data weblist. But I get an "Unspecified error" while executing the line
If trim(Browser("creationtime:=0").WinListView("regexpwndtitle:=Summary View ListView Control").GetSubItem(rcount,4))="404" then
Can you please advise Why I get this error.
Code:
Browser("creationtime:=0").highlight
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{F12}"
wait 20
Browser("creationtime:=0").WinListView("regexpwndtitle:=Summary View ListView Control").Click
wait 2
WshShell.SendKeys "{F5}"
Browser("creationtime:=0").Refresh
wait 20
totalcount= Browser("creationtime:=0").WinListView("regexpwndtitle:=Summary View ListView Control").GetItemsCount
For rcount = 1 to totalcount-1
If trim(Browser("creationtime:=0").WinListView("regexpwndtitle:=Summary View ListView Control").GetSubItem(rcount,4))="404" then
flag=true
Exit for
End if
next
If flag Then
msgbox "404 is present in row "&rcount
else
msgbox "404 is not present"
End If

Different Number of Spaces Depending on the Variable

I am trying to create a sort of list that looks something like this:
echo thing1 thing2
echo.
Then a for loop to list text files in a directory and a bit of information like this:
echo %var% %var1%
echo %var% %var1%
rem and so on
But no matter what length the variable is, there will always be 11 spaces which puts it off a bit with the header.
I know I could make some code to detect the length and save a number of spaces in a variable and then add it on.
But I was just wondering if there is another more simple way.
There's no mention how exactly the strings are taken so I'll leave this to you.
Here's how you can set spaces between two variables and the result will have fixed length.
#echo off
rem http://ss64.org/viewtopic.php?id=424
set max_len=25
set "var11=thing1"
set "var12=thing2"
set "var21=thing1xxxxxx"
set "var22=thing2"
set "var31=thing1xxxxxx"
set "var32=thing2"
call :setSpaces var11 var12
call :setSpaces var21 var22
call :setSpaces var31 var32
goto :eof
:setSpaces var1 var2 [RtnVar]
setlocal EnableDelayedExpansion
set "var1=!%~1!"
set "var2=!%~2!"
call :strlen0 var1 len1
call :strlen0 var2 len2
set /a needed_spaces=%max_len%-%len1%-%len2%
rem echo %needed_spaces%
set "spaces="
for /l %%a in (1;1;%needed_spaces%) do (
set "spaces=!spaces! "
)
set line=%var1%%spaces%%var2%
endlocal&if "%~3" neq "" (set %~3=%line%) else echo %line%
exit /b 0
:strlen0 StrVar [RtnVar]
setlocal EnableDelayedExpansion
set "s=#!%~1!"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "len+=%%N"
set "s=!s:~%%N!"
)
)
endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b