Batch Script not Executing filaname + Parameters - batch-file

My batch script asks the user if he wants to run the program LegoScript.exe
If the user hits 'N' or 'n' then it terminates the program, however if the user types 'Y' or 'y' then it should echo the string "legoscript.exe" and then place the cursor right after it on the same line, so that the user can type additional arguments, and then run the program with the provided arguments, however rather than running the program with the provided arguments, it just shows me a blinking caret indefinitely
Here is the batch script:
#echo off
setlocal enabledelayedexpansion
set "base_path=%userprofile%\\desktop\\LegoScript\\"
for /d %%d in ("%base_path%\V*") do (
set "latest=%%d"
)
set "LATEST=!latest!"
echo Latest folder is: %LATEST%
"%base_path%\\MINGW\\Bin\\gcc.exe" "%latest%\\LegoScript.c" -o "%latest%\\LegoScript.exe"
set /p run=Do you want to run LegoScript.exe? (y/n):
if /i "%run%"=="y" (
#cmd /k ^"cmd /c "echo|set /p="LegoScript.exe "&set /p "arg1= "&& set /p "arg2= "&& set /p "arg3= "&& set /p "arg4= "&& start "" "%latest%\\LegoScript.exe" %arg1% %arg2% %arg3% %arg4%
) else if /i "%run%"=="n" (
exit
) else (
echo Invalid input. Please enter y or n.
pause >nul
call %0
)
Anyone care to figure out how to fix this?
I tried adding extra double quotes and escape characters ('^') but no luck.

Related

How to show what user typed in this command?

please help! I was looking for the answer all over the internet.
Here's my code:
#echo off
title var test
:question
set a1=This
set a2=Is
set a3=a
set a4=Var
set a5=Test
choice /c 12345 /m "press a number"
if errorlevel=5 set num=5&goto answer
if errorlevel=4 set num=4&goto answer
if errorlevel=3 set num=3&goto answer
if errorlevel=2 set num=2&goto answer
if errorlevel=1 set num=1&goto answer
:answer
echo now change the answer.
set /p a%num%=
FOR /F "tokens=1-6" %%1 IN ("%a1% %a2% %a3% %a4% %a5% a%num%") DO echo %%1 %%2 %%4 %%5.&echo You typed=%%6
pause
goto question
As you can see I made the user select a number between 1 and 5 to change the specific word. But when I try same kind of code to show what he typed doesn't work :(
Environment variables should never begin with a digit and using a digits for loop variables should be also avoided. Run in a command prompt window call /? and output is the help for this command explaining how batch file arguments can be referenced with %0, %1, %2, ... which explains why environment variables with digit as first character and loop variables with a digit are not good in general even on being
a%num% in set of FOR does not reference the value of environment variable a1 or a2 or a3 or a4 or a5. It is just the name of the environment variable. The for loop is not necessary at all.
#echo off
title var test
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
pause
goto question
The command line call echo You typed: %%a%num%%% is parsed by Windows command processor before execution of the command line on number 3 entered to call echo You typed: %a3%. This command line is parsed a second time because of command call resulting in replacing %a3% by the value of environment variable a3 and so echo outputs the expected string.
It would be also possible to replace call echo You typed: %%a%num%%% by
setlocal EnableDelayedExpansion
echo You typed: !a%num%!
endlocal
The usage of delayed environment variable expansion results also in double parsing the command line before execution of command echo. For more details see How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Please read also this answer for details about the commands SETLOCAL and ENDLOCAL.
The two lines below in batch code above are also not really good taking into account that the user can really enter anything.
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
For example if the user enters number 1 and on next prompt enters:
Your user name is:& setlocal EnableDelayedExpansion & echo !UserName!& endlocal & rem
Then the batch file does something completely different than designed for and outputs the user's account name.
Secure would be the batch code:
#echo off
title var test
setlocal EnableExtensions DisableDelayedExpansion
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
setlocal EnableDelayedExpansion
echo !a1! !a2! !a3! !a4! !a5!.
echo You typed: !a%num%!
endlocal
pause
goto question
Now the user input string cannot modify anymore the command lines executed by Windows command processor.
A solution with the useless FOR loop would be:
setlocal EnableDelayedExpansion
for /F tokens^=1-6^ eol^= %%A in ("!a1! !a2! !a3! !a4! !a5! !a%num%!") do echo %%A %%B %%C %%D %%E.&echo You typed: %%F
endlocal
eol= is necessary to output everything correct also if use enters number 1 and next a string starting with a semicolon. The FOR options string cannot be enclosed in double quotes in this case like "tokens=1-6 eol=" because of this would define " as end of line character and nothing is output if user enters number 1 and enters next a string starting with ". The equal sign and the space must be escaped with ^ to be interpreted as literal characters by cmd.exe on double parsing the entire for command line before execution of command for.
Note: The FOR loop solution does not work correct on user enters for first variable value the special command line string as posted above. So it is also not really secure.

