batch file

Download Batch File

Post on 12-Nov-2014

3.126 views

Category:

Documents

5 download

Embed Size (px)

TRANSCRIPT

A Modern Batch Programming Tutorial (Win 2k/XP) Change the Look on This Site This page teaches you some modern NT-based batch programming and has some fairly advanced and pretty useful batch scripts to help you get started. Batch files are the simple and rather archaic, interpreted scripting language of MS-DOS and it's derivatives and followers such as Windows 9x and NT (NT, 2k XP). Basic MS-DOS knowledge is assumed, and it does help if you know some programming language already, although this is not strictly necessary, I'll try to explain most of the terminology on the way. I've written this in tutorial form so be sure to check out other options and commands not covered here extensively. Although many of the commands and scripts represented here will work on MSDOS and later, many Many of the newer command-options that are essential to advanced batch programming do require Windows NT, 2000 or XP (of course 2003 will also do as will probably LongHorn once it comes out). Although I didn't know it when I started writing this tutorial, I've later on realized that the batch language is equivalent to the old late 70s basics having commands roughly equivalent to input, print, let, if and goto. And because repetition can be expressed with goto and if as in most assembler languages, you can build more elaborate programs out of these primitives. You may wonder why one would write batch files these days as there are really good, real programming languages around. Things like Perl, CPP or Java to name a few. For full blown programs I prefer Java or CPP , true, and Perl needs to be installed separately. Nowadays I tend to do all of these little programs with Perl and use batch files just on systems that don't have Perl installed. But back to batches, I do find this an intellectual challenge, accomplishing as much with batches as is practical. Unlike Perl and many other languages, Batch files are native to DOS and you can count on them on every machine without having to download tens of megs of third-party software (well only about a floppy in case of tiny Perl). Batch syntax is also relatively easy and the language is interpreted. OK there are Visual Basic script and JavaScript but I don't really want to learn either of those for various reasons including platform dependence. WARNING: While I've been working with DOS since MS-DOS 5 and still do use the DOS BOX

occasionally, it is still possible that some information in this document is not 100 percent accurate, as I don't know the formal syntax of batch files exactly, nor do I know everything about DOS or NT specific batch commands. So, not to be used for mission critical stuff, hehe. Any comments, additions and corrections would be welcome, though. NOTE: I tend to use the term NT to mean the set of operating systems based on the NT kernel (NT, 2000, XP and 2003 currently). In most cases it means Windows 2000 or XP as most of the command-extensions were added in 2k, I believe. Contents

The Basics of Batch Programming Batch Syntax and Somee Practical Notes Relative Paths Wildcards Redirection Piping Variables commands Good Batch Programming Style NT Specific Command Extensions String Input, Integer Arithmetic and Looping The Else clause, String and Numeric Comparisons The Indispensable For Command More String Processing and Some Magic Variables Bigger Example Programs Emulating Gosub Random Lines Array Emulation and Sorting Epilog

The Basics of Batch Programming this section introduces the basic MS-DOS batch commands and concepts like relative paths, redirection, pipes and variables (but some Windows pitfalls are also mentioned). If you are an experienced batch programmer, you might want

