1 / 37

Batch script file

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

astin
Download Presentation

Batch script file

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Batch script file Additional information

  2. Important DOS ConceptsBatch 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 • Controlling other applications in Batch File CTEC 110

  3. Important DOS CommandsBatch script file copy con source of copy is the console rem comment pause program stops until a key is hit echo displays back information to the console call passes control to another batch file choice offers user options for execution goto transfers control to different part of batch file if checks conditions of two or more variables shutdown powers down Windows taskmgr Brings up Task Manager CTEC 110

  4. Objectives • Create useful batch files • Define the role of batch files in the DOS environment • Identify different batch file commands and their use • Create batch files using the DOS COPY command • List those steps required to create a file from the CONSOLE (keyboard) • Understanding how those commands work • Control the execution of those commands • What else can we do with batch files? • Be able to reference the DOS Command Index: http://www.easydos.com/dosindex.html CTEC 110

  5. What is a Batch File? • Executable program • Sequence of commands • Time saver • Often used for automation at startup CTEC 110

  6. Simple Example CTEC 110

  7. Other Examples • Can be long and tedious!! • Take a look at GO.BAT • On the root of the N Drive CTEC 110

  8. CTEC 110

  9. CTEC 110

  10. CTEC 110

  11. Stopping a Runaway Batch File • Ctrl + C Stops a batch file • “Terminate batch job (Y/N)?” • Answer this question with a “Y” (Yes)… CTEC 110

  12. Creating a Batch File • COPY CON example.bat • CON 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 Windows CTEC 110

  13. Creating a Simple Batch File • Make the root of the O drive your default • Type in the following commands: • COPY CON o:\look.bat • dir • tree • Ctrl + Z • <enter> • TYPE look.bat • look.bat CTEC 110

  14. Editing a Batch File • notepad • FileOpen • Dropdown boxchoose “All Files” • Select look.bat • Add these commands at the end date /t time /t • Close and save • look.bat CTEC 110

  15. Editing a Batch File • REM used for comments • Comments are not acted upon • notepad look.bat • Add at the beginning of the file: REM This file was created by <Your Name> REM On <Today’s Date> • Close and save • look.bat CTEC 110

  16. Editing a Batch File • ECHO displays information back to the user • ECHO string of words • ECHO by itself displays a blank line • Usually 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 key CTEC 110

  17. Editing a Batch File • Go back to editing the look.bat file • Change all the REMs to ECHOs • Add PAUSE on the line between the last ECHO and before DIR • Run the batch program again • LOOK.BAT CTEC 110

  18. Editing a Batch File • Go back to editing the look.bat file • Add a PAUSE between DIR and Tree • Run the batch program again • LOOK.BAT CTEC 110

  19. Looking at a Real Batch File • Check out the WUGXP folder on the G drive for the batch file called GO.BAT • type WUGXP\go.bat CTEC 110

  20. Passing Parameters • BatchFileName 1stPar 2ndPar 3rdPar … • Parameters in file: %1 %2 %3 . . . • Create NAME.BAT on the root of O Drive • COPY CON name.bat ECHO Your first name is %1 ECHO Your last name is %2 Ctrl + Z • name.bat David Sims CTEC 110

  21. Passing Parameters • Create NAME.BAT on the root of O Drive • COPY CON times.bat %1 /t %2 /t Ctrl + Z • times.bat date time CTEC 110

  22. The ECHO command • The 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: • ECHO • ECHO is on. • ECHO This is a message! • This is a message CTEC 110

  23. The ECHO command • ECHO OFF - turns off the display of commands • COPY CON rems.bat REM This is a comment line ECHO OFF REM This comment line will not be seen CTRL + Z • rems.bat CTEC 110

  24. The ECHO command • Use notepad to adjust times.bat ECHO OFF Add %1 /t %2 /t • Save and close • times.bat date time CTEC 110

  25. The CALL command • The CALL command syntax: CALL [d:][path]batchfilename [options] • Calls another batch file, then returns to current batch file to continue • Example: • CALL go.bat CTEC 110

  26. The CALL command • Use notepad to adjust times.bat ECHO OFF %1 /t PAUSE Add CALL o:\look.bat Add %2 /t • Save and close • times.bat date time CTEC 110

  27. The CHOICE command • The 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 choice • ERRORLEVEL variable set to choice 1,2,3, etc • ERRORLEVEL is 0 on CTRL + C • ERRORLEVEL 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 y • CHOICE /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." CTEC 110

  28. 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 interactive • The /C switch followed by set of keys and a question mark • Default are Y and N keys • The /S switch keys must match the case of letters • The /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=NoneECHO You chose to format %DRIVE% • Format drive A:, B:, C:, D: or None? CTEC 110

  29. The GOTO command • The GOTO command syntax: GOTO (label) • Transfer control to a labeled line • The LabeledLine needs to have a “:” as the first character and no spaces • Example: echo off :START LabeledLine echo This is an infinite loop goto START CTEC 110

  30. The GOTO command • Create a batch file called infinite.bat ECHO OFF :START ECHO This is an infinite loop GOTO START • Save and Close infinite.bat • Infinite.bat (execute the batch file) CTEC 110

  31. The IF command • COPY CON SecretWord.bat ECHO off IF not linux==%1 GOTO MESSAGE ECHO you guessed the right word GOTO END :MESSAGE ECHO you made a bad guess :END • SecretWord.bat linux CTEC 110

  32. Calling Other Programs • Shutdown (Has a variety of options) • Display a Graphical User Interface • Logoff • Shutdown Computer • Shutdown and Restart Computer • Force Running Applications to close • Taskmgr(Starts up the Windows Task Manager) • Will start up the Task Manager • Good for troubleshooting Windows CTEC 110

  33. shutdown -l CTEC 110

  34. Calling Other Programs • Try to run some of the following commands on your own… • Some of these may take some time to complete • It is best to understand what they do before attempting to execute them!!! CTEC 110

  35. calc Calculator cleanmgr.exe Disk Cleanup compmgmt.msc Computer Management desk.cpl opens the display properties devmgmt.msc Device Manager diskmgmt.msc Disk Management eventvwr Windows Event Log (Event Viewer) explorer . Open explorer with the current folder selected. firewall.cpl Opens the Windows Firewall settings lusrmgr.msc Local Users and Groups Administrator mmsys.cpl Sound/Recording/Playback properties mstsc.exe Remote Desktop Connection perfmon Opens Reliability and Performance Monitor powercfg.cpl Power management control panel applet regedit Registry Editor CTEC 110

  36. runas Run specific tools and programs with different permissions than the user's current logon provides schtasks Enables an administrator to create, delete, query, change, run and end scheduled tasks on a local or remote system. secpol.msc Local Security Settings services.msc Services control panel systeminfo Displays a comprehensive information about the system tasklist.exe List Processes on local or a remote machine whoami /all Display Current User/Group/Privilege Information winver.exe Find Windows Version wscui.cpl Windows Security Center CTEC 110

  37. END of Additional Information • HOMEWORK • Lab 4 • DOS Quiz 4 CTEC 110

More Related