set /a in If statement condition unable to work

Below are my simple calculator batch i trying to do, however on the set /a unable to do the job. What is the problem and my mistake?
#echo off
Title Calculator
:start
echo Press 1 for "+"
echo Press 2 for Exit
echo.
set /p input="Please choose your option: "
if %input%==1 (
echo.
set /p num1="Please enter first number: "
set /p num2="Please enter second number: "
set /a ans=%num1%+%num2%
echo Your answer is %ans%
pause
cls
goto start
)
if %input%==2 (
echo.
echo Thanks!
pause
exit
) else echo Invalid input!
pause
goto start
When i first run the batch is will return me the Missing operand. When i continue again the error disappear without giving me the answer, when the third time i continue, it return me the answer of the number that i wanted to add up.
For example:
1. 10+10 result is Missing operand
2. 1+1 result is empty
3. 2+2 result is 20 (which is the 2 number i enter at first time)
Please help what my error is. Thanks.
Here is your batch code with using delayed expansion and indents as wisely suggested by rojo:
#echo off
title Calculator
setlocal EnableExtensions EnableDelayedExpansion
:Begin
echo Press 1 for "+"
echo Press 2 for Exit
echo/
set "input="
set /P "input=Please choose your option: "
if "!input!"=="1" (
echo/
set /P "num1=Please enter first number: "
set /P "num2=Please enter second number: "
set /A ans=num1+num2
echo Your answer is !ans!
pause
cls
goto Begin
)
if "!input!"=="2" (
echo/
echo Thanks^^!
echo/
pause
exit /B
)
echo Invalid input^^!
echo/
pause
echo/
goto Begin
Variable input is always cleared before user is asked because otherwise the user could just hit key RETURN or ENTER to keep current value of variable input.
Delayed expansion is used on checking user input against 1 or 2 in case of user enters a character which would result in a syntax error on execution with not using delayed expansion. Better would be nevertheless the usage of command choice for first user prompt.
For an expression evaluated on runtime by using set /A the environment variables can be specified directly without being expanded at all. So instead of using
set /A ans=%num1%+%num2%
or
set /A ans=!num1!+!num2!
it is enough to write
set /A ans=num1+num2
because with parameter /A command set interprets num1 and num2 automatically as names of variables.
Delayed expansion is nevertheless needed to print the result value stored in variable ans because command processor expands otherwise %ans% by nothing respectively the value of previous run on parsing the entire block starting with ( and ending with ). This can be seen on running your batch file from within a command prompt window with first line changed to #echo on for debugging.
For more details run in a command prompt window set /? or help set and read the output help pages.
start is the name of a command. Therefore it is not good to use this word as name of a label although possible.
By the way: Always use set "variable=value" and never set variable="value" as this makes a big difference, see the answers on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to set environment variables with spaces?

i would seek assistance on the following batch script on set and goto

