Replaceable Parameters in Batch Files in Batch Files

Total Page:16

File Type:pdf, Size:1020Kb

Replaceable Parameters in Batch Files in Batch Files CS140M Fall 2014 Overview • Will learn to create batch files to automate a sequence of commands to accomplish various tasks. Intro to Batch Files • The use of batch file subcommands will be discussed and then used with batch commands. • How to halt the execution of a batch file will be explained. By Al Lake • Will write batch files using replaceable parameters. Fall 2014 LBCC • Learn how batch files can be used from the desktop. CS140M Fall 2014 CS140M Lake 2 Concepts of Batch and Concepts of Batch and Interactive Processing Interactive Processing Batch file: Batch file: • Must be an ASCII file • String together series of commands • Created with Edit, COPY CON, text editor, or executed sequentially with one command word processor which has “Save as text file” without human interaction or interruption option • Has .BAT or .CMD extension • Each line in batch file contains only one command Fall 2014 CS140M Lake 3 Fall 2014 CS140M Lake 4 ASCII Chart Concepts of Batch and Interactive Processing Why batch files are used: • Minimize keystrokes • Automate frequent/consistent procedures • Do not need to interact with computer while batch files are running Fall 2014 CS140M Lake 5 Fall 2014 CS140M Lake 6 Lake 1 CS140M Fall 2014 Concepts of Batch and Concepts of Batch and Interactive Interactive Processing Processing Batch processing: Interactive processing: • Automate frequent/consistent • Also called online or real time mode procedures • User interacts directly with computer • Batch files can be run at anytime • Information processed without delay • No user/computer interaction required Fall 2014 CS140M Lake 7 Fall 2014 CS140M Lake 8 How Batch Files Work How Batch Files Work •The operating system determines if a file is a data file or a program file by the file extension. •File extensions: – .BAT batch file – .DOC MS Word file – .XLS MS Excel file Fall 2014 CS140M Lake 9 Fall 2014 CS140M Lake 10 How Batch Files Work How Batch Files Work To locate file, operating system: If file not found, get error message: • Looks internally for command • Using Extension Priority Rules, “Filename is not recognized as an searches on default drive and directory internal or external command, • Searches search path as set in PATH operable program, or batch file.” statement following Priority Rules Fall 2014 CS140M Lake 11 Fall 2014 CS140M Lake 12 Lake 2 CS140M Fall 2014 How Batch Files Work Using an Editor to Write Batch Files • A batch file is a program. Word‐processing programs can create text files if they have a • Each line in a batch file must nondocument or text mode. contain only one command. Fall 2014 CS140M Lake 13 Fall 2014 CS140M Lake 14 Using an Editor to Write Batch Activity—Writing and Files Executing a Batch File KEY CONCEPTS: • Create batch files with any text editor or Notepad: word processor with ASCII output • Window’s text editor • Batch file extensions ‐ .BAT or .CMD • Execute the batch file the same ways as • Used to write batch files you do any command Fall 2014 CS140M Lake 15 Fall 2014 CS140M Lake 16 Writing and Executing a Batch Writing and Executing a Batch File to Save Keystrokes File to Save Keystrokes Batch files can save keystrokes. Sometimes easier to use COPY CON instead of EDIT to write simple batch file. Examples: DIR/AD Syntax: DIR/OS and DIR/O‐S COPY CON filename Fall 2014 CS140M Lake 17 Fall 2014 CS140M Lake 18 Lake 3 CS140M Fall 2014 Writing and Executing a Activity—Writing & Executing Batch File to Save Keystrokes a One‐Letter Batch File Drawbacks to using COPY CON: KEY CONCEPTS: • Cannot correct errors once • Pressing <F6> and <Enter> signals completion • <Ctrl> +Z same as pressing <F6> <Enter> is pressed • Displaying file with TYPE indicates it is ASCII file • Cannot correct errors in • Create one‐line batch files existing files • Directories have no size Fall 2014 CS140M Lake 19 Fall 2014 CS140M Lake 20 Activity ‐ Creating a Batch File to Alter Using Batch Files to Alter Your the Command Line Session Environment Command Line Environment Open command line session by: KEY CONCEPTS: • Created shortcut to open command line • Clicking icon on Start menu session on desktop • Clicking shortcut • Can decide what directory to be in • Can be altered to run in customized way • With /k parameter ‐ include batch file that is executed each time command line session opened • Value limited on floppy • Valuable on hard drive Fall 2014 CS140M Lake 21 Fall 2014 CS140M Lake 22 Creating Shortcuts for Creating Shortcuts for Batch Files on the Desktop Batch Files on the Desktop To run a batch file from Windows: Some things can be done with • Double‐click file name in shortcut that cannot do in the command line interface. Windows Explorer/My Computer • Click shortcut to execute batch • Create shortcut for file and place on desktop and click icon • Can change shortcut icon Fall 2014 CS140M Lake 23 Fall 2014 CS140M Lake 24 Lake 4 CS140M Fall 2014 Batch Files to Run Windows Activity—Creating a Programs Shortcut on the Desktop KEY CONCEPTS: •Where Windows system files • Clicking shortcut executes file reside will vary depending on • Can change icon so it is more installation of Windows XP distinctive Professional. Fall 2014 CS140M Lake 25 Fall 2014 CS140M Lake 26 Batch Files to Run Windows Activity—Creating a Batch Programs File to Run Notepad Windows “keeps notes” about itself in KEY CONCEPTS: system environment. • %SYSTEMROOT% (Environmental variable) o Windows knows where Windows files are located • Path stored in environmental variable and substitutes correct name %Path% o %% signs around name ‐ OS substitutes value for that variable. • Drive referred to in environment as • Opened Notepad without returning to %SystemDrive% desktop • Directory where system files are located •Used Notepad to create log file (case referred to as %SystemRoot% sensitive) •Created log file Fall 2014 CS140M Lake 27 Fall 2014 CS140M Lake 28 Activity—Creating a Batch File to Run Notepad Batch File Commands KEY CONCEPTS: • START command: o Can start a program in new window while still executing batch program in previous window o If title following command is in quotes, will give window that title Fall 2014 CS140M Lake 29 Fall 2014 CS140M Lake 30 Lake 5 CS140M Fall 2014 Batch File Commands The REM Command REM command (remarks) allows user to place comments in file that are displayed but not executed Fall 2014 CS140M Lake 31 Fall 2014 CS140M Lake 32 The REM Command Activity—Using REM KEY CONCEPTS: REM allows file to be documented: o Purpose of REM ‐ provide explanatory o Gives explanation of the purpose of a remarks about the batch file program o If line begins with REM ‐ explanatory text follows and no action taken o Can include name of batch file, last time it was updated, and author of o No more lines in batch file ‐OS returns to system level batch file Fall 2014 CS140M Lake 33 Fall 2014 CS140M Lake 34 The ECHO Command The ECHO Command ECHO ON command: ECHO OFF command: o Displays command and the o Minimizes screen clutter command’s output to the screen o User turns off display of command o Is the default value then only see command’s output Fall 2014 CS140M Lake 35 Fall 2014 CS140M Lake 36 Lake 6 CS140M Fall 2014 The ECHO Command Activity—Using ECHO KEY CONCEPTS: o ECHO ON ‐ see remarks and commands o ECHO OFF ‐ output displayed not actual commands o @ECHO OFF ‐ @ suppresses display of ECHO OFF o Purpose of batch files defeated if interaction required by user Fall 2014 CS140M Lake 37 Fall 2014 CS140M Lake 38 Activity—Using ECHO The CLS Command CLS command used to clear screen when it becomes full of no longer needed information. Fall 2014 CS140M Lake 39 Fall 2014 CS140M Lake 40 Activity ‐ Using CLS The PAUSE Command KEY CONCEPTS: PAUSE command instructs the o After ECHO turned off screen cleared batch file to stop executing until the user takes some action. Fall 2014 CS140M Lake 41 Fall 2014 CS140M Lake 42 Lake 7 CS140M Fall 2014 Activity—Using Pause Stopping a Batch File from Executing KEY CONCEPTS: To interrupt a batch file during o Batch file “paused” ‐ stopped execution: executing until a key is pressed ‐ action is taken o Press <Ctrl> + C o Press <Ctrl> + <Break> Fall 2014 CS140M Lake 43 Fall 2014 CS140M Lake 44 Stopping a Batch File Activity—Quitting a Batch from Executing File KEY CONCEPTS: o Results of Pressing <Ctrl> + C Return to system prompt o Because of speed of computer difficult to when batch file interrupted. ascertain how many lines of batch file read when <Ctrl> + C pressed Fall 2014 CS140M Lake 45 Fall 2014 CS140M Lake 46 Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files Can use fixed or variable Example of fixed parameter: parameters with batch files. o DIR A: /W Examples of variable parameter: o TYPE THIS.FIL o TYPE TEST.TXT Fall 2014 CS140M Lake 47 Fall 2014 CS140M Lake 48 Lake 8 CS140M Fall 2014 Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files Can parse batch commands: • Batch files can also use replaceable parameters. • Keying in additional information on command line • Replaceable parameters also called • Dividing computer language into dummy, substitute or positional parts that can be made useful to parameters. computer Fall 2014 CS140M Lake 49 Fall 2014 CS140M Lake 50 Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files User supplies markers (place holders) to let batch file know The place holder used in batch that variable will be keyed in files is the percent sign (%), with batch file name. followed by a number (0‐9).
Recommended publications
  • Windows BATCH Scripting Loops
    Windows BATCH Scripting_Loops Table of Contents Loops ............................................................................................................................................... 2 Loops Multiple File Example ........................................................................................................... 5 Loops Directory Example ................................................................................................................ 8 Loops Multiple Lines in a File Example ........................................................................................... 9 Loops Running a Command Multiple Times ................................................................................. 10 For Loops ....................................................................................................................................... 11 Tokenizing Loop Example -1 ........................................................................................................ 13 Tokenizing Loop Example -2 ........................................................................................................ 16 Notices .......................................................................................................................................... 17 Page 1 of 17 Loops Loops A core component of scripts – allow one thing many times Windows batch scripts use “FOR – DO” and has 4 different constructs depending on which switches are used with “FOR” • Multiple Files – perform one command on each file FOR %a
    [Show full text]
  • Chapter 10 Introduction to Batch Files
    Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files Chapter 10 Introduction to Batch Files LEARNING OBJECTIVES 1. Compare and contrast batch and interactive processing. 2. Explain how batch files work. 3. Explain the purpose and function of the REM, ECHO, and PAUSE commands. 4. Explain how to stop or interrupt the batch file process. 5. Explain the function and use of replaceable parameters in batch files. 6. Explain the function of pipes, filters, and redirection in batch files. STUDENT OUTCOMES 1. Use Edit to write batch files. 2. Use COPY CON to write batch files. 3. Write and execute a simple batch file. 4. Write a batch file to load an application program. 5. Use the REM, PAUSE, and ECHO commands in batch files. 6. Terminate a batch file while it is executing. 7. Write batch files using replaceable parameters. 8. Write a batch file using pipes, filters, and redirection. CHAPTER SUMMARY 1. Batch processing means running a series of instructions without interruption. 2. Interactive processing allows the user to interface directly with the computer and update records immediately. 3. Batch files allow a user to put together a string of commands and execute them with one command. 4. Batch files must have the .BAT or .CMD file extension. 5. Windows looks first internally for a command, then for a .COM files extension, then for a .EXE file extension, and finally for a .BAT or .CMD file extension. 6. Edit is a full-screen text editor used to write batch files. 7. A word processor, if it has a means to save files in ASCII, can be used to write batch files.
    [Show full text]
  • Learning Objectives ECHO Commands. Command. 10. Explain
    . SA Learning Objectives After completing this chapter you will be able to: 1. List commands used in batch files. 2. List and explain batch file rules. 3. Use a batch file with a shortcut. 3. Explore the function of the REM, 4. Use the SHIFT command to move param- ECHO commands. eters. 4. Explain the use of batch files with shortcuts. 5. Use the IF command with strings for condi- 5. Explain the purpose and function of the tional processing. GOTO command. 6. Test for null values in a batch file. 6. Explain the purpose and function of the 7. Use the IF EXIST /IF SHIFT command. test for the existence of a file or a 7. Explain the purpose and function of the IF subdirectory. command. 8. Use the SET command. 8. Explain the purpose and function of the IF 9. Use the environment and environmental EXIST /IF variables in batch files. 9. Explain the purpose and function of the IF 10. Use the IF ERRORLEVEL command ERRORLEVEL command. XCOpy to write a batch file for testing exit 10. Explain the purpose and function of writing codes. programs. 11. Use the FOR...IN...OO command for repeti- 11. Explain the purpose and function of the tive processing. environment and environmental variables. 12. Use the CALL command in a batch file. 12. Explain the use of the SET command. 13. Explain the purpose and function of the Chapter Overview FOR...IN...OO command. You learned in Chapter 10 how to write simple 14. Explain the purpose and function of the batch files and use replaceable parameters.
    [Show full text]
  • Starting a Batch File Example
    LA Solutions ShellBatch.doc Starting a Batch File from MicroStation There have been a couple of questions in the MicroStation newsgroups along the lines of “How do I start an operating system batch file without leaving MicroStation?” One answer to the question is to use Visual Basic for Applications (VBA), which is part and parcel of MicroStation V8. VBA provides extensive functionality, which you can extend by referencing built-in Visual Basic (VB) methods, and even by calling the Win32 native application programming interface (API) directly. VB provides a Shell() method, which means ‘attempt to start whatever executable program I give you’. A batch file (having a .bat extension) is not an executable file. The way to ‘execute’a batch file is to give its path to the Windows built-in command executable cmd.exe. The way to persuade the operating system to do something with a batch file is to put these ideas together like this… Shell(“cmd.exe /k batchfile.bat”) Unfortunately, things aren’t always as simple as we would like, because Shell() can’t itself divine the location of the executable file. The VBA project ShellBatch.mvba takes care of these details, and all you have to know is how to start it. 1. Copy ShellBatch.mvba to one of the folders in your computer’s VBA search path (MS_VBASEARCHDIRECTORIES). C:\Program Files\Bentley\Workspace\Standards\VBA is a good choice 2. Open MicroStation’s key-in window from the Utilities menu 3. Start your batch file with the following key-in: you must know the full path to the file: vba run [ShellBatch].Module1.Main <batch file path> For example: vba run [ShellBatch]Module1.Main D:\temp\test.bat Copyright © LA Solutions 2003…2005 visit http://www.la-solutions.co.uk.
    [Show full text]
  • Windows Command Line?
    Table of contents 1. Why an ebook on the Windows Command Line? 2. Make an example directory 3. A little exercise: open the Windows Command Prompt window and go to the example directory 3.1 The prompt 3.2 Moving into a (sub)directory 4. Pattern-matching 5. Command ‘DIR’ and Glob patterns 6. The ‘COPY’ command and Glob patterns 6.1 Copy files from the current directory into a subdirectory 6.2 Copy files from the current directory into a subdirectory in binary mode 6.3 Combine ASCII-files and put the result into a subdirectory 6.4 Combine binary files and put the result into a subdirectory 6.5 Are the files copied correctly? 6.6 Copy a selection of files with the ‘FOR’ loop 7. The ‘DEL’ command and Glob patterns 7.1 Delete files from the current directory 7.2 Delete files from the subdirectory ‘my Doc’ -1 7.3 Delete files from the subdirectory ‘my Doc’ -2 7.3.1 An alternative 7.3.2 ROBOCOPY 8. Passing multiple commands 9. The ‘REN’ or ‘RENAME’ command 9.1 Change subdirectory name 9.2 Change file extensions 9.3 Modify filenames from the current directory: basic examples 9.4 Truncate a filename by using ‘?’ 9.5 Modify filenames in the subdirectory ‘my Doc’: basic example 10. More complex replacements 10.1 Add a prefix to filenames with the same characters at the beginning 10.2 Add a prefix to filenames with the same extensions 10.3 Add a suffix at the end of filenames with the same extensions 10.4 Substitute a character in a specific position 11.
    [Show full text]
  • Altair Monarch 2021 Command Line User Guide 1
    Altair® Monarch® v2021 COMMAND LINE USER GUIDE TABLE OF CONTENTS [1] Overview ................................................................................................................................................... 1 [2] Classic Command Line Parameters ....................................................................................................... 2 Input Options ................................................................................................................................................... 2 Input Database Options .................................................................................................................................. 2 Input Report Options ....................................................................................................................................... 3 View Options ................................................................................................................................................... 3 Export Options ................................................................................................................................................ 3 Command Line Examples ............................................................................................................................... 4 [2] Launching Quick Help ............................................................................................................................. 6 [3] Using Classic Command Lines to Export Data ....................................................................................
    [Show full text]
  • Windows Task Scheduler Run Batch File
    Windows Task Scheduler Run Batch File Ramsay is subsacral: she Platonises bestially and snool her nighty. Transcendentalism Bronson sometimes mollycoddling his pardonableness unscrupulously and transshipping so agitato! Unassayed Julius always dialyzing his hasp if Pooh is salable or shivers underhand. Quickly export your stats to Excel, receive reminders for coming meetings and easily contact your clients. Please enter at a batch file, run batch file extension? If you tell us your reasons we may work something out. Instructs Foxtrot RPA to enter silent mode. How to update a snapshot in Azure? Below is additional information about how to do this. Get insights from your form responses with our Export To Excel feature. Hence kind of need the browser to be present on desktop. Powershell seems to have a special power to identify a specific instance of a program you just started. Besides the post any required, and exe and allows you can i look for your form each start data analyzing tools that ten minutes ahead of run windows task batch file. This will ensure the account has enough permissions to access the remote computers. Just follow the steps shown below and you will be good. Consider when i run windows task scheduler run batch file. Security options is set to Run whether user is logged on or not. Reddit on an old browser. The rest will be done using arguments. How to Automatically Elevate a Batch file to Run it as Administrator? More Things to Try. When connectivity is restored the desktop will become immediately available. The Action tab lets you choose what the task should do on the trigger.
    [Show full text]
  • Lab 13: Introduction to Scripting: NEXUS Files, Batch Files, Mesquite, R, Python
    Integrative Biology 200A University of California, Berkeley Systematics Spring 2010 By Nick Matzke Lab 13: Introduction to Scripting: NEXUS files, batch files, Mesquite, R, Python In today’s lab, we will learn about scripting. A “script” is a simple program that basically assembles a bunch of commands into a list and then executes them in series. While everyone likes window-based programs for figuring out how to do an analysis the first time, what if you have to do the same analysis a dozen times, or hundreds of times with slightly differing parameters? What if you only do the analysis once a year, and have trouble remembering the sequence of commands? Scripting can be a huge advantage for a scientist in these situations. A word about programming: if you’re not going to take a bunch of computer science classes, the best way to learn programming is to have someone introduce you to the basics and get you over the initial “hump.” The hardest part of learning a program from scratch is figuring out how to do the most basic things. Books can be useful, but tend to be very general and may not help you with your specific task. An introductory online example can be very useful. In general, the best strategy is to get something minimal working, and go from there. In this lab, we will attempt to get one simple task working for each of these methods of scripting: NEXUS files, batch files, Mesquite, R, and Python. R is a free, general statistics package which you will learn about more in IB200b.
    [Show full text]
  • Declare Bat File in Bat File
    Declare Bat File In Bat File Marital and inconvenient Nevin divorce her nonbelievers semipermeability barbecues and runabout vitalistically. Fun and eczematous Patrice often predestined some besetment deafeningly or hire phosphorescently. When Rhett booms his suer wakes not cumulatively enough, is Hirsch climatical? Let us know how do it is the above command in bat file gets it will duplicate files from inside excel from the I have each batch file that copies one file and places it in another location as click text file The do step flow to marvel the text file with peculiar but again cannot exit out. Can you help please for calling a exe or bat file by the code of vb60 Thanks. Winzip Batch File. Scripts can use env variables Support Forum WinSCP. EXEC masterxpCMDShell 'cfindwordbat' ii Running parameterized batch file DECLARE PassedVariable VARCHAR100 DECLARE. Variables in Batch Scripting Language o7planning. A batch file can almost run usually double clicking it in Windows explorer or by typing the. You can batch them as constants at it beginning for readability. Zip file rename it, i am creating new. Wanna join a bat scripts in running any moment there with. After making few hours of trying but still didn't get very straight bat script for windows 7 x64 to. Using Batch Scripts and SQLCMD to Write Out my Database's. Use double percent signs to carry out naked for command within this batch file Variables are gentle sensitive side they watch be represented with an alphabetical value given as a b or c Required Specifies one been more files directories or text strings or a forehead of values on award to almost the command.
    [Show full text]
  • Batch File Programming by Ankit Fadia ______
    Batch File Programming By Ankit Fadia __________________________________________________________________ Batch file programming is nothing but the Windows version of Unix Shell Programming. Let's start by understanding what happens when we give a DOS command. DOS is basically a file called command.com It is this file (command.com) which handles all DOS commands that you give at the DOS prompt---such as COPY, DIR, DEL etc. These commands are built in with the Command.com file. (Such commands which are built in are called internal commands.).DOS has something called external commands too such as FORMAT, UNDELETE, BACKUP etc. So whenever we give a DOS command either internal or external, command.com either straightaway executes the command (Internal Commands) or calls an external separate program which executes the command for it and returns the result (External Commands.) So why do I need Batch File Programs? Say you need to execute a set of commands over and over again to perform a routine task like Backing up Important Files,Deleting temporary files(*.tmp, .bak , ~.* etc) then it is very difficult to type the same set of commands over and over again. To perform a bulk set of same commands over and over again, Batch files are used. Batch Files are to DOS what Macros are to Microsoft Office and are used to perform an automated predefined set of tasks over and over again. So how do I create batch files? To start enjoying using Batch files, you need to learn to create Batch files. Batch files are basically plain text files containing DOS commands.
    [Show full text]
  • Introduction What Is a Batch Processing File?
    Introduction Using LabTalk, Origin's built-in programming language, you can create a batch processing file to automate your application and thus speed up your work. This article: · defines the term "batch processing file", · shows you how simple it is to create and run one, and · shows you how to integrate it into Origin's graphical user interface (GUI). What Is a Batch Processing File? A batch processing file is a form of Origin script file that acquires and processes multiple data files (most often without user intervention) and reports the results to the user. Batch processing files are created, ran, and integrated into Origin's GUI in exactly the same way as all Origin script files. In order to learn more about them, you should first learn more about Origin script files as a whole =>. What is an Origin script file? How is it used? An Origin script file is an ASCII text file which consists of a series of one or more LabTalk statements, often divided into sections. When called, the LabTalk statements contained in an Origin script file (or section of it) are sent to Origin's LabTalk interpreter, whereupon a series of actions are automatically carried out in Origin. An Origin script file can be run from the Script Window, from a menu option, or from a toolbar button. In fact, many of the menu commands and toolbar buttons in Origin execute Origin script files (or sections of them) 'behind the scenes.' Viewing these Origin script files that are executed behind the scenes is a good way to learn more about LabTalk programming and syntax.
    [Show full text]
  • Batch File Commands Copy Files with Modification Date
    Batch File Commands Copy Files With Modification Date Absent-minded Jeth garners, his codon blinker spooks unwontedly. Which Berkeley slushes so exothermallymaliciously that and Jermain induces giddies Fridays. her actin? Obtrusive Wilek speans that subshrub docketing Instead of the same set intersection: pop up will not support needed afterwards, files with batch file commands and changes directories and how Sn SplitCreateObjectwscriptshellexeccmd c dir GOFcsv b s. Copy all files and sub-directories recursive from the career directory install the. It with batch commands you copy command interpreters invoked from copied to immediately view the dates? Syncing Files Using The Windows Command Line code. Whether a file commands with batch date? XCopy via the CMD won't maybe because already have apartment type out dirs out manually if you don't follow CMD DOS window command line interface. This Instruction Article explains how to setup a Windows batch file to delete files. Copy metadata Date Modified to Date Acquired. Or date to. Copy the file in restartable mode. This wrong with your message if a batch? This is was common way that set protectins. It with batch commands interactively examine file dates when locating your command used with options in quotes are defined or a csv or offline, in or ascii. Places as file commands, and files from command? Using DOS commands or zipping files is her easy to do terminate for your. Begin a subroutine declaration. Click custom End Session. It with copying command is copied again instead of commands are you want to a folder using xcopy copies. How cute I automate a said of files with WSFTP Pro where.
    [Show full text]