batch script file

Download Batch script file

Post on 21-Jan-2016

82 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Batch script file. Additional information. Important DOS Concepts Batch script file. Objectives… What is a Batch File? Stopping a Runaway Batch File Creating a Batch File Editing a Batch File Passing Parameters to a Batch File Commonly used Batch File Commands - PowerPoint PPT Presentation

TRANSCRIPT

  • Batch script file Additional information

  • ObjectivesWhat is a Batch File?Stopping a Runaway Batch FileCreating a Batch FileEditing a Batch FilePassing Parameters to a Batch FileCommonly used Batch File CommandsControlling other applications in Batch FileChapter 5 adds (37 slides)CTEC 110*Important DOS ConceptsBatch script file

    CTEC 110

  • Important DOS CommandsBatch script filecopy consource of copy is the consoleremcommentpauseprogram stops until a key is hitechodisplays back information to the consolecallpasses control to another batch filechoiceoffers user options for executiongototransfers control to different part of batch fileifchecks conditions of two or more variablesshutdownpowers down WindowstaskmgrBrings up Task ManagerChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • ObjectivesCreate useful batch filesDefine the role of batch files in the DOS environmentIdentify different batch file commands and their useCreate batch files using the DOS COPY command List those steps required to create a file from the CONSOLE (keyboard)Understanding how those commands workControl the execution of those commandsWhat else can we do with batch files?Be able to reference the DOS Command Index:http://www.easydos.com/dosindex.html

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • What is a Batch File?Executable programSequence of commandsTime saverOften used for automation at startupChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Simple ExampleChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Other ExamplesCan be long and tedious!!Take a look at GO.BATOn the root of the N DriveChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Stopping a Runaway Batch FileCtrl + C Stops a batch fileTerminate batch job (Y/N)?Answer this question with a Y (Yes)

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Creating a Batch FileCOPY CON example.batCON stands for console (the keyboard) whereby the commands entered into keyboard will automatically be put into the file example.bat

    Ctrl + Z places an end-of-file character in the file and stores it on disk.Can also use notepad in WindowsChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Creating a Simple Batch FileMake the root of the O drive your defaultType in the following commands:COPY CON o:\look.batdirtreeCtrl + Z

    TYPE look.batlook.batChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Editing a Batch FilenotepadFileOpenDropdown boxchoose All FilesSelect look.batAdd these commands at the enddate /ttime /tClose and savelook.bat

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Editing a Batch FileREM used for comments Comments are not acted uponnotepad look.batAdd at the beginning of the file:REM This file was created by REM On Close and savelook.batChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Editing a Batch FileECHO displays information back to the userECHO string of wordsECHO by itself displays a blank lineUsually displays the ECHO setting (ON or OFF)Try using a period (.) instead (put a space before)

    PAUSE is for stopping the program until the user hits a keyChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Editing a Batch FileGo back to editing the look.bat fileChange all the REMs to ECHOsAdd PAUSE on the line between the last ECHO and before DIR

    Run the batch program againLOOK.BATChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Editing a Batch FileGo back to editing the look.bat fileAdd a PAUSE between DIR and Tree

    Run the batch program againLOOK.BATChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Looking at a Real Batch FileCheck out the WUGXP folder on the G drive for the batch file called GO.BATtype WUGXP\go.bat

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Passing ParametersBatchFileName 1stPar 2ndPar 3rdPar Parameters in file: %1 %2 %3 . . .Create NAME.BAT on the root of O DriveCOPY CON name.batECHO Your first name is %1ECHO Your last name is %2Ctrl + Zname.bat David SimsChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Passing ParametersCreate NAME.BAT on the root of O DriveCOPY CON times.bat%1 /t%2 /tCtrl + Ztimes.bat date timeChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The ECHO commandThe ECHO command syntax:ECHO [ON | OFF]ECHO [message]

    Displays messages, or turns command-echoing on or off.Type ECHO without parameters to display the current echo setting

    Examples:ECHOECHO is on.ECHO This is a message!This is a message

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The ECHO commandECHO OFF - turns off the display of commandsCOPY CON rems.batREM This is a comment lineECHO OFFREM This comment line will not be seenCTRL + Zrems.bat

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The ECHO commandUse notepad to adjust times.batECHO OFFAdd%1 /t%2 /tSave and close

    times.bat date time

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The CALL commandThe CALL command syntax:CALL [d:][path]batchfilename [options]

    Calls another batch file, then returns to current batch file to continue

    Example:CALL go.bat

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The CALL commandUse notepad to adjust times.batECHO OFF%1 /tPAUSEAddCALL o:\look.batAdd%2 /tSave and closetimes.bat date time

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The CHOICE commandThe CHOICE command syntax:CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

    This tool allows users to select one item from a list of choices and returns the index of the selected choiceERRORLEVEL variable set to choice 1,2,3, etcERRORLEVEL is 0 on CTRL + CERRORLEVEL is 255 when invalid choice is made

    EXAMPLES:CHOICE /?CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."CHOICE /T 10 /C ync /CS /D yCHOICE /C ab /M "Select a for option 1 and b for option 2."CHOICE /C ab /N /M "Select a for option 1 and b for option 2."Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The CHOICE command(Second look)The CHOICE command syntax (second look):CHOICE [/C:[:]list of keys] [/N] [/S] [/T[:]choice,number] [text]

    CHOICE is used to make batch files interactiveThe /C switch followed by set of keys and a question markDefault are Y and N keysThe /S switch keys must match the case of lettersThe /T switch is the number of seconds a user has to press keys

    Examples:CHOICE /C:ABCDN /N /T:N,10 Format drive A:, B:, C:, D: or None? IF ERRORLEVEL 1 SET DRIVE=drive A: IF ERRORLEVEL 2 SET DRIVE=drive B: IF ERRORLEVEL 3 SET DRIVE=drive C: IF ERRORLEVEL 4 SET DRIVE=drive D: IF ERRORLEVEL 5 SET DRIVE=None ECHO You chose to format %DRIVE% Format drive A:, B:, C:, D: or None?Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The GOTO commandThe GOTO command syntax:GOTO (label)

    Transfer control to a labeled lineThe LabeledLine needs to have a : as the first character and no spaces

    Example:echo off:STARTLabeledLineecho This is an infinite loop goto START

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The GOTO commandCreate a batch file called infinite.batECHO OFF:STARTECHO This is an infinite loopGOTO STARTSave and Close infinite.batInfinite.bat (execute the batch file)Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • The IF commandCOPY CON SecretWord.batECHO offIF not linux==%1 GOTO MESSAGEECHO you guessed the right wordGOTO END:MESSAGEECHO you made a bad guess:ENDSecretWord.bat linuxChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Calling Other ProgramsShutdown (Has a variety of options)Display a Graphical User InterfaceLogoffShutdown ComputerShutdown and Restart ComputerForce Running Applications to close

    Taskmgr (Starts up the Windows Task Manager)Will start up the Task ManagerGood for troubleshooting Windows

    Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • shutdown -lChapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Calling Other ProgramsTry to run some of the following commands on your ownSome of these may take some time to completeIt is best to understand what they do before attempting to execute them!!!Chapter 5 adds (37 slides)CTEC 110*

    CTEC 110

  • Chapter 5 adds (37 slides)CTEC 110*calc Calculatorcleanmgr.exe Disk Cleanupcompmgmt.msc Computer Managementdesk.cpl opens the display propertiesdevmgmt.msc Device Managerdiskmgmt.msc Disk Managementeventvwr Windows Event Log (Event Viewer)explorer . Open explorer with the current folder selected.firewall.cpl Opens the Windows Firewall settingslusrmgr.msc Local Users and Groups Administratormmsys.cpl Sound/Recording/Playback propertiesmstsc.exe Remote Desktop Connectionperfmon Opens Reliability and Performance Monitorpowercfg.cpl Power management control panel appletregedit Registry Editor

    CTEC 110

  • Chapter 5 adds (37 slides)CTEC 110*runas Run specific tools and programs with different permissions than the user's current logon providesschtasks Enables an administrator to create, delete, query, change, run and end scheduled tasks on a local or remote system.secpol.msc Local Security Settingsservices.msc Services control panelsysteminfo Displays a comprehensive information about the systemtasklist.exe List Processes on local or a remote machinewhoami /all Display Current User/Group/Privilege Informationwinver.exe Find Windows Versionwscui.cpl Windows Security Center

    C

Recommended

View more >