Bat file to query service status with options - batch-file

I have this query to check the status of a service and give options based on that status but I am having some issues in the error portion. IT only seems to work of there is not an option chosen. Even then it only works the first time. AS you can see I have 3 options but if i select option 4 it defaults to option 1. PLease take a look and explain what I am missing.
#echo off
ECHO Status of MGT System Manager Service
Goto Welcome
ECHO.
:Welcome
sc query MGTSM | findstr /i "STATE"
ECHO What would you like to do?
ECHO 1. Start Service
ECHO 2. Stop Service
ECHO 3. Exit
set /p choice=Select your choice.
if '%choice%'=='' ECHO "%choice%" is not valid please try again.
if '%choice%'=='1' goto Start Service
if '%choice%'=='2' goto Stop Service
if '%choice%'=='3' goto Exit
ECHO.
:Start Service
net start MGTSM
goto Welcome
:Stop Service
net stop MGTSM
goto Welcome
:Exit
exit

set /p does not touch the variable, if you enter nothing (just ENTER). Therefore you should delete the variable before:
:Welcome
....
set "choice=" //this deletes %choice%
set /p "choice=Select your choice: "
if '%choice%'=='1' goto StartService
if '%choice%'=='2' goto StopService
if '%choice%'=='3' goto Exit
REM this line is only reached, when all of the "IF ... GOTO" above failed
echo not valid input: "%choice%"
goto welcome
...
Labels and goto have a problem with spaces: goto start service will go to "start", ignoring "service". Same with Labels: :Start Service, the Label is :Start, the "Service" is ignored.

Related

Incorrect syntax when asking for user input

