Batch program closes with Enter even if no option is selected - batch-file

I'm new to stackoverflow!
Straight to it:
PC / Windows 10 x64
Script level: Novice
I've written a basic batch program with selectable menu, however, if Enter is made with nothing from the menu, the batch program closes.
I'm looking for a simple script that will make Enter (with no selection) essentially do nothing, or return to the beginning of the batch program.
Thanks in advance.
Jon
:mainmenu
#echo off
cls
color 1f
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo WINDOWS COMMAND UTILITY (MAIN MENU):
echo ------------------------------------
echo.
echo [1] - BOOT COMMANDS
echo.
echo [2] - WINDOWS HEALTH COMMANDS
echo.
echo [0] - Exit Menu (Closes this window)
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO bootcommands
IF %M%==2 GOTO winhealth
IF %M%==0 GOTO exit
GOTO exit
:bootcommands
#echo off
cls
color 4e
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo BOOT COMMANDS:
echo --------------
echo.
echo [1] - Windows (Normal Startup)
echo.
echo [2] - Windows Safe Mode (Offline)
echo.
echo [3] - Windows Safe Mode with Networking
echo.
powershell write-host -fore yellow -back red Tampering with the UEFI/Bios may render the PC unbootable!
echo [8] - Boot into UEFI/Bios
echo.
echo [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO normal
IF %M%==2 GOTO safe
IF %M%==3 GOTO safenetwork
If %M%==8 GOTO bootbios
IF %M%==0 GOTO mainmenu
GOTO exit
:normal
bcdedit /deletevalue {default} safeboot
goto reboot
exit
:safe
bcdedit /set {default} safeboot minimal
goto reboot
exit
:safenetwork
bcdedit /set {default} safeboot network
goto reboot
exit
:bootbios
shutdown /r /fw /f /t 2
exit
:reboot
shutdown /f /t 2
:winhealth
#echo off
cls
color 1f
echo.
echo =========================================================
echo = R U N A S A D M I N I S T R A T O R =
echo =========================================================
echo.
echo.
echo WINDOWS HEALTH COMMANDS:
echo ------------------------
echo.
echo [1] - *Create a System Restore Point
echo.
echo [2] - Elevated Command Prompt (Administrator Privileges)
echo.
echo [3] - SFC (Offline) File System Checker
echo.
echo [4] - DISM CheckHealth (Offline) Deployment Image Servicing and Management
echo.
echo [5] - DISM ScanHealth (Offline) Deployment Image Servicing and Management
echo.
powershell write-host -fore red -back yellow Option 6 requires Internet Connection!
echo [6] - DISM RestoreHealth (Online) Deployment Image Servicing and Management
echo.
echo [7] - CHKDSK (Offline) Check Disk checks the file system and file system
echo metadata of a volume for logical and physical errors.
echo.
echo [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
IF %M%==1 GOTO createrestore
IF %M%==2 GOTO cmdadmin
IF %M%==2 GOTO sfc
IF %M%==3 GOTO dismhealth
IF %M%==4 GOTO dismscan
IF %M%==5 GOTO dismrestore
IF %M%==6 GOTO chkdsk
IF %M%==0 GOTO mainmenu
GOTO exit
:createrestore
cls
echo.
echo Creating a System Resore point. Please be patient...
echo.
echo.
wmic.exe /Namespace:\\root\default Path SystemRestore Call CreateRestorePoint "Windows Command Utility", 100, 7
echo.
exit
:cmdadmin
start cmd /k
exit
:sfc
cls
echo.
echo Starting System File Checker. This is a utility in Microsoft Windows
echo that scans for and restores corrupted Windows system files.
cmd /k sfc /scannow
exit
:dismhealth
cls
echo.
echo This is a quick scan and will determine if the image is repairable.
cmd /k DISM /Online /Cleanup-Image /CheckHealth
exit
:dismscan
cls
echo.
echo This is a quick scan and will determine if there are any corruptions detected.
cmd /k DISM /Online /Cleanup-Image /ScanHealth
exit
:dismrestore
cls
echo.
echo Your system will automatically connect to the Windows Update
echo service to download and replace the corrupt files.
cmd /k DISM /Online /Cleanup-Image /RestoreHealth
exit
:chkdsk
cmd /k chkdsk.exe /f /r /x
exit
:exit
exit

As per my comment, the fix is to use the correct command utility, choice.exe.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
:mainmenu
ClS
Color 1F
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo WINDOWS COMMAND UTILITY (MAIN MENU):
Echo ------------------------------------
Echo=
Echo [1] - BOOT COMMANDS
Echo=
Echo [2] - WINDOWS HEALTH COMMANDS
Echo=
Echo [0] - Exit Menu (Closes this program)
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 120 /N /M "Input option [Number]"
If ErrorLevel 3 Exit /B
If ErrorLevel 2 GoTo winhealth
:bootcommands
ClS
Color 4E
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo BOOT COMMANDS:
Echo --------------
Echo=
Echo [1] - Windows (Normal Startup)
Echo=
Echo [2] - Windows Safe Mode (Offline)
Echo=
Echo [3] - Windows Safe Mode with Networking
Echo=
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoP "Write-Host -Fore Yellow -Back Red 'Modifying the UEFI/BIOS may render the PC unbootable!'"
Echo [8] - Boot into UEFI/BIOS
Echo=
Echo [0] - Return to MAIN MENU
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 12380 /N /M "Input option [Number]"
If ErrorLevel 5 GoTo mainmemu
If ErrorLevel 4 GoTo bootbios
If ErrorLevel 3 GoTo safenetwork
If ErrorLevel 2 GoTo safe
:normal
%SystemRoot%\System32\bcdedit.exe /deletevalue {default} safeboot
goto reboot
:safe
%SystemRoot%\System32\bcdedit.exe /set {default} safeboot minimal
goto reboot
:safenetwork
%SystemRoot%\System32\bcdedit.exe /set {default} safeboot network
goto reboot
:bootbios
%SystemRoot%\System32\shutdown.exe /r /fw
Exit /B
:reboot
%SystemRoot%\System32\shutdown.exe /r
Exit /B
:winhealth
ClS
Color 1F
Echo=
Echo =========================================================
Echo = R U N A S A D M I N I S T R A T O R =
Echo =========================================================
Echo=
Echo=
Echo WINDOWS HEALTH COMMANDS:
Echo ------------------------
Echo=
Echo [1] - Create a System Restore Point
Echo=
Echo [2] - Elevated Command Prompt (Administrator Privileges)
Echo=
Echo [3] - SFC (Offline) File System Checker
Echo=
Echo [4] - DISM CheckHealth (Offline) Deployment Image Servicing and Management
Echo=
Echo [5] - DISM ScanHealth (Offline) Deployment Image Servicing and Management
Echo=
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoP "Write-Host -Fore Red -Back Yellow 'Option 6 requires an internet connection!'"
Echo [6] - DISM RestoreHealth (Online) Deployment Image Servicing and Management
Echo=
Echo [7] - CHKDSK (Offline) Check Disk checks the file system and file system
Echo metadata of a volume for logical and physical errors.
Echo=
Echo [0] - Return to MAIN MENU
Echo=
Echo=
%SystemRoot%\System32\choice.exe /C 12345670 /N /M "Input option [Number]"
If ErrorLevel 8 GoTo mainmenu
If ErrorLevel 7 GoTo chkdsk
If ErrorLevel 6 GoTo dismrestore
If ErrorLevel 5 GoTo dismscan
If ErrorLevel 4 GoTo dismhealth
If ErrorLevel 3 GoTo sfc
If ErrorLevel 2 GoTo cmdadmin
:createrestore
Rem Rest of your code, irrelevant to your question, goes here.

Related

Is there a way I could check the status of a Pi-Hole through a batch script

I'm trying to make a script that can help me remotely monitor my Pi-Hole through batch, and I want to add a feature that checks if the Pi-Hole is actually on, possibly by using ping, but I can't figure out how to.
Current Code:
ECHO OFF
ECHO CHECKING PI-HOLE STATUS
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Pi-Hole Console
ECHO 2 - Pi-Hole Network Interface
ECHO E - EXIT
ECHO.
SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF
IF %M%==e GOTO EOF
REM ------------------------------
REM SSH Server details
REM ------------------------------
:WEB1
CLS
call ssh pi#192.168.1.10
GOTO MENU
:WEB2
CLS
start opera pi-hole/admin
GOTO MENU
:EOF
taskkill /f /im cmd.exe

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.

I am trying to make my own Batch Game, but when I try to run the Script/Code it Instantly Closes

Alright, so I am trying to make a batch game. I have already coded/scripted the menu. So I tried to run it to see if it works, but it instantly closes! could anyone take a look at the code/script to help me find out what I am doing wrong?
Also I am using Notepad++
#echo off
title Dungeon Slayer - A Text RPG
:MainMenu
cls
echo.
echo Dungeon Slayer
echo A Text RPG
echo.
echo Build: A1
echo.
echo 1. Play
echo.
echo 2. Settings
echo.
echo 3. Changelog
echo.
echo 4. Exit
echo.
set /p MainMenuSelection
if /p "%MainMenuSelection%" EQU "1" goto :Play
if /p "%MainMenuSelection%" EQU "2" goto :Settings
if /p "%MainMenuSelection%" EQU "3" goto :Changelog
if /p "%MainMenuSelection%" EQU "4" goto :Exit
goto :MainMenu
:Play
cls
echo.
echo Currently Gameplay Development has not been started.
echo.
pause
goto :MainMenu
:Settings
cls
echo.
echo Currently we have not made any settings.
echo.
pause
goto :MainMenu
:Changelog
cls
echo.
echo 11/16/2018
echo.
echo -A1 Build Released
echo.
pause
goto :MainMenu
:Exit
cls
exit
Here's an example using Choice.
#Echo Off
Set "Build=A1"
Set "BDate=11/16/2018"
Set "GName=Dungeon Slayer"
Set "GDesc=A Text RPG"
Title %GName% - %GDesc%
:MainMenu
ClS
Echo=
Echo=%GName%
Echo=%GDesc%
Echo=
Echo Build: %Build%
Echo=
Echo 1. Play
Echo=
Echo 2. Settings
Echo=
Echo 3. Changelog
Echo=
Echo 4. Exit
Echo=
Choice /C 1234 /M "Which item"
If ErrorLevel 4 Exit /B
If ErrorLevel 3 GoTo ChangeLog
If ErrorLevel 2 GoTo Settings
:Play
ClS
Echo=
Echo Sorry, gameplay development has not yet started.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:Settings
ClS
Echo=
Echo Sorry, we are yet to create any settings.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:ChangeLog
ClS
Echo=
Echo=%BDate%
Echo=
Echo -%Build% Build Released
Echo=
Timeout 3 >Nul
GoTo MainMenu

Batch file autocloses after running

I am learning A LOT lately and have managed to get a basic bat file using script i have found online and mixing it all into one.
I have hit a small glitch where one of the lines now exports to a text file in the location the file is run from.
My issue now is that it autocloses after creating the text file rather than going back to the main start screen.
Can anyone help me out with this?
I have put the script that autocloses below for reference.
the final bit seems to close it (goto :eof) but if i remove that it stops exporting to the text file?
*Still to do - backup of pst files
title CMD Utility (C) Geoff Palmer Version 1.2
mode 48,40
#echo off
echo (C) Geoff Palmer Version 1.0
echo.
cls
goto start
rem ####################### START ######################
:start
echo (C) Geoff Palmer Version 1.2
echo.
mode 48,40
color 0f
#echo off
for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b
echo %DisplayName% Welcome to the CMD utility (Beta)!!
echo What option would you like to execute?
echo.
echo NETWORKING:
echo _____________
echo 1) Ipconfig
echo 2) Reset Network
echo 3) Ping an ip address
echo 4) Trace Root
echo 5) NetStat
echo 6) Get Mac address
echo 7) Backup saved Wifi Networks
echo.
echo SYSTEM INFO:
echo ______________
echo 8) Who am I
echo 9) Uptime
echo 10) PC Info
echo 11) Performance Check
echo 12) Disk Check
echo 13) Driver Check
echo 14) Mirror Drive
echo 15) Recover Drive
echo.
echo WINDOWS TOOLS:
echo __________________
echo 16) Clear recycle bin and Temporary files
echo 17) Hard drive Defrag
echo 18) Start or End a Task
echo 19) Battery Performance
echo 20) Outlook Backup
echo.
echo Cleanup:
echo __________________
echo 21) Remove Battery Performance Log
echo 22) Remove Driver Log
echo.
echo 23) Exit
echo.
set option=
echo.
set /p option=Select One:
echo.
if "%option%"=="1" goto ipconfig
if "%option%"=="2" goto Network
if "%option%"=="3" goto ping
if "%option%"=="4" goto tracert
if "%option%"=="5" goto netstat
if "%option%"=="6" goto getmac
if "%option%"=="7" goto Wifi
if "%option%"=="8" goto whoami
if "%option%"=="9" goto uptime
if "%option%"=="10" goto pcinfo
if "%option%"=="11" goto perfmon
if "%option%"=="12" goto CHKDSK
if "%option%"=="13" goto DRIVER
if "%option%"=="14" goto MIR
if "%option%"=="15" goto recover
if "%option%"=="16" goto clearmgr
if "%option%"=="17" goto defrag
if "%option%"=="18" goto task
if "%option%"=="19" goto Power
if "%option%"=="20" goto pst
if "%option%"=="21" goto Cleanpwr
if "%option%"=="22" goto Cleandrv
if "%option%"=="23" goto Exit
echo.
ECHO “%option%” is not valid please try again
goto start
ECHO.
rem ###################### Options #####################
rem ###################### ipconfig #######################
:ipconfig
mode 70,40
cls
color 0c
echo.
ipconfig
pause
cls
goto start
rem ###################### Network #######################
:Network
mode 80,40
cls
color 0A
cls
ipconfig /release
ipconfig /renew
ipconfig /registerdns
goto start
rem ###################### ping #######################
:ping
cls
color 0b
#echo off
mode 45,2
color 0F
set /p a="Enter IP ADDRESS: "
MODE 60,15
ping %a%
echo [----------------------------------------------------------]
pause
cls
goto start
rem ###################### tracert #######################
:tracert
mode 60,5
cls
color 0b
echo Please enter web page you would like to trace?
echo [EXAMPLE] http://www.google.com
echo.
Set /p trace=
mode 75,50
echo.
echo Trace Root initiate
tracert %trace%
pause
cls
goto start
rem ###################### netstat #######################
:netstat
#echo off
color 0F
:options
mode 45,10
cls
echo MENU OPTIONS:
echo =====================
echo a All links
echo e Ethernet Statistics
echo f Displays FDQN
echo n Addresses and ports
echo =====================
echo q to quit
echo =====================
set /p input="Enter Options Here: "
if "%input%"=="a" goto:a
if "%input%"=="e" goto:e
if "%input%"=="f" goto:f
if "%input%"=="n" goto:n
if "%input%"=="q" goto:start
echo Please choose from the Options listed above!
pause
goto:options
:a
mode 80,60
netstat.exe -a
pause
goto:options
:e
mode 80,15
netstat.exe -e
pause
goto:options
:f
mode 80,25
netstat.exe -f
pause
goto:options
:n
mode 80,20
netstat.exe -n
pause
goto:options
rem ###################### getmac #######################
:getmac
mode 60,25
#echo off
cls
color 0e
echo.
arp -a
echo.
echo Have you got all the info needed?
pause
cls
goto start
rem ###################### Wifi #######################
:Wifi
mode 90,10
cls
echo This program will save all Wifi passwords on this computer to file then close.
pause
#echo off
setlocal enabledelayedexpansion
:main
title WiFiPasswordReveal v1.0b (c) Geoff Palmer
echo. >> results.txt
echo Reveal all saved WiFi passwords Batch file script v1.0b (c) Geoff Palmer >> results.txt
echo. >> results.txt
:: Get all the profiles
call :get-profiles r
:: For each profile, try to get the password
:main-next-profile
for /f "tokens=1* delims=," %%a in ("%r%") do (
call :get-profile-key "%%a" key
if "!key!" NEQ "" (
echo WiFi: [%%a] Password: [!key!] >> results.txt
)
set r=%%b
)
if "%r%" NEQ "" goto main-next-profile
echo. >> results.txt
goto :eof
::
:: Get the WiFi key of a given profile
:get-profile-key <1=profile-name> <2=out-profile-key>
setlocal
set result=
FOR /F "usebackq tokens=2 delims=:" %%a in (
`netsh wlan show profile name^="%~1" key^=clear ^| findstr /C:"Key Content"`) DO (
set result=%%a
set result=!result:~1!
)
(
endlocal
set %2=%result%
)
goto :eof
::
:: Get all network profiles (comma separated) into the result result-variable
:get-profiles <1=result-variable>
setlocal
set result=
FOR /F "usebackq tokens=2 delims=:" %%a in (
`netsh wlan show profiles ^| findstr /C:"All User Profile"`) DO (
set val=%%a
set val=!val:~1!
set result=%!val!,!result!
)
(
endlocal
set %1=%result:~0,-1%
)
goto :eof
goto start
rem ###################### whoami #######################
:whoami
mode 45,5
color 0d
whoami
pause
cls
goto start
rem ###################### uptime #######################
:uptime
mode 50,4
color 0d
systeminfo | find "System Boot Time:"
pause
cls
goto start
rem ###################### pcinfo #######################
:pcinfo
mode 80,60
cls
color 0e
systeminfo
pause
cls
goto start
rem ###################### perfmon #######################
:perfmon
mode 45,5
cls
color 0b
echo.
echo Please wait program starting!
PERFMON
pause
cls
goto start
rem ###################### CHKDSK#######################
:CHKDSK
mode 48,5
cls
echo Please enter the drive you would like to check?
echo.
Set /p DSK=
echo.
cls
echo Check disk initiate
CHKDSK %DSK%:
pause
cls
goto start
rem ###################### clearmgr#######################
:clearmgr
echo Just follow the On Screen instructions and Press OK, Press Delete and OK.
CleanMgr
echo DONE!!
pause
cls
goto start
rem ###################### defrag #######################
:defrag
echo You will need to run this program as Administrator
echo This will Defrag all volumes:
pause
defrag /C /H /V
pause
cls
goto start
rem ###################### DRIVER#######################
:DRIVER
mode 80,50
cls
echo Querying Drivers
echo Exporting to C Drive
driverquery /v /fo csv > "C:\driverlist.csv"
pause
C:\users\%username%\desktop\driverlist.csv
goto start
rem ###################### MIR #######################
:MIR
color 0c
echo Please Enter the Source DRIVE
echo.
set /p SOURCE=
echo Please Enter the Destination DRIVE
echo.
set /p DEST=
ROBOCOPY %SOURCE%: %DEST%: /MIR
pause
cls
goto start
rem ###################### recover #######################
:recover
echo Please Enter the Source file path
echo.
set /p SOURCE=
TYPE SOURCE
pause
cls
goto start
rem ###################### Power Check #######################
:Power
mode 80,10
cls
echo You will need to run this program as Administrator
echo.
echo This will save a battery report in the root hard drive
echo.
powercfg /batteryreport /output "C:\battery_report.html"
Pause
C:\battery_report.html
goto start
rem ###################### task #######################
:task
mode 40,10
cls
echo TASK KILL OR START TASK MENU
echo.
echo What option would you like to execute?
echo.
echo T) Tasklist
echo 1) Task Kill
echo 2) start task
echo 3) back
echo.
set option=
echo.
set /p option=Select One:
echo.
if "%option%"=="T" goto tasklist
if "%option%"=="1" goto taskkill
if "%option%"=="2" goto starttask
if "%option%"=="3" goto back
rem ################# tasklist ###################
:tasklist
mode 48,50
cls
echo The fowlling process are actives!
tasklist
pause
cls
goto task
rem ################# taskkill ###################
:taskkill
echo What process would you like to end?
echo.
set /p task_name= task name without .exe
taskkill /f /im %task_name%.exe
pause
cls
goto task
rem ################# starttask ###################
:starttask
echo What process would you like to start?
echo.
set /p task_name=
start %task_name%
cls
pause
goto task..
rem ################# back ###################
:back
cls
goto start
rem ###################### cleanpwr #######################
:Cleanpwr
cls
echo Removing Battery Performance Log
pause
del /f C:\battery_report.html
echo.
echo File Deleted!
pause
cls
goto start
rem ###################### cleandrv #######################
:Cleandrv
cls
echo Removing Driver List
pause
del /f C:\driverlist.csv
echo.
echo File Deleted!
pause
cls
goto start
rem ###################### PST #######################
:pst
mode 80,40
echo Format your USB stick you want to save PST files to as P
pause
cls
color 0a
robocopy "C:\users\%username%\appdata\local\Microsoft\Outlook" "P:\Backup" /mir
pause
cls
goto start
rem ###################### exit #######################
:exit
mode 45,5
cls
echo Are you sure you want to EXIT?
echo Select c to cancel or press ok to exit
set /p input="Enter Options Here: "
if "%input%"=="ok" goto:ok
if "%input%"=="c" goto:start

Batch variables do not work

I have the following batch script:
#echo off
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar %argi% %argii%
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit
The problem is that if I run it on the first time, the java jar file is throwing exceptions because it doesn't understands the variable arguments %argi% and %argii%. I press [b] to start again at the options menu, I do the same thing again and voila, it works! Why? Why it doesn't work at the first time, and if i press [b] to go back and put again the args it works fine every time?
I start the app:
If I kill the script with Ctrl-C and reopen it, it will have the same behaviour again...first time error, and then success.
Thank you!
you need delayed expansion :
#echo off
setlocal enableDelayedExpansion
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar !argi! !argii!
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit

Resources