Actually I'm a 1st almost 2nd grader lol but totally agree.... but compared to MD and other good scriptwriters I think I'm Pre-K
How to create a simple menu (Windows Console) Code: @echo off color 09 :MAINMENU CLS title "Application" by "nick/name" MODE CON COLS=76 LINES=22 echo. echo. Menu Title here (optional) echo. -------------------------------------------------------------- echo. echo. echo. A. Option echo. B. Option :CHOOSEACTION set /p userinp= ^> Select Option - (A-B): set userinp=%userinp:~0,1% if /i "%userinp%"=="A" goto action if /i "%userinp%"=="B" goto action echo. echo. Invalid Option echo. GOTO CHOOSEACTION :action execution code here @ echo off <------------is used to hide the command from being displayed on the console. color XX <------ is used to change color display of console (optional) :MAINMENU <-----this is where batch start displaying (menu) cls <------- is use to clear the hole screen from previous messages/text/code displayed title [title here]<------- set title in upper border of windows (console) MODE CON COLS=XX LINES=XX <----- set the size of windows in combination with "COLS=xx" (columns=size) "LINES=xx" (lines=size) echo. <------ blank line echo. [text here]<-----display message/text echo. A.Option <----- display text first option :CHOOSEACTION <------ here start the menu option/choices set /p userinp=^> text here - (A-D) <----- display user can choose options from A to D set userinp=%userinp:~0,1% <----this tell console to execute option selected by user if /i "%userinp%"=="A" goto<--- means if user select x option/number Upper or lower case equal (A or a) go to some menu or do some action echo. oops invalid option <-------display message invalid option if user select an option other than offered GOTO CHOOSEACTION <---- this will display the option to chose again if user select an option other than offered (go back to menu options)
@MasterDisaster Thanks for the echo and var demonstrations. Go for it with the Conditional Statements -- IF and Looping Statements -- FOR if your up to it Thanks for your time and effort doing that
im jst a beginner..quite new to writing a script..about echo command for example, i wrote echo hello babe and save it as .bat file..when i open it, the console just open and close automatically.How can i make the console stay until I close it ?
Ok so I am working on a task to run my "IORRT" task silent every 30 days like my KMS task runs silent here Code: schtasks /create /tn "Office" /tr '"C:\Users\timesurfer\Documents\My Dropbox\Public\KMS\Office Volume Activation Script for Z.W.T Keygen.exe"' /sc monthly /mo 6 /st 00:00:00 /ru "" /f Here is the one I'm using now for IORRT Code: schtasks /create /tn "IORRT" /tr %file% /sc daily /mo 30 /RL HIGHEST /f Maybe Code: schtasks /create /tn "IORRT" /tr '"C:\IORRT\IORRT.bat"' /sc daily /mo 30 /st 00:00:00 /ru "" /f
Could you continue with another basics demonstration please? Conditional Statements -- IF and Looping Statements -- FOR
This should become a Sticky if it get better, and a good place to learn Console Scripting "Batch"....
The IF command is used to process a batch of commands only if certain conditions are met. The IF command can be used in five ways. 1. IF [NOT] ERRORLEVEL number command ERRORLEVEL is a system variable that stores the exit code of the previous command. If the previous command did not encounter any errors then ERRORLEVEL would be 0, otherwise it would be 1. The ERRORLEVEL can have values from 0-255. If it is higher or has negative value, it will not produce the desired output. To access access errorlevels, it should be used as a variable %ERRORLEVEL%. The NOT keyword is optional and is used if you want to process batch commands when the IF condition is false. Code: IF ERRORLEVEL 0 echo This is a success message! IF ERRORLEVEL 1 echo You cannot see this message IF NOT ERRORLEVEL 1 echo You can see this message 2. IF [/I][NOT] string1 == string2 command The == operator is used to check if two strings are equal to each other. The /I switch is used to check case-insensitive strings Code: set product=Ultimate IF %product%==Ultimate echo Product is %product% IF NOT %product%==Professional echo Product is %product% IF /I %product%==ultimate echo Product is %product% IF /I NOT %product%==Professional echo Product is %product% 3. IF [NOT] EXIST filename command The EXIST keyword is used to check if a file is present in the specified location or not. This is commonly used to check for the existence of file before doing a file operation like copy, move, rename, delete or attribute changes. Code: set file="E:\Test" IF NOT EXIST %file% md %file% set file="E:\test\test.txt" IF NOT EXIST %file% echo This is a test file >%file% IF EXIST %file% notepad %file% 4. IF [/I] string1 compare-op string2 command The compare-op can take one of the following values EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal This is more flexible when comparing two numeric values. Code: IF 50 EQU 50 echo 50 is equal to 50 IF 50 NEQ 25 echo 50 is not equal to 25 IF 25 LSS 50 echo 25 is less than 50 IF 25 LEQ 50 echo 25 is less than or equal to 50 IF 50 GTR 25 echo 50 is greater than 25 IF 50 GEQ 25 echo 50 is greater than or equal to 25 5. IF [NOT] DEFINED variable command The Defined keyword is used to check if a variable is defined or not. Code: IF DEFINED var echo %var% IF NOT DEFINED var set var=This is a nice day echo %var% The ELSE keyword can be used with all the above constructs. The ELSE keyword should be on the same line as the end of IF command Code: IF EXIST %file% ( echo File is present ) ELSE ( echo File not found ) It is important that the ) and ELSE should be on the same line for the IF command to work properly.
I like this thread, very useful and interesting too. Ill also make the thread sticky. Keep up the good work.
Thanks I really thought this thread had the potential with some help from our resident script masters as a great place to have enthusiasm and some feed back as to how scripting works and a place to share any developing ideas or ask questions. Take care
Get the location of your script There is two ways to get the location of your batch file on the system where it is executing. %cd% %~dp0 The first one, %cd% is available directly at the command prompt, while the second is available only from within a script. Option number 2 is the preferred way of doing this (see Note). Example of usage Code: set workingDir=%cd% echo %workingDir% Code: set parentDir=%~dp0 echo %parentDir% NOTE Please note that %cd% will return the current working directory of the script. This means that if you have cd'd to another directory, this will be returned. %~dp0 will always return the path to the script.