is there a way for me to set a batch script after user input , it set a variable and then use go to. for example
SET INPUTLocation=""
SET /P INPUTLocation=Please select a number:
echo you enter %INPUTlocation%
IF /I '%INPUTlocation%'=='1'set router="router1" GOTO testnow`$`
IF /I '%INPUTlocation%'=='2' set router="router2" GOTO testnow'$
IF /I '%INPUTlocation%'=='3' set router="router3" GOTO testnow'$'
You mean executing multiple commands? You can do that by putting them in parentheses:
SET INPUTLocation=""
SET /P INPUTLocation=Please select a number:
echo you enter %INPUTlocation%
IF /I '%INPUTlocation%'=='1' (
set router="router1"
GOTO testnow`$`
)
IF /I '%INPUTlocation%'=='2' (
set router="router2"
GOTO testnow'$
)
IF /I '%INPUTlocation%'=='3' (
set router="router3"
GOTO testnow'$'
)
Note, if you want to input a single character, you may also use the CHOICE command. It lets you select a single character from a specified set of characters, and it doesn't require you to press enter afterwards. For these kinds of menu-selections, it is more convenient than SET.

How to open a program using user input

I am writing a program that will open a program when the user types in: "Open chrome" then it will open chrome. I have this:
SET /P Start=What should i do?
IF /i "%start%"==" open calculator" GOTO CALCULATOR
:CALCULATOR
Start C:\windows\system32\calc.exe
So at Calculator it starts the program calculator.
But i want the user to type in "Open" and the code knows that "Open" = "Start" and whatever comes next is what it has to open.
So the code takes apart what the user types in so if they type in "Open" then it turns that into start and puts it in the code so:
set /p "Input= "
%Input% + %Input2.exe
so the first thing the user types in equals Input and the second thing the user types in equals Input2 so they can type in Start Calc and the code will be: Start calc.exe because it's added a .exe in the second word that the user types in.
When you give me an answer can you please explain what each bit does?
#echo off
setlocal
:nextCommand
echo/
set /P "input=What should I do? "
if /I "%input%" equ "exit" (
echo Bye...
exit /B
)
rem Try to change "open" by "start" in the input line
set "changed=%input:open=start%"
rem Check if the input changed
if "%changed%" equ "%input%" (
echo I don't know how to %input%
goto nextCommand
)
rem Execute the changed line adding ".exe" at end
%changed%.exe
echo Done!
goto nextCommand
I haven't actually tested this, but it should work.
for /F %%a in (%input%) do (
if %%a == "open" (
if %%b == "chrome" (
REM Do stuff
)
if %%b == "calculator" (
REM Do other stuff
)
)
)

Problems with BAT file: IF/ELSE not working as expected

I'm trying to make a short BAT file and I'm having trouble with one of its functions. I've tried a number of different ways to do this and none of them seem to work, but being a beginner at this I can't figure out the problem. Basically, the script, as it runs, is supposed to check if a certain .BAT file exists, and if it does, the script asks if the user wants to run it. If the user indicates Y, the other BAT is called and then the original script proceeds. If the user indicates N, the script is supposed to proceed without calling the other BAT. So far the script always notices and asks about the file, but choosing Y at the prompt never works. I'm sure the solution is obvious, but it's escaping me. Here's the code:
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
The problem is your variables are being evaluated before they enter the if's, which means cmd won't see any changes until they have ended.
This is causing problems for you as your variables kmpreproc and, depending on the first if result, kmname change within the if blocks.
The fix (presuming the rest of your code is working) is to enable delayed expansion and use delayed expansion instead of normal expansion, by changing the %'s to ! on your variables.
setlocal enabledelayedexpansion
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\!kmname!\!kmname!.flt (
ECHO !kmname!.flt found, will now create !kmname!.ive.
CD C:\Visual\area\!kmname!\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "!kmpreproc!" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\!kmname!\!kmname!.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
Here is below my corrected and tided up code :
#echo off
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%"=="Y" (
GOTO PREPROC
)
GOTO CONTINUE
)
)
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
:PREPROC
ECHO Running Preprocess.bat.
call "cmd /c start Preprocess.bat"
pause
:CONTINUE
ECHO Continuing process.
PAUSE
If it doesn't work try writting setlocal EnableDelayedExpansion under #echo off and replacing all % with ! .

Resources