Msdos batch files tutorial




















That is because the colon is an illegal label character. Some people use a single colon to place remarks in a batch file, but since DOS uses this to identify its labels and those lines will be read, I suggest the single colon not be used, except of course, as a label precursor. You'll read more on labels in the tutorial's examples.

Alternatively, others will use "REM" which is short for "Remark". DOS does not execute these lines either, but it does read them, slowing things down. This is not a problem with today's fast processors and hard drives, but can make a difference on slower computers when the batch file is long and contains a lot of remarks.

Regardless, my philosophy is that anything which speeds operations should be used. I must mention there is also a problem with "REM" lines which contain redirectors and piping, which I won't get into here because it is beyond the scope of this tutorial. Getting back to the example batch file: For the third line I employ the double colons with the remainder of the line blank. It is used as a separator between the title and the file itself.

Although they do not affect the running, or contribute to the mis-running of the batch file, they should be used here because DOS will display an unnecessary prompt on the screen for any blank line before an "ECHO OFF" command.

The fourth line is required to tell DOS to not display the succeeding lines on the monitor screen, and the " " symbol tells DOS to not show the line itself. Since a batch file is just a series of commands, what is typed in the file would be echoed on the screen as DOS executes each of those lines, just as though you were typing each instruction at the command prompt.

However, in most cases, the user is only interested in the end result and does not need to see everything that DOS is doing. Using the "Echo Off" command means each command issued by the file as it works toward completing the requested task will not be echoed to appear on the screen.

Next is a blank line. DOS ignores blank lines if they occur beyond an "Echo Off" command , and executes the following line immediately. I insert such lines to separate the various steps the file does to complete its assigned task. It makes no difference to the running of the file but does give each part its own space, making for better readability. This is important for lengthy, complicated files, especially if you go back into them a long time after they were written.

This is important if you are logged onto a floppy, CD-ROM, RAM, or flash drive, or onto another hard drive, yet wish to start word processing immediately. The next line changes to the WP60 directory and the final line tells WordPerfect to start. Note that I used the full executable name WP. I always do this as a precaution against companion viruses. I won't get into a long explanation here but a companion virus substitutes its own executable for common program-starting commands. In this case, it might substitute WP.

Be aware that some programs use their own batch files to start themselves. When writing batch files to start those programs you must refer your batch file to that batch file. Such lines will be discussed soon. There you have it. Save that as "WP. Since the batch file changes to the correct directory for WordPerfect, DOS does not have to search the path to locate the WP executable file.

Since it no longer has to search, having WordPerfect in the path statement is unnecessary. The fewer directories through which DOS must search, the faster it locates what it wants, resulting in a more efficient, and thus faster, operation of your computer. Be sure to tell WordPerfect where its files are via the program's set-up section.

If this is not been done, removing its directory from the path may result in some error messages while running WordPerfect. Typically, pressing Shift-F1 will display the set-up screen when in WordPerfect. It gives a wide-format display of the contents of the directory with sub-directories and file types in different colours. You could display the directory contents by incorporating DOS's "DIR" command, if you wish, but Color Directory dresses up the screen and makes it easier to discern file types.

Now, we could add these commands to the end of the above batch files, but since I want to this to happen after running most batch files I write, it's a better idea to have this be its own batch file, and then have other batch files refer to this new one as part of its list of commands. It will save typing those commands into every batch file, but more important is that should you decide to change your desktop, only this one specific file needs to be altered, because all others will refer to this one.

We have already discussed the opening information lines, the " ECHO OFF" command, the double colons and blank lines, so I will skip them here and for all future discussions. The final command is for the Color Directory. It's also a batch file called "DR. I have named this batch file "CLR.

I can now add this "CLR" command to the end of any batch file to clear the screen and return me to my desktop automatically after completing any task. The reason this works is that after a DOS batch file hands control over to a program, when the program finishes, it returns the reins to DOS which remembers that there is another line in the batch file to run.

It therefore runs the "CLR" batch file upon exiting a given program, which then returns me to my desktop. I won't get into an explanation of its options here because that would be outside the subject of this article. You'll need to consult the WP manual to learn more; however I'll give you an example of some. With these options, the batch file now becomes:. Some testing shows that the query string -q will always be executed before the -I SQL file, which allows us to use a query string to set up a temporary table.

The trick of renaming the Perl. However the solution I have seen so far needed batch code before and after the Perl script where as the solution presented below only needs some lines of DOS at the top.

The added DOS script is generic and works independent from the name of the file. When running the batch the DOS command interpreter will read the first lines and execute the file itself in Perl context. This example works just as the previews one but will wait 4 seconds before the application finally closes.

This is just enough time to inspect the screen output before the window vanishes. The delay can probably be done much easier in Perl, but somebody just starting on Perl might find this still useful. TOP Simple menu framework. Plug in a new menu item and coresponding script as single function block. Example: FTP -v -i -s:ftpscript. Script Output. Example - Installation Framework. Features: simple and well structured settings changeable at runtime settings are persistent , the batch will remember all settings from the last run progress indication during simulated file installation.

Download: BatchInstall. Simulating an installation for! Pretend to install! This advanced batch template features: Initialization of the command processor Version History for manual script change tracking Window title of choice Delayed exit for unattended completion.

Download: BatchDelayOnExit. Hello World REM. Features: The lookup list will be appended to the batch file itself. Download: BatchListOfFiles. FileSearch, type '-' to refresh lookup list: logoMed. Download: BatchSubstitute. Showing progress in the output window seems impractical in DOS batch, since there is no way to overwrite a previews output for an updated progress status during each progress tick. Using the ECHO command is not nice, screen content quickly scrolls out of sight.

A nice side effect is that you can minimize the batch window and still observe the progress output in the task bar, since the task bar text will always reflect the window title text. Download: DosProgress. TOP The assumption is: A batch script snippet can be named a function when Example - Function with Arguments. Example - Calling a Function. Example - Returning Values using Variable Reference.

Parsing Function Arguments. Passing Function Arguments. Returning Values the Classic Way. Returning Values via References. Let the caller determine how to return the function result and avoid the need of dedicated variables.

A function can be called with the CALL command followed by the function label. A single batch file can contain multiple functions defined like this. The label becomes the function name. For example, a batch file could be used to run frequently run commands, deleting a series of files, moving files, etc. A simple batch file does not require any special programming skills and can be done by users who have a basic understanding of MS-DOS commands.

A good example of a batch file for someone who is more familiar with Windows or the MacOS is to think of a batch file as a shortcut in Windows or an icon on the MacOS. Much like a shortcut, batch files could be used to run one or more commands or programs through the command line.

Another example of a very well known batch file is the autoexec. This batch file contained all the necessary commands and programs used to run MS-DOS and Windows each time the computer booted. To create a basic batch file in MS-DOS, follow the below steps that give you an example of how to create a basic batch file. If you wish to add more lines to this batch file you would type "edit test. Some versions of MS-DOS and bootable diskettes may not have the edit command; if this the case, you would either need to obtain the edit.

If, however, you're more comfortable using Microsoft Windows or your operating system, you can use any text editor, such as Notepad or Wordpad , to create your batch files, as long as the file extension ends with.

In the below example we use the Windows notepad to create a batch file. However, in the below listing we have listed all commands in all caps to help you identify what is a command and what is not. When the above one-line batch file is created, add your name after the batch file. For example, typing myname being the name of the bat file and then your name:. Running different programs How to start Windows files and programs from a batch file Creating a batch file delay How to make a time log.

Below is an example of how you can implement the choice options into your batch files.



0コメント

  • 1000 / 1000