Batch opening wrong file type - batch-file

So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks

Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe

Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona

Related

How do I start Minecraft and Valorant from a batch file?

Last night, I decided to try coding a batch file. I used some references and some online walkthroughs and ended up making the following program.
echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
start MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
start RiotClientServices.exe
exit
This program is a simple one that you log into with a username and password, which then leads to a directory of games to open. The krunker.io works, but I wasn't sure on how to call Minecraft and Valorant. The processes that open up the games are what I called. For example, MinecraftLauncher.exe is the name of the process that would launch Minecraft. When I choose Minecraft from the menu, it gives the error message 'Windows cannot find "MinecraftLauncher.exe" Make sure you typed the name correctly, and then try again.` This is strange given this is the literal name of the launcher. I was wondering if anyone with a bit more experience could help me out.
You have to go thru the path to initialize the application. Here's a step-by-step:
cd/
cd "Program Files (x86)/Minecraft"
start MinecraftLauncher.exe
You have to ensure that cmd is at c:\ using cd/, then go to launcher file location and starts it. You can do that by using start/d.
So, here's your code:
#echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
cd/
start/d "C:\Program Files (x86)\Minecraft" MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
cd/
start/d "C:\Program Files (x86)\*VALORANT LAUNCHER FOLDER*" RiotClientServices.exe
exit
And your login system is broken. If you put any key other than 1 or 2, you go direct to :GameMenu, it can be easily solved by putting :error at the very end of the file or using a goto home before echo You have to select a valid option!
Hope this helps you.

Batch Script: Using GOTO on IF statement

When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if %choice%=="fire" GOTO fireplace
if %choice%=="desk" GOTO desk
if %choice%=="door" GOTO door
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
:desk
echo.
echo You go to the desk.
echo.
:door
echo.
echo You go to the door.
echo.
Doublequote %choice% or it will not be equal: desk is not equal as "desk".
And exit your Label block with a goto:eof or exit/b.
Use the /i switch with IF so you can also use DESK or DesK
if /i "%choice%"=="fire" GOTO fireplace
if /i "%choice%"=="desk" GOTO desk
if /i "%choice%"=="door" GOTO door
goto:error
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b
:desk
echo.
echo You go to the desk.
echo.
exit/b
:door
echo.
echo You go to the door.
echo.
exit/b
You need to protect the entry points of each of your target blocks, otherwise when it's done executing the target block, it will "fall into" the next block.
Before any label line (e.g., :fireplace), you'll need a GOTO to make sure that the program flow doesn't "fall into" the next routine:
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if /I "%choice%" EQU "fire" GOTO fireplace
if /I "%choice%" EQU "desk" GOTO desk
if /I "%choice%" EQU "door" GOTO door
GOTO END
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
GOTO END
:desk
echo.
echo You go to the desk.
echo.
GOTO END
:door
echo.
echo You go to the door.
echo.
:END
Note also the changes to the IF statements. These allow you to handle the case of typing FIRE or Fire instead of just fire.
In addition to the tips from Jeff and SachaDee,
if you name all your labels equal to the choice you could use a loop
For %%A in (fire desk door) Do If /i "%choice%" equ "%%A" Goto %choice%
Or with a llimited number of choices you could use choice.exe and work with a single letter answer (no enter needed then) and evaluate the returned errorlevel.

My batch script doesn't use if command correctly

I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

goto unexpected at this time error for game test

When I run this, it keeps saying "goto" unexpected at this time. I have tried to solve the issue, but it won't work.
Here is the code:
#echo off
color 0a
title TEST FOR HACK SIM
:menu
ECHO what do you want to test?
echo.
echo -open disk
echo -save save
echo -open save
echo.
echo.
set /p choice= choose one:
if %choice% == "open disk" goto disk
if %choice% == "open save" goto saveo
if %choice% == "save save" goto saves
:disk
call D:\
echo did it work!?
goto menu
:saveo
cls
pause
(
set /p save=
)<C:\Users\%username%\Searches\savefiles.txt
echo %save%
pause
goto menu
:saves
set /p fun= what number do you want?>
(
echo %fun%
)>C:\Users\%username%\Searches\savefiles.txt

How to detect invalid user input in a Batch File?

I want to use a batch file to ask for a password to continue, i have very simple code that works.
#echo off
:Begin
cls
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
I need to detect when the user entered an invalid password, i have spent an hour researching but i found nothing. I'm not advanced in any way so try and keep it very simple like the code above.
Please help me.
You were missing exactly one line of code!
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
cls
Echo %pass% is not the PASSWORD
goto :Begin
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
If the password is invalid the code will simply continue to the next line which now is goto :Begin. This will restart the sequence. I changed the order around a bit so that the screen was cleared as well and the error was printed.
Similar to Monacraft's answer, but using if-else statements:
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password (goto Start) else *echo Invalid password. && goto :Begin)
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
What this does is it checks for the correct password. If it isn't the correct password, it will display an error message and go back to :Begin, which restarts the sequence.

Resources