"goto was unexpected at this time" .bat - batch-file

I'm making a blackjack gambling simuator using .bat files. At one point i get the error "goto was unexpected at this time". Anyone know a solution to this problem? I have included the section of code that is causing the problem.
Echo if %%DealerHand%% GTR 21 set DealerStatus=Bust
Echo if %%DealerHand%%==21 set DealerStatus=Blackjack!
Echo if %%DealerHand%% LSS 21 set DealerStatus=%%DealerHand%%
Echo if %%handP4%% GTR 21 set P4status=Bust
Echo if %%handP4%%==21 set P4status=Blackjack!
Echo if %%handP4%% LSS 21 set P4status=%%handP4%%
Echo cls
Echo Echo Table Number:1
Echo Echo Dealer:David
Echo Echo Money:$%%money%%
Echo Echo Money on table:$%%bet%%
Echo Echo Players:
Echo Echo.
Echo Echo Dealer:Hand:%%DealerStatus%%
Echo Echo.
Echo Echo %acctname%:Hand:%%P4status%%
Echo Echo.
Echo Echo Enter to continue
Echo Pause^>nul
Echo if %%DealerStatus%% == Bust goto winCheck1
Echo if %%DealerStatus%% == Blackjack! goto DealerWins1
Echo if %%DealerStatus%% == %%DealerHand%% goto winCheck1
Echo :winCheck1
Echo if %%P4hand%% == Bust goto DealerWins1
Echo if %%P4hand%% == Blackjack! goto P4wins1
Echo if %%P4hand%% GTR %%DealerHand%% goto P4win1
Echo if %%P4hand%% LSS %%DealerHand%% goto DealerWins1
Echo if %%P4hand%% == %%DealerHand%% goto DealerWins1
*P.S. I'm using this .bat file to generate a new .bat file contianing code. Hence the Echo before every line.
Any help is good help!
Thanks.

Your test for the string "Blackjack!" is going to fail due to the exclamation mark. Try wrapping the test with quotes:
Echo if "%%DealerStatus%%" == "Blackjack!" goto DealerWins1
and
Echo if "%%P4hand%%" == "Blackjack!" goto P4wins1
I suspect however, that there are a few other issues in your code that may be tripping you up. The %P4hand% variable is a number earlier in your code snippet. Are you intending to test %P4status% instead? You may also want to check your use of tag names for consistency as there's P4wins1 as well as P4win1 in use.

The first advice I'll give you is to put ECHO ON at the top of your file (or just remove ECHO OFF if it's there), run it again, and figure out which line has the error. Batch wants to tell you what's wrong, but everybody always suppresses echo even while debugging.
That said, even without knowing which line has the error, my psychic debugging skills tells me that somehow %DealerHand% is the empty string, which causes a syntax error in one of these two lines:
if %%DealerStatus%% == %%DealerHand%% goto winCheck1
if %%P4hand%% == %%DealerHand%% goto DealerWins1
Running that if statement with %DealerHand% undefined or the empty string will give you the error you see. You don't show where DealerHand is set, but if it should not be empty, you'll need to make sure it has a value when you enter this code.
If it is possible that DealerHand has an empty value, then you can guard against the syntax error by wrapping the variable in some characters, so that both sides of the IF always have non-empty strings.
if {%%DealerStatus%%} == {%%DealerHand%%} goto winCheck1
Which evaluates to if {} == {} goto winCheck1 when both variables are null.

Related

How to echo string and user input to a file

I'm trying to echo a variable into an autoexec.cfg file but I'm having some trouble with echoing a string and a variable to a file simultaneously.
I've tried putting quotes around the echo statement like
echo "sv_maxplayers %answer%">%location%
and, while it echos the string correctly, it includes the quotations marks.
("sv_maxplayers 4")
My script is currently
#ECHO OFF
set location=".\Risk of Rain 2_Data\Config\autoexec.cfg"
set /p answer="Please enter a number 1-16:"
if %answer% GTR 0 (
if %answer% LSS 17 (
goto set
)
)
echo Invalid argument.
goto exit
:set
echo sv_maxplayers %answer%>%location%
echo Done!
goto exit
:exit
pause
exit
Running that returns
Please enter a number 1-16:4
sv_maxplayers
Done!
Press any key to continue . . .
in the console and clears autoexec.cfg.
It should output as
sv_maxplayers 4
Any idea what I'm doing wrong?
Found the solution! I was missing some spaces.
echo sv_maxplayers %answer%>%location%
needs to be
echo sv_maxplayers %answer% > %location%

Im Making A Batch File Create Another File But i Cant add a parentheses

