creating a batch file

5
Creating a Batch File Primary Software: Primary Software Version: 1.0 Primary Software Fixed Version: N/A Secondary Software: N/A Problem: I need to create a batch file so that I can run multiple installers during the installation of my LabVIEW executable. What are batch files used for, and what is the correct syntax to use when creating one? Solution: A batch file is a regular text file (*.txt) except that it has been given a *.bat extension. It can be created with any ASCII text editor such as Notepad or the DOS Editor. You simply type DOS commands into the file, one command per line. When you run the batch file, the series of DOS commands will be executed as though the commands were typed at a DOS prompt in a Command window. Basic Dos Commands Here is a List of Basic DOS Commands that you may want to use in your batch file: REM REM [comment] Records comments (remarks) in a batch file. COPY Copies one or more files to another location. COPY [/A | /B] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]] [/V] [/Y | /Y] source Specifies the file or files to be copied. /A Indicates an ASCII text file. /B Indicates a binary file. destination Specifies the directory and/or filename for the new file(s). /V Verifies that new files are written correctly. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. /Y Causes prompting to confirm you want to overwrite an existing destination file. The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /Y on the command line To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format). MOVE Moves files and renames files and directories. To move one or more files: MOVE [/Y | /Y] [drive:][path]filename1[,...] destination To rename a directory: MOVE [/Y | /Y] [drive:][path]dirname1 dirname2 [drive:][path]filename1 Specifies the location and name of the file or files you want to move. destination Specifies the new location of the file. Destination can consist of a drive letter and colon, a directory name, or a combination. If you are moving only one file, you can also include a filename if you want to rename the file when you move it. [drive:][path]dirname1 Specifies the directory you want to rename.

Upload: hierromovil

Post on 11-Dec-2015

12 views

Category:

Documents


5 download

DESCRIPTION

jj

TRANSCRIPT

Page 1: Creating a Batch File

Creating a Batch File

Primary Software: Primary Software Version: 1.0Primary Software Fixed Version: N/ASecondary Software: N/A

Problem: I need to create a batch file so that I can run multiple installers during the installation of my LabVIEW executable. What arebatch files used for, and what is the correct syntax to use when creating one?

Solution: A batch file is a regular text file (*.txt) except that it has been given a *.bat extension. It can be created with any ASCII texteditor such as Notepad or the DOS Editor.  You simply type DOS commands into the file, one command per line.  Whenyou run the batch file, the series of DOS commands will be executed as though the commands were typed at a DOSprompt in a Command window.   

Basic Dos CommandsHere is a List of Basic DOS Commands that you may want to use in your batch file: 

REMREM [comment]Records comments (remarks) in a batch file.   COPYCopies one or more files to another location. COPY [/A | /B] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination  [/A | /B]] [/V] [/Y | /­Y]   source       Specifies the file or files to be copied.  /A           Indicates an ASCII text file.  /B           Indicates a binary file.  destination  Specifies the directory and/or filename for the new file(s).  /V           Verifies that new files are written correctly.  /Y           Suppresses prompting to confirm you want to overwrite an               existing destination file.  /­Y          Causes prompting to confirm you want to overwrite an               existing destination file. The switch /Y may be preset in the COPYCMD environment variable.This may be overridden with /­Y on the command line To append files, specify a single file for destination, but multiple filesfor source (using wildcards or file1+file2+file3 format). MOVEMoves files and renames files and directories. To move one or more files:MOVE [/Y | /­Y] [drive:][path]filename1[,...] destination To rename a directory:MOVE [/Y | /­Y] [drive:][path]dirname1 dirname2   [drive:][path]filename1 Specifies the location and name of the file                          or files you want to move.  destination             Specifies the new location of the file. Destination                          can consist of a drive letter and colon, a directory                          name, or a combination. If you are moving only one                          file, you can also include a filename if you want                          to rename the file when you move it.  [drive:][path]dirname1  Specifies the directory you want to rename.

Page 2: Creating a Batch File

  dirname2                Specifies the new name of the directory.   /Y              Suppresses prompting to confirm creation of a directory                  or overwriting of the destination.  /­Y             Causes prompting to confirm creation of a directory or                  overwriting of the destination. The switch /Y may be present in the COPYCMD environment variable.This may be overridden with /­Y on the command line.The MOVE command moves a file to a new location.  DELDeletes one or more files. DEL [drive:][path]filename [/P]ERASE [drive:][path]filename [/P]   [drive:][path]filename  Specifies the file(s) to delete.  Specify multiple                          files by using wildcards.  /P            Prompts for confirmation before deleting each file.  CLSClears the screen. CLS PAUSESuspends processing of a batch program and displays the message:Press any key to continue.... PAUSE ECHODisplays messages, or turns command­echoing on or off.   ECHO [ON | OFF]  ECHO [message] Type ECHO without parameters to display the current echo setting. The @ symbol means don’t echo a line if echo is on. So, typing ECHO OFF prevents the user from watching thebatch program execute and typing: @echo off will keep the user from seeing the ECHO OFF message. It is common for batch programs to start with the @ECHOOFF command followed by CLS.  If you turn ECHO OFF in your batch program, be sure to put ECHO ON at the end ofthe program.  Otherwise other DOS programs may not show important messages.

 Launching an ExecutableTo launch an executable from a batch file, simply enter the path of the executable, just like you would do at a commandprompt.  If you use a relative path, it will be relative to the directory that the batch file was launched from. Using Command line Arguments:Command line arguments are parameters which follow the program to be launched at the command prompt.  Forinstance, lets say my batch file is called example.bat.  At a command prompt, I might type: Example.bat John Smith Here, “John” is the first command line argument and “Smith” is the second.  My Batch file then might appear as: @echo offclsecho My first name is %1echo My Last name is %2