I am trying to make a quick program in Batch that asks for user input, but when i get to the input part, the script exits with error:
The syntax of the command is incorrect.
This is my code:
#echo off
color 0a
title WinDoS/PoD- 0.1
echo Automatic DoS/PoD Tool
echo Select what you want to do.
echo 1. Attack
echo 2. Instructions
echo 3. IP Getter
echo 4. IP Searcher
echo 5. Exit
set /p userchoice=
if %userchoice% == 1
goto attack
if %userchoice% == 2
goto instructions
if %userchoice% == 3
goto getter
if %userchoice% == 4
goto searcher
if %userchoice% == 5
exit
:searcher
arp -a
pause
:attack
echo Enter IP adress to attack
set /p address=
goto sequence
:sequence
ping %address% -l 65500 -w 1 -n 1
goto sequence
:instructions
echo WinDoS/PoD Instructions
echo WinDoS/PoD is a program that allows you to perform Denial Of Service (DoS) attacks using a method known as Ping Of Death (PoD)
echo Press 1 on the home menu to enter the attacker program.
echo Input an ip address to DoS
echo If you don't know the IP address to a site, use the built in getter.
echo If you want to view all available network targets, use the built in searcher.
pause
:getter
echo Website (full link):
set /p %website%=
tracert %website%
pause
What is wrong with the syntax?
You're using the wrong command for choosing a known list item, you should be using choice, not set.
Here's an example using your provided information as its basis:
#Echo Off
Color 0A
Title WinDoS/PoD- 0.1
:Menu
Echo(
Echo Automatic DoS/PoD Tool
Echo(
Echo Home Menu
Echo(
Echo 1. Attack
Echo 2. Instructions
Echo 3. IP Getter
Echo 4. IP Searcher
Echo 5. Exit
Echo(
%__AppDir__%choice.exe /C 12345 /N /M "Select a Home Menu item number>"
If ErrorLevel 5 GoTo :EOF
If ErrorLevel 4 GoTo Searcher
If ErrorLevel 3 GoTo Getter
If ErrorLevel 2 GoTo Instructions
:Attack
ClS
Set /P "address=Enter IP address to attack> "
:Sequence
%__AppDir__%ping.exe %address% -l 65500 -w 1 -n 1
GoTo Sequence
:Instructions
ClS
Echo WinDoS/PoD Instructions
Echo(
Echo This program allows you to perform Denial Of Service (DoS) attacks,
Echo using a method known as Ping Of Death (PoD).
Echo(
Echo Press 1 on the Home Menu to begin the Attack program.
Echo Input an IP address to DoS
Echo If you don't know the IP address for a site, use the built-in Getter.
Echo If you want to view all available network targets, use the built-in Searcher.
Echo(
Pause
GoTo Menu
:Getter
ClS
Set /P "website=Website (full link)> "
%__AppDir__%tracert.exe %website%
Pause
GoTo Menu
:Searcher
ClS
%__AppDir__%arp.exe -a
Pause
GoTo Menu
Please note, that I have not looked at the usage of any of your commands, I have offered only improvements to the layout and functionality.

Skipping to next step in Batch program

#echo off
setlocal EnableDelayedExpansion
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is !name2!^^! I'm an AI. I'm here to help with your lazy
Butt :D^^!
timeout /t 3 >null
echo!name2!: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >null
echo A. My name is
echo B. Express
set /p input=
if !input! equ B goto Writing2
cls
echo!name2!: Hello !name!, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
set /p input=!name!:
if !input! equ 1 goto Writing
if !input! equ Yes goto Writing
if !input! equ 2 exit
if !input! equ No exit
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route----->
goto Writing
Hello!
What I am trying to do here is skipping all the steps and go straight to "Writting2" if typed Express.
I am almost done with this fun program but I can't figure out a good way skip all the steps.
When I type "Express or select 'B'
it crashes.
but If I write a name the program works as usual!
Thanks in Advance!
Sorry in Advance if I did something wrong in the community.
This is a way you can do what you want:
#echo off
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is %name2% I'm an AI. I'm here to help with your lazy
::echo Butt :D^^!
timeout /t 3 >nul
echo %name2%: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >nul
echo A. My name is
echo B. Express
choice /c:AB>NUL
if errorlevel 2 goto Writing2
set /p "name=Enter your name: "
:Writing
cls
echo %name2%: Hello %name%, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
choice /c:1Y2N>NUL
if errorlevel 4 goto exit
if errorlevel 3 goto exit
if errorlevel 2 goto Writing
if errorlevel 1 goto Writing
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route-----^>
goto Writing
:exit
exit /b
Remember: Variables can be accessed by %variable_name% and you can set them by set "variable_name=variable_value" as #Compo mentioned above.
It is better to use choice /c option in your future batchfiles. A disadvantage of this option is that you cannot enter a string with more than 1 character, but it does it's own errorhandling, so you don't have to deal with invalid responses. Also, when you write
echo Anyway. Initiating EXPRESS Route----->
> symbol causes problems as it is a redirection character and should be escaped:
echo Anyway. Initiating EXPRESS Route-----^>

Batch Coding Menu Option to Go Back

I'm fairly new to coding, and I've been told to start with batch files. I'm attempting to make a text based game that prompts the user to choose between a few options to progress through the story. I've got the basic idea down, but I ran into some trouble with making a menu. I've done some research on here, and have searched other sites, but can't seem to make heads or tails of some of the answers to similar questions. Most of the answers write out code that I assume would work, but I need to know the WHY behind these answers, and I haven't really found any answers that I could understand.
TLDR; I need to make a menu for a text based game, that allows the user to return the label they left off on.
What I have so far:
:MainMenu
cls
echo Input Options:
echo.
echo ::1) View Storyline Changes
echo ::2) Restart Test
echo ::3) Resume Test
echo ::4) Help
echo ::5) Quit
echo.
set /p input=Input:
if "%input%" == "1" goto StorylineChanges
if "%input%" == "2" goto Rst
if "%input%" == "3" goto ResumeTest
if "%input%" == "4" goto HelpScrn
if "%input%" == "5" exit
:StorylineChanges
cls
echo Unimportant to test.
echo.
echo Input "m" to go back
echo.
set /p input=Input:
if "%input%" == "m" goto Main Menu
if "%input%" == " " goto WrongInputStorylineChanges
:Rst
cls
goto :StartTest
echo off
:ResumeTest
cls
REM ***THIS IS WHERE I NEED HELP ***
pause >nul
goto StartTest
As you can probably immediately tell, I BARELY have a grasp on basic commands, so please keep that in mind if you provide an answer. I'd really appreciate any help you guys could give me. Thanks so much.
I cannot unravel the spaghetti code, but I can give an example
detailing how to go back to the menu as the title asks.
#echo off
:MainMenu
cls
echo Input Options:
echo.
echo ::1) View Storyline Changes
echo ::2) Restart Test
echo ::3) Resume Test
echo ::4) Help
echo ::5) Quit
echo.
set /p input=Input:
if "%input%" == "1" call :StorylineChanges
if "%input%" == "2" call :Rst
if "%input%" == "3" call :ResumeTest
if "%input%" == "4" call :HelpScrn
if "%input%" == "5" exit /b 0
goto :MainMenu
:StorylineChanges
echo Use "goto :eof" to exit script or exit called label.
pause
goto :eof
:Rst
echo Use "exit /b" to exit script or exit called label with a errorlevel.
pause
exit /b 0
:ResumeTest
>&2 echo Exit called label with errorlevel 1. This line is to stderr.
pause
exit /b 1
:HelpScrn
echo Help. Exit with implicit 0.
pause
exit /b
The above uses call to access each label. Exit of a called label
returns back to the point of the call. The menu is in a loop so it keeps
showing until you input 5 to exit the script.
I added pauses into the labels so you could see the messages before clr
is called.

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.

