[Batch] The Official Batch File Repository - Learn & ask questions about batch files

Discussion in 'Scripting' started by timesurfer, Oct 11, 2010.

  1. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
  2. DARKOR04

    DARKOR04 MDL Tester/Developer

    Jul 5, 2010
    497
    909
    10
    #23 DARKOR04, Oct 13, 2010
    Last edited: Oct 13, 2010
    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
     
  3. DARKOR04

    DARKOR04 MDL Tester/Developer

    Jul 5, 2010
    497
    909
    10
    #24 DARKOR04, Oct 13, 2010
    Last edited by a moderator: Apr 20, 2017
    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)
     
  4. pkaji123

    pkaji123 MDL Addicted

    Aug 22, 2009
    763
    234
    30
    Great thread,I would start learning this..
     
  5. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    @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 :D
     
  6. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    #27 gamepoint, Oct 13, 2010
    Last edited by a moderator: Apr 20, 2017
    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 ?
     
  7. Butterfly_Joe

    Butterfly_Joe MDL Novice

    Feb 5, 2010
    39
    28
    0
    @echo off
    echo hello babe
    pause
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #29 timesurfer, Oct 13, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Code:
    @echo off
    echo hello babe
    pause
    
    You won that one Butterfly_Joe :p...lol
     
  9. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    thanx man..is it a must to put @echo off in the starting line?
     
  10. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    Play with to see like try

    @Echo with no off
     
  11. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    i see!.......if there is no "off" ..other commands in the .bat file wwill be displayed isn't it??
     
  12. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #33 timesurfer, Oct 14, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    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
     
  13. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    Could you continue with another basics demonstration please?

    Conditional Statements -- IF

    and

    Looping Statements -- FOR

     
  14. DARKOR04

    DARKOR04 MDL Tester/Developer

    Jul 5, 2010
    497
    909
    10
    This should become a Sticky if it get better, and a good place to learn Console Scripting "Batch"....;)
     
  15. Tito

    Tito Super Mod / Adviser
    Staff Member

    Nov 30, 2009
    18,682
    18,581
    340
    Thats right...
     
  16. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #37 MasterDisaster, Oct 17, 2010
    Last edited by a moderator: Apr 20, 2017
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. alextheg

    alextheg MDL Expert

    Jan 7, 2009
    1,776
    812
    60
    #38 alextheg, Oct 17, 2010
    Last edited: Oct 17, 2010
    I like this thread, very useful and interesting too.
    Ill also make the thread sticky. Keep up the good work. :D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    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 :)
     
  19. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #40 Calistoga, Oct 17, 2010
    Last edited by a moderator: Apr 20, 2017
    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.

    1. %cd%
    2. %~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.