I try to make a batch file make another batch file with parentheses like this
set brackleft ="("
set brackright =")"
#(
echo #echo off
echo if exist exp.txt %brackleft%
echo goto a
echo %brackright% else %brackleft%
echo echo Bye
echo pause
echo %brackleft%
echo pause
echo :a
echo echo Hi
echo pause
) > Test.bat
But when I right click and edit the new batch file this comes up
#echo off
if exist exp.txt
goto a
else
echo Bye
pause
ECHO is off.
pause
:a
echo Hi
pause
Note:
rem ↓ this space is harmful creates %brackleft % variable instead of %brackleft%
set brackleft ="("
set brackright =")"
rem ↑ this space is harmful as well: creates %brackright % variable
rem ↑
Let's remove that harmful spaces:
set brackleft="("
set brackright=")"
Then, echo %brackright% else %brackleft% would result to ")" else "(".
You need next syntax with escaped both left and right parentheses as ^( and ^); Moreover, ^ caret should be escaped as well (see position of double quotes):
set "brackleft=^("
set "brackright=^)"
I can't add a parentheses
set brackleft ="("
set brackright =")"
You have created a variable with the name "brackleft " and "brackright " (note the trailing spaces).
Any extra spaces around either the variable name or the string, will not be ignored, SET is not forgiving of extra spaces like many other scripting languages.
Source set
Notes:
Remove the spaces before the =.
You also need to remove the "s (otherwise they will be echoed)
You also need to escape the ( and ) in order for the echo to work later.
^^ is used to make a single ^ escape character.
You should use ^^^ to escape the ( or ) inside an existing code block.
Your logic seems to have a mistake. echo %brackleft% should probably be echo %brackright%, otherwise your parentheses don't match up.
So the code becomes:
set brackleft=^^^(
set brackright=^^^)
Corrected batch file:
set brackleft=^^^(
set brackright=^^^)
#(
echo #echo off
echo if exist exp.txt %brackleft%
echo goto a
echo %brackright% else %brackleft%
echo echo Bye
echo pause
echo %brackright%
echo pause
echo :a
echo echo Hi
echo pause
) > Test.bat
Output:
F:\test>type Test.bat
#echo off
if exist exp.txt (
goto a
) else (
echo Bye
pause
)
pause
:a
echo Hi
pause
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
syntax - Escape Characters, Delimiters and Quotes.

Batch program not working

I am new to batch coding and I am having an issue where whenever I select an option, it goes to 1 (which is :MEMBERS) even if I enter 2 or 3. I am unsure why this is happening.
#echo off
cls
color A
:MENU
cls
echo ===============================
echo Mavast Client v0.01
echo ===============================
echo Enter a number from the list and hit enter
echo to run that program.
echo 1. Members
echo 2. Support us
echo 3. Exit
set /p var= Run:
if %var%=="1" goto MEMBERS
if %var%=="2" goto SUPPORT
if %var%=="3" goto EXIT
:MEMBERS
cls
echo Owner/Rifler = C0MpL3T3
echo Admin/Stratcaller = TDGR (The Dutch Grim Reaper)
echo Awper = NONE
echo Lurker = NONE
echo Entry Fragger = NONE
echo press any key to continue
timeout -1 > NUL
goto MENU
:SUPPORT
cls
echo Support us by donating BitCoins or skins
echo to donate skins, just trade them to TDGR or C0MpL3T3.
echo to donate bitcoins, send any amount you want to this address:
echo 1DSRHe7L842MZjwTWYPzgGzbsZ8zU97w9g
echo thank you!
echo press any key to continue
timeout -1 > NUL
goto MENU
:EXIT
cls
exit
try with
if "%var%"=="1" goto MEMBERS
if "%var%"=="2" goto SUPPORT
if "%var%"=="3" goto EXIT
Quotes are also compared in IF conditions.

Set /p %foo% doesn't function as expected

I have a problem with my code. I am trying to make a "hacker tool" with the tree command. Here is my code:
#echo off
title $userOne ProxyMatrix
color a
echo Hello $userOne,
echo Please enter search function for today's commands:
set /p %commands%=""
:redo
echo Specify Storage Device
set /p %drive%=""
title $userOne ProxyMatrix: Running on %drive% drive at %random% bits per nano
color C
tree %drive% /f
:runagain
color a
echo Run again?
set /p %redo%=""
if %redo%="yes" goto redo
else if %redo%="y" goto redo
else if %redo%="Y" goto redo
else if %redo%="Yes" goto redo
else if %redo%="no" goto end
else if %redo%="No" goto end
else if %redo%="n" goto end
else if %redo%="N" goto end
else echo Thats not a valid answer!
pause
goto runagain
:end
echo Thank you for choosing InGen, inc.
pause
I realize that this won't "hack" anything, its more of a novelty. The problem is, the set /p %redo% and the if/else if statements don't work. They just quit the program. Can someone explain what i'm doing wrong? Thanks.
Syntax is set /p variable=prompt.
Instead of set /p %redo%="" write set /p redo="" or even better set /p "redo="
EDIT
your if syntax is broken too.
Syntax is: if value1==value2 command or if value1==value2 (command1) else (command2)
"Best Practice is to enclose both sides of the comparison with quotes (to avoid syntax errors with empty values or contained spaces):
if "%variable"=="value" echo yes
I would shorten the code to:
set /p %redo%=""
if /i "%redo:~0,1%"=="y" goto redo
if /i "%redo:~0,1%"=="n" goto end
else echo Thats not a valid answer!
/i tells if to ignore capitalization
%variable:~0,1% means "take a substring starting with the first letter (counting starts at 0) with length=1 (so it takes the first letter)
(there is no else needed)