Multiple choices menu on batch file?

Hi I want to make a batch file menu, that asks 'Select app you want to install?' for example
App1
App2
App3
App4
App5
ALL Apps
Select what app:_
What I want is, for example I want to install App2, App3, and App5, so I can type on by App ID's 'Select what app:2,3,5' . And when user Select option 6, it will install all Applications!
I know this is possible on bash scripting, but Im not sure on batch scripting?
An example of batch menu is http://mintywhite.com/software-reviews/productivity-software/create-multiple-choice-menu-batchfile/
Answer
This will do what you want. Let me know if you have any questions. All you have to do is follow the two steps listed in the script.
Script
:: Hide Command and Set Scope
#echo off
setlocal EnableExtensions
:: Customize Window
title My Menu
:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=One"
set "App[2]=Two"
set "App[3]=Three"
set "App[4]=Four"
set "App[5]=Five"
set "App[6]=All Apps"
:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo. Menu Title
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
call echo %x%. %%App[%x%]%%
goto MenuLoop
)
echo.
:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"
:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%
:: Process Input
call :Process %Input%
goto End
:Validate
set "Next=%2"
if not defined App[%1] (
set "Message=Invalid Input: %1"
goto Menu
)
if defined Next shift & goto Validate
goto :eof
:Process
set "Next=%2"
call set "App=%%App[%1]%%"
:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "One" echo Run Install for App One here
if "%App%" EQU "Two" echo Run Install for App Two here
if "%App%" EQU "Three" echo Run Install for App Three here
if "%App%" EQU "Four" echo Run Install for App Four here
if "%App%" EQU "Five" echo Run Install for App Five here
if "%App%" EQU "All Apps" (
echo Run Install for All Apps here
)
:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof
:End
endlocal
pause >nul
you may use choice.exe see here : http://ss64.com/nt/choice.html
You want to use set /p Example below:
echo What would you like to install?
echo 1 - App1
echo 2 - App2
set /p whatapp=
if %whatapp%==1 (
codetoinstallapp1
) else if %whatapp%==2 (
codetoinstallapp2
) else (
echo invalid choice
)
Here's a trick I learned:
echo.1) first choice
echo.2) second choice
echo.3) third choice
echo.4) fourth choice
:: the choice command
set pass=
choice /c 1234 /n /m "Choose a task"
set pass=%errorlevel%
::the choices
if errorlevel 1 set goto=1
if errorlevel 2 set goto=2
if errorlevel 3 set goto=3
if errorlevel 4 set goto=4
goto %goto%
While I use only 1-4 it would be very easy to add more possible choices.
#echo off
:menu
cls
echo.
echo Select the case color you want to create:
echo ==========================================
echo.
echo App 1
echo App 2
echo App 3
echo App 4
echo.
echo ==========================================
echo Please answer Y/N to the following:
set /p App1= Install App 1?
set /p App2= Install App 2?
set /p App3= Install App 3?
set /p App4= Install App 4?
if /I "%App1%" EQU "Y" goto :Option-1
if /I "%App1%" EQU "N" goto :1
:1
if /I "%App2%" EQU "Y" goto :Option-2
if /I "%App2%" EQU "N" goto :2
:2
if /I "%App3%" EQU "Y" goto :Option-3
if /I "%App3%" EQU "N" goto :3
:3
if /I "%App4%" EQU "Y" goto :Option-4
if /I "%App4%" EQU "N" goto :End
:Option-1
App 1 Loc.
goto 1
:Option-2
App 2 Loc.
goto 2
:Option-3
App 3 Loc.
goto 2
:Option-4
App 4 Loc.
:End
Exit
Menu with analog of checkbox.
#echo off
set size=3
::preset
set chbox2=x
:prepare
for /L %%i in (0,1,%size%) do (
if defined chbox%%i (
set st%%i=Y
) else (
set chbox%%i=
)
)
:menu
cls
echo.
echo 1. [%chbox1%] name_1:
echo.
echo 2. [%chbox2%] name_2:
echo.
echo 3. [%chbox3%] name_3:
echo.
echo.
echo.
choice /C 1234567890qa /N /M "Select [1-9] >> [a]pply or [q]uit:"
echo.
set inp=%errorlevel%
if %inp%==11 (
exit
)
if %inp%==12 (
call :apply
)
::switch
if defined st%inp% (
set st%inp%=
set chbox%inp%=
) else (
set st%inp%=Y
set chbox%inp%=X
)
goto :menu
:apply
for /L %%i in (0,1,%size%) do (
if defined st%%i (
call :st%%i
echo.
)
)
echo.
pause
goto :menu
:st1
echo First Command
goto :eof
:st2
echo Second Command
goto :eof
:st3
echo Third Command
goto :eof
You can set lines checked as defaults under :preset label.
A batch file is a list of command prompt commands. The following code prints to the terminal:
echo whateveryouwant
print your menu using these echo statements in a batch file.
Getting user input can be found here: How to read input from console in a batch file?
The installing of applications is a little more complex - you need to know the requirements of your apps and where files should be moved - that should be simple, as well; use move on the appropriate files in the appropriate place.
Here's an example of a batch script menu I'm using:
#echo off
setlocal
:begin
cls
echo [LOCAL ACCOUNTS REMOTE ADMIN] --------------------------------------
echo 1 -- List local accounts on a remote machine
echo 2 -- Create a local account on a remote machine
echo 3 -- Change a local account password on a remote machine
echo 4 -- Delete a local account on a remote machine
echo;
echo 5 -- exit
echo;
set /P rmFunc="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3 4 5 x) do if #%rmFunc%==#%%I goto run%%I
goto begin
:run1
rem list local accounts code
goto begin
:run2
rem create local account code
goto begin
rem and so on, until...
:run5
:run9
:run99
:runx
endlocal
goto :EOF
The most relevant bits are the set /p line and the for...in lines. The for...in line basically compares the choice entered with every menu item number, and if match, goto run#; otherwise start over from the beginning.
I saw that none of the above answers completely answered his/her question. One feature that they have left out is selecting all the software it installs in one go (so to speak).
So I made this off the top of my head (extremely sorry if there is something wrong with it, I'll edit it if there is).
#echo off & setlocal enabledelayedexpansion
echo What would you like to install?
::Put your options here, preferably numbered.
set /p op=Type the numbers of the software you want to install (separated by commas with no spaces. E.g: 1,3,2):
for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next
:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%
:next
::Put some more stuff here...
So that was a bit excessive. Feel free to change some things around and such.
What the code is doing is getting the user input (such as if you put in "1,3,4"), putting each number into its own variable, checking if that variable is empty, and if it isn't, sending you to code that does whatever the option was. It does this a few times until all the variables have been assessed.
This is a proposed analysis for improvement on David Ruhmann's code relating to the "Validate Input" section above:
Testing the menu for special characters works a charm except for the following characters "^&<". When each are submitted for input the program closes.
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:<=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Escaping the ^ and & works, but something very peculiar going on with the parsing of "<" and ">" (escaping these doesn't appear to work). If we reverse the order of the two statements as in the above amendment we find "<" works, but now ">" doesn't.
However, shifting the second statement with "<" down thus, both redirection characters work but now ")" doesn't!!
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
set "Input=%Input:<=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Another great tutorial for batch menus is found here.
I'm using this
#echo off
:a
echo Welcome to a casual log-in (you are a idiot)
echo.
pause
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p c=Email:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p u=Password:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
msg * Welcome %c%.
goto a
There is actually an extremely easy way to do this.
#echo off
echo Which app do you want to install?
echo [APP 1]
echo [APP 2]
echo [APP 3]
echo.
echo Type 1, 2, or 3.
set /p "AppInstaller=>"
if %AppInstaller%==1 goto 1
if %AppInstaller%==2 goto 2
if %AppInstaller%==3 goto 3
:1
[INSTALL CODE]
:2
[INSTALL CODE]
:3
[INSTALL CODE]
The menu, when coded like this, will look like this:
Which app do you want to install?
[APP 1]
[APP 2]
[APP 3]
Type 1, 2, or 3.
>_
The code sets the variable AppInstaller to 1, 2, or 3. The file determines this and redirects you to an installer for each one.
This is fairly easy code that I use a lot in multiple choice games:
#echo off
color 0a
cls
:download
echo App 1
echo App 2
echo App 3
echo App 4
echo App 5
echo All Apps
echo
echo Select What App (1, 2, 3, ect.):
set /p apps=
if %apps%==1 goto 1
if %apps%==1 goto 2
if %apps%==1 goto 3
if %apps%==1 goto 4
if %apps%==1 goto 5
if %apps%==1 goto all
:1
(Your Code Here)
:2
(Your Code Here)
:3
(Your Code Here)
:4
(Your Code Here)
:5
(Your Code Here)
:all
(Your Code Here)

Resources