to skip this section if you feel like it. However, if you've done batches but your batch knowledge is a bit rusty, for instance, acquired in the good old DOS days, you may wish to quickly browse through this in case there's anything new. Let me emphasize that I'm not trying to be complete and cover all the options and idioms, just the most useful you'll likely use most of the time. Batch Syntax and Somee Practical Notes Batch files are series of MS-DOS commands typed in a file, one command per line. The file uses the MS-DOS character set, has an extension of .bat and it is run automatically if you type it's base name without the extension. If an exe or com file with the same basename exists, you might want to explicitly specify the .bat extension to guarantee that the batch file gets executed. If you need to use high-ASCII characters in the file such as umlauts and special graphics symbols, you must save it as MS-DOS text otherwise the characters might be different from what you expected. Many of the better Windows text editors can save as MS-DOS text (even Wordpad can), and there's good old Edit which still writes out MS-DOS files. The philosophy of batch programming is that nearly all of the batch constructs are ordinary commands that can also be used outside batch scripts in MS-DOS. Although some of these commands are virtually never used outside batches, they are still their in DOS. So most of the commands you'll be likely using are ordinary DOS commands and work just as you would expect them to. By the way, if you run into problems in a batch file (e.g. it doing unwanted things, getting stuck in a loop etc...) you can in most cases quit the execution of a batch file by pressing ctrl+c and answering y when asked if you really want to terminate the batch job. To get a list of all MS-DOS commands type in help in the prompt. For help on an individual command type it's name followed by a slash and a question mark (e.g. copy /?). I recommend getting to know most of the commands that look interesting, so you'll be familiar with the set of tools used in real world batch scripting. In order for help to work in Windows 9X, you need to download this set of old DOS commands , extract it and run help in the current directory. One excellent resource covering pretty much everything from ancient DOS

utilities like edlin to cmd extensions and little known commands such as findstr is Microsoft Windows XP Command Reference . I definitely recommend it even over the DOS command help pages. Relative Paths Although many people know the cd and md commands, it is not that common to use relative path names in DOS that much. The path names are called relative because they are specified relative to the current directory (.)). Here are some examples: cd games\duke3d Will change to .\games\duke3d where . is the current directory. It is not necessary to type cd games cd duke3d separately, but on the other hand this means less re-typing if you make a mistake. I'd suggest using complete pathnames as much as possible to decrease the number of lines in your batch scripts, though. Notice that md games\mygame Doesn't work as you would expect, unless you have NT command extensions on but that's another story. Another way of using relative paths is to refer to directories that are on a lower level on the directory hierarchy. Say we are in \temp copy ..\autoexec.bat . Will copy autoexec from the root directory to the subdir temp. This can also be risky, don't ever type del . in temp like I once did. The two periods can be chained like this ..\..\..\ to refer to even lower levels on the directory tree. You can also continue directory specifications after a period. Supposing we are in \games\dukebacup xcopy /s ..\duke3d\* . Will copy everything under \games\duke3d to \games\dukebackup including sub-directories. Notice that \games\duke3d is actually also a relative path name (it's relative to the current drive). Changing drives can also be done relatively, d: wil change to the previous directory in which you were on drive d where as d:\ switches to the root of drive

d. Finally, environment variables (see the section on variables) can be used as pathnames. A common example is to refer to the windows system32 directory regardless of actual Windows directory name with: %windir%system32 The variable named windir can be used in NT and later on in which it's defined to contain the location of your Windows directory. Wildcards Wildcards are an extremely useful tool for processing sets of files. Most, though not all, DOS commands do support wildcards. The idea is that in stead of giving a name of a single file, you use some wildcards to make the name more generic. All the file names that match the given wildcard expression will be processed. The most common wildcard is the asterisk sign * which replaces any number (including 0) of any characters. Examples: *.txt Will select all files that end in .txt for processing *.* and * are strictly speaking different. *.* selects all file names with extensions, that is, files that have a period in the name. Where as * will also select files that don't have any extension. Actually, doS doesn't seem to make the difference between the two, even if it should, although most Unix shells likely do. foo*bar* Will select everything starting with foo and also containing the text bar as well: foobar.aaa foobarb.ab foo.bar and fooblabar.txt would all be selected. The other wildcard character is the question mark which replaces 0 or 1 instances of any characters. *.st? Would select all files whose extension starts with st, the third character may be anything and even files with extensions starting with just .st would be selected.

Redirection Redirection is the process of directing command output to a file or reading keyboard input for a program from a file. It's a standard trick of Unix chunkies but not too well known in DOS. Here's how it works: command output Will read lines of keyboard input from the file input and write the output of the command to the file output. Either of the redirection symbols (< or >) can be omitted at will. If the output file doesn't exist, it will be created. If it does exist, however, it is overwritten. To be ab

Recommended

View more >