Batch program exiting for no reason without error
Batch program exiting for no reason without error
I Am Making A Batch Program For My Other Program "Super Command Promt" (Check it out on pastebin its really good!) And I Noticed It Was A Bit Tricky To Make Plugins For It So I Decided To Start Working On A Simple Compiler To Make It Easier To Make Plugins, But I Cant Seem To Get It To Work! I Open The File Type The Command:
compile test.txt test.scmd
But It Wont Work It Just Exits Without A Error Instead Of Making The File And Going Going Back To The Prompt Screen
Heres the Script:
REM Made By LiamBogur
REM To install copy into notepad and press save as select all files and save it as "SCMDCOMPILER.bat"
REM Feel free to message me or email me at: liamhmccracken@gmail.com (I dont check my email much so try messaging me first)
cls
@echo off
set Version=1.0.0
title Super Command Prompt Plugin Compiler
echo Super Command Prompt Plugin Compiler [Version %Version%]
echo.
:A
set CommandA=
set Command=
set CommandB=
set CommandC=
set /p CommandA=^>
for /f "tokens=1" %%i in ("%CommandA%") do set Command=%%i
for /f "tokens=2" %%i in ("%CommandA%") do set CommandB=%%i
for /f "tokens=3" %%i in ("%CommandA%") do set CommandC=%%i
if /I %Command%==HELP echo Compiler ^| HELP Displays This Message
if /I %Command%==HELP echo Compiler ^| COMPILE [inputfile] [outputfile] Compiles The Input File Into SCMD Format (eg. input.txt output.scmd)
if /I %Command%==HELP echo Compiler ^| EXIT Exits The Compiler
if /I %Command%==HELP echo Commands ^| COMMAND [command] [commands] Makes The Command "command" And Makes It Run "commands"
if /I %Command%==HELP echo Commands ^| RUN [commands] Run Some Commands In Batch
if /I %Command%==HELP echo Commands ^| COMMENT [comment] Adds A Comment To Your File
if /I %Command%==HELP echo Commands ^| HELP [message] Adds That Message To PHELP (eg."HELP Restart Restarts The Computer")
if /I %Command%==EXIT exit
if /I %Command%==COMPILE goto B
goto A
:B
echo Decoding %CommandB%.
for /f "delims=*" %%i in (%CommandB%) do (
set WordN=0
for /f "tokens=*" %%u in ("%%i") do (
set /A WordN+=1
if %WordN%==1 (
set CCommand=0
if /I %%u==COMMAND (
set CCommand=1
set LineO=if ^%A^%==2 if /I ^%Command^%==
)
if /I %%u==RUN (
set CCommand=2
set LineO=call
)
if /I %%u==COMMENT (
set CCommand=3
set LineO=rem
)
if /I %%u==HELP (
set CCommand=4
set LineO=if ^%A^%==2 if /I ^%Command^%==PHELP echo %CommandC:~0,-5% ^^^|
)
) else (
if CCommand==1 (
set LineO=%LineO%%%u
) else (
set LineO=%LineO% %%u
)
)
echo %LineO%>>C:UsersPublicTemp.txt
)
mv C:UsersPublicTemp.txt %CommandC%
goto A
I really need help with this one!
Edit 1:
Heres the code for test.txt:
COMMENT Lol this is cool
COMMAND test Lol 123 456
HELP Oh No!
And Heres What Its Meant To Look Like Compiled
REM Lol this is cool
if %A%==2 if /I %Command%==test Lol 123 456
if %A%==2 if /I %Command%==PHELP echo TEST ^| Oh No!
Start by commenting out
@echo off
. Disabling echo will mean that you won't see what commands are being executed when the script runs.– mojo
Jul 2 at 18:03
@echo off
If you solved your question, post the solution, then mark it as the answer as soon as you can.
– emix
Jul 3 at 9:26
2 Answers
2
The error message I see after ensuring that echo is on is:
echo was unexpected at this time.
This is not entirely enlightening, but it does suggest a syntax error (though not with echo—probably with what comes before it).
The fundamental problem I see with what you're doing is that you're doing string comparisons with variables that may be empty, and you're not enclosing them inside quotes. This becomes a syntax error, because then cmd runs a command that looks like
if /I ==HELP echo Commands ^| HELP [message]
cmd is not smart about variables. It's doing a straightforward substitution wherever the variable is referenced.
The typical method for constructing non-fragile (robust is an overstatement when applied to anything regarding cmd) is to enclose both LHS and RHS values within quotes so that empty values result in syntactically legal statements.
if /I "%Command%"=="HELP" echo Commands ^| HELP [message]
This way, when the substitution takes place (for the last line of input) in which Command
is empty, the command executed looks like this
Command
if /I ""=="HELP" echo Commands ^| HELP [message]
which is syntactically legal.
You probably want to use EXIT /B
rather than just EXIT
, because EXIT
by itself will end the calling cmd process, which, in testing, is usually your cmd window. Most people don't want this to happen. EXIT /B
will end the script without ending the shell process.
EXIT /B
EXIT
EXIT
EXIT /B
Thanks! But I Fixed It Now! I Re-coded The Compiling Part And Also Figured Out That Batch Doesnt Really Like Nested Loops So I Created A Function And The Loop Just Calls The Function So Its Technically Not Inside Another Loop!
– LiamBogur
Jul 3 at 9:22
I feel pity for your
SHIFT
key.– emix
Jul 3 at 9:27
SHIFT
Needed for loops "work" just as well as any other programming construct in batch. Nested goto loops are almost as cumbersome as in any other language.
– mojo
Jul 3 at 10:56
I Figured Out That For Some Reason For My Specific Program Nested For Loops Were Being A Bit Buggy Where They Would Just Behave Randomly And Didn't Really Work So I Created A Function And Put My Second For Loop In There And Just Had The First Call The Function, Then It Worked Perfectly!
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why do you capitalise every single word?? Anyway, you should run your batch file by opening a command prompt window and typing its path rather than double-clicking it, so you can see any error messages...
– aschipfl
Jul 2 at 12:10