Page 3: Creating a Batch File

pauseecho on The %1 and %2 act as place holders for the command line arguments.    Program Control Usually, commands in a batch file will execute sequentially.  However, it is sometimes desirable to carry out commandsin a different order.  DOS has several programming structures which are similar to BASIC programming formanipulating your batch file. GOTODirects Windows to a labelled line in a batch program. GOTO label   label   Specifies a text string used in the batch program as a label. You type a label on a line by itself, beginning with a colon.The GOTO command can be used to jump from one point in a program to another.  It can be used as a simplemethod for skipping forward or backwards in your program.  You need to place labels in your program to act asreference points.  For example: echo offREM print  COOL all over the screen:startecho COOLgoto startecho on “:start” is the label .  This program will type COOL over and over again in an infinite loop until you stop the programby pressing CTRL + C.    FOR...IN...DORuns a specified command for each file in a set of files. FOR %variable IN (set) DO command [command­parameters]   %variable  Specifies a replaceable parameter.  (set)      Specifies a set of one or more files.  Wildcards may be used.  command    Specifies the command to carry out for each file.  command­parameters             Specifies parameters or switches for the specified command. To use the FOR command in a batch program, specify %%variable instead of%variable. The FOR statement does not have to be used with files, it can also be used with strings. Example: REM command that prints out a list of namesFOR %%a IN (Andy Bob Carl Darren Eric) DO echo %%a  The loop will execute the echo command 5 times because there are 5 items in the argument list. Notice how you canuse the variable %%a as a substitute for each of the names. IFPerforms conditional processing in batch programs. IF [NOT] ERRORLEVEL number commandIF [NOT] string1==string2 commandIF [NOT] EXIST filename command   NOT               Specifies that Windows should carry out the command only                    if the condition is false.

Page 4: Creating a Batch File

  ERRORLEVEL number Specifies a true condition if the last program run returned                    an exit code equal to or greater than the number specified.  command           Specifies the command to carry out if the condition is                    met.  string1==string2  Specifies a true condition if the specified text strings                    match.  EXIST filename    Specifies a true condition if the specified filename                    exists.  The IF statement will execute the “command” if the condition is TRUE.  It can optionally be used with the NOTmodifier to reverse the condition.   Example: ECHO OFF  REM check whether a file called 'test.txt' exists IF EXIST test.txt GOTO :success IF NOT EXIST test.txt GOTO :error  :success ECHO file test.txt exists GOTO :end  :error ECHO Error ­ can't find the test.txt file GOTO :end   CHOICEWaits for the user to choose one of a set of choices. CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text] /C[:]choices Specifies allowable keys. Default is YN/N           Do not display choices and ? at end of prompt string./S           Treat choice keys as case sensitive./T[:]c,nn    Default choice to c after nn secondstext         Prompt string to display ERRORLEVEL is set to offset of key user presses in choices.  Example: CHOICE/C:yn Do you want to install MyTest %1 IF ERRORLEVEL 2 GOTO :no IF ERRORLEVEL 1 GOTO :yes  :yes setup.exe GOTO :end  :noecho operation cancelled  :end   You can use the CHOICE statement with as many possible choices as you want.  CHOICE/C:abcd choose a letter IF ERRORLEVEL 4 GOTO choice_d IF ERRORLEVEL 3 GOTO choice_c IF ERRORLEVEL 2 GOTO choice_b IF ERRORLEVEL 1 GOTO choice_a

 Much more can be done with DOS and batch files, but the information offered above should be sufficient for most basic

Page 5: Creating a Batch File

batch files.  If you would like more information about creating batch files, consult a DOS manual. For informationspecifically related to creating batch files for LabVIEW Installers please visit the KnowledgeBase articles below. Note: For help with any DOS command, type /? after the command at a DOS prompt.

Note: For more information on Batch Files, refer to the document Running Setup in Batch Mode

Related Links: Knowledgebase 149BHCQH: How Do I Make My LabVIEW­built Installer Run an Executable After Installation?KnowledgeBase 2A6ECPG4: How Can I Include Other Installers with the Installer for My LabVIEW Application?External Link: Running Setup in Batch Mode