Redirecting CMD Commands To An EXE File

Basically I have created an choice batch that every so often I can archive the contents of my boot data since I change it quite often and it works absolutely perfect, however I face the problem that every time I compile the batch from .BAT to.EXE with Advanced BAT to EXE converter the command 'bcdedit' never works and says “Not recognised as an internal or external command, operable program or batch file.”
Now the first thing I did was to make sure if I had the environment variables directed on my hard drive and it seemed fine:
Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Users\Support\DOCUME~1\MYFILE~1\Programs\SYSTEM~1\DISKEE~1\
It has all the variables needed to work perfectly plus a few external ones which is ok then I thought that maybe I should try to make it direct on my folder, by doing some research I found on another forum:
Setting or Modifying a System Wide Environment Variable In CMD.EXE
I made a separate .BAT and I called it and it seems it did create the separate variable:
C:\Users\???\Documents\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Users\Support\DOCUME~1\MYFILE~1\Programs\SYSTEM~1\DISKEE~1" /f
But It Still Didn't Work!
I read upon another form:
Syswow64 Redirection
The person solution was to use Create Process () to create a program that automatically redirects him to the folder using sysnative (System32) but I'm a total beginner at C#, CMD is my strongest area when it comes to coding.
Please please I beg of you to help me as soon as possible and if you do have an answer please state in the easiest way possible and why. Here's my script just in case:
:START
echo.
echo Call apath.bat
echo.
echo.
echo (E) - Start Process.
echo (C) - Launch Part 2 Of Process.
echo (N) - Load NoDrives Manager.
echo (D) - Display Currect Account and Computer Information
echo (X) - Exit EDIM.
echo.
echo NOTE - It will not work if not started with Admin Privillages.
echo.
:Choice
set/p Option=Please enter your option:
if '%Option%' == 'E' goto :Incognito
if '%Option%' == 'C' goto :Touch
if '%Option%' == 'N' goto :Manager
if '%Option%' == 'D' goto :Data
if '%Option%' == 'X' goto :Exit
echo.
echo.
echo Invalid Option - Please Reselect.
goto :Choice
:RetryE
Echo An Error was found!
set/p RetryE=Retry? Y/N:
if '%RetryE%' == 'Y' goto :Incognito
if '%RetryE%' == 'N' goto :Exit
:Incognito
Timeout 5
echo.
echo.
echo.
Echo Saving OriginalBCD...
bcdedit /export "Data/BCD/OriginalBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Checking presence of BCD...
IF NOT EXIST "Data/BCD/OriginalBCD" goto :RetryE
Echo Deleting Boot Entry...
bcdedit /delete {current}
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Saving EditedBCD...
bcdedit /export "Data/BCD/IncogBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Checking presence of BCD...
IF NOT EXIST "Data/BCD/IncogBCD" goto :RetryE
Echo Allowing User Control For Assigning System Reserved Partitions...
Echo -Commands-
Echo Diskpart
Echo List volume
Echo Select Volume "" (The one that has no Letter and remember number)
Echo Assign Letter=Z
Echo Select Volume "" (The one that has Letter E and remember number)
Echo Remove Letter=E (This is the new system reserved partition)
Echo Then exit Diskpart to finish Part 1.
Diskpart
Echo Ready To Restart!
Timeout 5
Shutdown /r /t 30
Goto :Start
:RetryC
set/p RetryE=Retry? Y/N:
if '%RetryC%' == 'Y' goto :Touch
if '%RetryC%' == 'N' goto :Exit
:Touch
echo.
echo.
echo.
Echo Loading NDM...
Echo NOTE - Store the password somewhere safe!!!
Start "" "Data/NDM.exe"
Echo Loading EditedBCD...
bcdedit /import "Data/BCD/IncogBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryC
IF %ERRORLEVEL% LSS 0 goto :RetryC
Echo Process Complete!
Timeout 5
Echo Returning to menu...
goto :Start
:Manager
echo.
Echo NOTE - Store the password somewhere safe!!!
Start "" /WAIT "Data/ndm.exe"
Echo Returning to Menu...
goto :Start
:Data
echo.
echo.
echo.
echo Processing Data...
Systeminfo
diskpart /s "Data/discpart.txt"
Echo Returning to Menu...
goto :Start
:Exit
Exit
Thanks So Much!
bcdedit is on the path by default so unless you are reusing the PATH variable then the problem is in the batch compiler.
You can test this by using the plain batch file - and if it works then you know where the problem is.

Resources