Batch Guide. Batch Guideby Terry Newton. This is an attempt at explaining the MSDOS batch language. It is not. complete, just the basics. For more information on individual commands.
Hi Jon, I'm not sure how you can schedule a task without knowing the path to the batch file… but in case you're looking at simply picking up the drive letter for a batch file launched interactively by a user, the. Processing Lists of Data. A nagging problem in batch programming is how to take a list of items in a file and do anything useful with it. One solution is to use Ed Schwartz' @.COM program or a similar external utility, but.
DOS's built- in HELP command. Some familiarity of DOS is assumed. CD, MD, DEL, RENAME etc. Table of Contents. All of the examples assume English DOS 6, some may run under DOS 5 but. DOS 6 adds features which are very useful to batch programmers.
I quoted the help text from the actual IF command built in to CMD.EXE, which had those dots. I don't know why they included them, it does seem inconsistent. Of course, what the EXIST keyword actually needs is a valid file name. Cd 'C:\Program Files (x86)\Wunderlist2' start Wunderlist.exe. exit. Above is an example of the complete batch file script. Below is a breakdown of what is going on in the script. @echo off This simply prevents commands from.
A batch file is a kind of script file in DOS, OS/2 and Windows. It consists of a series of commands to be executed by the command line interpreter, stored in a plain text file. A batch file may contain any command the. How do I read input from the console in a batch file? What I am trying to achieve is the functionality of scanf in C. How can I do the same in a batch file? Batch Files: Examples (All) Click the floppy disk or icons to download the ZIPped sources. Click the question mark icons to view the MD5 and SHA1 checksums for the ZIPped sources. Batch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate. Since. A while ago in one of my older posts I included a little gem that I think deserves it’s own dedicated post; calling PowerShell scripts from a batch file. Why call my PowerShell script from a batch file? When I am writing a. Links: Windows Environment Variables at ss64.com; Command shell overview at Microsoft; Quoting and escaping. You can prevent the special characters that control command syntax from having their special meanings as follows.
CHOICE and a version of FIND that returns an errorlevel. Elements of the Batch Programming Language. The best way to learn batch is to experiment while referring to the. HELP command, it explains everything. Batch programs are essentially sequences.
DOS commands that 'type themselves' when the batch file is run. Batch files consist of control elements, internal DOS commands and external. With a little ingenuity one can duplicate most of. For the types of things. Variables. Batch uses the percentage sign (%) to mark variables. The set command. is used to assign and clear variables.
When DOS reads the batch file, strings. Variable names have eight significant characters (I think) and are always. Windows). [Testing under Win. Dos 6.] In addition. The shift command moves everything down.
The following batch illustrates the use of variables.. Hello. set var. 2=World. Explanation - the first line prevents the commands from being displayed.
The. set command is used to set two variables to "Hello" and "World". Next, echo is used to display the two variables to the screen. Hello World! display. Finally. the two variables are cleared to keep them from needlessly consuming environment. Speaking of environment, you should specify more space than stock DOS.
CONFIG. SYS line similar to.. The number after /e: specifies how much space to set aside, usually. You can also execute command /e: 5. This can be included inside the batch itself.
For quickies, especially if you wish to restore all variables to their. Type exit to quit the command shell and return to the previous. Redirection and Pipes. Normally, input is taken from the keyboard and output goes to the console. Redirection allows input and output to refer to a file or device instead. Pipes allow the output of one program to be used as input to another program.
These symbols only work with programs that read from "standard input". DOS commands. The < symbol causes file to be fed to the program as input. The > symbol causes the program's output to be sent to the following. The > > symbol causes the program's output to be appended to the.
The | symbol (the pipe) causes the output of the preceding program. The following example shows how to use redirection with the FIND command.. If saved as say DOESIT. BAT, entering doesit "Word" text. Word" if the file TEXT. FIL. contains the string "Word" (at least under DOS 6).
Since we're. not interested in the actual output of the FIND command, it is redirected. Labels, Conditions and Branching. A label is any line that begins with a colon. Comments in batch code. REM. command because labels are processed before redirection symbols.
The goto command is used to transfer control to another part. For example.. : : test goto's. This text can be anything ***. Done. The decision mechanism in batch is the if command. It can be. used to compare one string to another, determine if a file exists or determine. If the evaluation is true. The not modifier reverses.
Examples.. if not %var%.==. MYFILE. DAT goto got_file. Errorlevel is equal or greater than 5. Errorlevel is less than 4. Notice the periods in the if %var%.==. The way the command is worded it branches. Read the wording in the.
Subroutines, CALL and FORMost of the elements are in place, but still no subroutines. These can. be tackled either by defining a return value and GOTO'ing the routine. GOTO's to the value of the return variable upon completion. For example.. set return=next. A more elegant way is to use a branch at the top of the batch that branches. This can be combined with the for and call.
Here is an example that prints. Sub. goto %2. for %%a in (%1 %2 %3 %4 %5) do call %0 Sub printname %%a. Launching programs. One of the most useful aspects of batch is its ability to set up custom. Here is a typical made- up scenario.
Suppose you have a game that requires you to change to say the C: \GAMES\MUT. MUTANT with the command line parameter /NSB. Rather. than typing all that, just put it into a batch and place it in a path directory.
The path - this is a list of directories held in the. PATH% variable that DOS uses to find programs without having to specify. For example, the default path in a simple setup. C: \DOS; C: \WINDOWS; C: \. Each directory is separated by a semicolon. When a command is typed.
DOS tries the current directory. If not there it begins searching. It is convenient to place batch. If you haven't done so yet, make the directory. C: \BATCH (for example) and add it to the path line, as in.. C: \DOS; C: \WINDOWS; C: \; C: \BATCH.
Any batch file now placed in C: \BATCH can be run from anywhere without. Now, back to the mythical example, let's. MUTANT. BAT and. place it in C: \BATCH (on the path now)..
NSB. The first line turns off command echo (contained at the beginning of. C drive is active, then. Here is an example for a virus scanner that does not. In this particular example, if the batch is called without parameters defaults. The specific commands, directories and parameters will vary depending. The command line is stored in. SCAN this translates to "scan the current.
If this batch. was saved as say SCN. BAT into a path directory (got that C: \BATCH directory. SCN instead of. having to type the directory\command parameters. Launching Windows 9. Windows 9. 5 has a built- in utility for launching Windows programs and. Windows programs directly from batch, a. Previously this required special utilities to "bridge.
The basic syntax is simple.. The full filename with directory information must be specified if not. If filename is a long. The /w option causes. Other start options. For example.. start /w /max "C: \Program Files\Myprogram\readme.
Anything that can be double- clicked to run can be launched with start. One very useful application of this is running Windows files from inside. AView that allows different viewers to. Here is a general purpose viewer for. RUNAFILE. BAT (for example).
Windows will figure out what to launched based on the selected file's. Menus for programs. The CHOICE command that comes with DOS 6 is handy for creating custom. DOS programs as well as simple Yes/No questions. Here is a simple.
GAMES MENU. echo ==========. Doom. echo 2 - Raptor. Flight. Sim. echo 4 - Lotus. Q - Quit. choice /c: 1. Q> nul. if errorlevel 5 goto done. CHOICE missing. The idea is simple, set up initial conditions, in this case change to. C: drive and the \games directory, print a menu using ECHO commands.
CHOICE command with the. Q (the valid choices) and redirect it to > nul so it. After the user makes a decision, the errorlevel. Raptor for example.
They are listed backwards because "if errorlevel. ANSI codes can be used to spruce up menus like these, even decent interfaces. Several menu. utility programs are also available that are called from batch. Using menuing systems.. Several freeware, shareware and commercial menuing programs are available. They're not as. fun as doing it entirely in batch but they can be more productive.
A more elaborate program is something called GO- MENU from an archive. DOSVAR2. 0 from Pinnacle Software. The shareware program was something.
GO- MENU. EXE is great. Here is its help screen when run with "/?"..
GO- MENU v. 1. 2. A - - Copyright (C) 1.
Pinnacle Software (Montreal). Purpose: Displays a menu of up to 1. Author: Tim Campbell (Dial 5. GEnie T. CAMPBELL1. Format: GO- MENU [menu- file- name] [nnn] [save- file- name]. Parms: Specify nnn to save & read last selection.
Program can continue if create or read of save file fails. Defaults: menu- file- name = GO- MENU. MNU. save- file- name = GO- MENU. SAV. Menu File: Line 1 Menu heading. Line 2+ Menu lines. Note: Lines starting with ; are ignored.
Returns: 1 - 1. 0 depending on menu selection. Esc in menu. 2. 50 for help request (this display). Here is an example of how I use it, extracted from my master system. This is the text file USER. MNU to define the menu text.. My Computer System.
Run Windows. Run DOS Prompt. Set Environment. House. Keeping Utilities. System Information. Files / Environment. I have no idea if GO- MENU is freeware, shareware or isn't even supposed. You can probably find it in the Sim.
Tel. archives. Look for "DOSVAR2. ZIP". Many others are available. Some of them can create very. I haven't..). Obtaining User Input. The CHOICE command is fine for asking limited- choice questions but it. There are two approaches. COM file like SENVAR.
COM that directly. Or you can do it completely in batch.
I've seen a couple of variations to this technique, one is contained in. Dirk Van Deun's Collection.
Batch Techniques file. The one I've been using originated from a. I've modified it to put the temp files in C: \DOS to.
ENTER. BAT'. You might need to change the. PC Magazine June 2.
C: \DOS dir and shortens var names. User input is returned in variable STR. C: \DOS\en#er. bat fc con nul /lb.
C: \DOS\enter. bat echo set str=. C: \DOS\enter. bat echo : loop. C: \DOS\enter. bat echo if not '%%str%%==' set str=%%str%% %%5.
C: \DOS\enter. bat echo if '%%str%%==' set str=%%5. C: \DOS\enter. bat echo shift. C: \DOS\enter. bat echo if not '%%5==' goto loop.
C: \DOS\enter. bat. C: \DOS\en#er. bat. Simply "call input. If this routine. is included in the batch program itself, set a return variable and call. Enter filename.. set return=here.
You entered %str%. A simpler way to call it is to use a universal branch at the top of. Loop goto %2. then when you want input do a.. Enter filename.. call %0 Loop input. When using batch input routines, do not enter redirection symbols or.
DOS, especially < > and |. How to set up SHELL and ANSITo use batch effectively you should check your CONFIG. SYS file for proper.