Unknown
Aug 25, 2004, 12:38 PM
[doHTML]<b>BATCH FILE COMMANDS</b><br><b class="title5">Simple programming commands in a batch environment</b><br><br><br>Many people think batch files are mostly things of the past. Sometimes, though, a well-conceived batch file is just the thing to automate the job you want to do.</p> <br>I am not going to cover all the theory and practice of batch files from the ground up. Any good book on DOS (now found in the Antiquities section of your local library <g>), and many of the best on Windows, will have a section on batch files. Simply put, a batch file is a plaintext file with a name ending in .BAT. In its simplest form, it contains a series of commands that could be executed from a command prompt (system prompt). The batch file simply autoexecutes them for you. (In fact, AUTOEXEC.BAT is the best known, and most widely used, batch file.) To execute a batch file, type its name at a command prompt, or execute a Windows shortcut that does the same thing.</p> <br>The simplest idea of how to write a batch file is: Figure out how you would type the commands at a DOS prompt, then type them, one per line, in a text file — and you’ve written your batch file.</p> <br>However, there are also more sophisticated batch file structures, using simple programming commands built into the batch structure. This article summarizes the most important of these.</p> <a name="commarg"></a><br> <h4 class="med">Commandline Arguements {%x}</h4> <br>Variables can be inserted into a batch structure in the form of command line arguements. These are in the form %1, %2, etc. To populate the variables, type the desired values after the batch file name when executing it.</p> <a name="envvar"></a><br> <h4 class="med">DOS Environment Variable Names {%nn%}</h4> <br>DOS environment variables also can be used as variables in batch files. For example:</p> <p align="center"><b class="violet">COPY %windir%\<i>filename</i> a:</b></p> <br>Where does one get a list of DOS environment variables? I have never found a comprehensive list; but here’s the really cool part! You can make them up as you go along, and assign them as you wish (as long as you don’t grab one that has a legitimate assigned value, such as, say, %windir%, the Windows directory name!). Pick a name, populate it with the SET command by any means known to you (including having one batch file run a process that includes setting one, and then another batch file using it), then use it by placing the name between flanking % signs. Environment variables remain until overwritten, or until a reboot. (If you set them in a DOS window, they will end when that session is closed.)</p> <br>Silly example: To change the current logged drive to D:, do the following:</p> <br><b class="violet">SET GONEXT=D:<br> %GONEXT%</b></p> <br>By the way, a partial but lengthy list of existing environment variables can be gotten by typing <b class="violet">SET</b> at a command prompt. (Thanks to correspondent “Paul” for the reminder!)</p> <a name="startcom"></a><br> <h4 class="med">START Command</h4> <br>The START command can launch a Windows program either by specifying the program name (and its command-line parameters), or by specifying a data file name that is associated with a particular program (one that would automatically launch if you clicked on it in Windows).</p> <br>For example, if you have NOTEPAD.EXE associated with all TXT files, then you could open the file SOME.TXT in any of the following four ways:</p> <br><b class="violet">NOTEPAD SOME.TXT<br> SOME.TXT<br> START NOTEPAD.EXE SOME.TXT<br> START SOME.TXT</b></p> <br>Why use one or the other? Well, sometimes you may have to use one particular form to get a result — depending, for example, on how the particular program is coded. Though the first form usually will work, you may want, for example, to write a more general batch file to open any particular program and associated file — without knowing what the requirements of all such files might be. You could, then, write a general batch file line such as <b class="violet">START %1% %2%</b>.</p> <br>You may use any of four command line parameters with the START command. These go <b>after</b> the word START, but <b>before</b> the program name:</p> <br><b class="violet">/minimized</b> or <b class="violet">/m<br> /maximized</b> or <b class="violet">/max<br> /restored</b> or <b class="violet">/r<br> /wait</b> or <b class="violet">/w</b></p> <br>The first three determine the screen status in which the program opens. The last one forces the batch file to halt processing until the called program has finished executing. (This can be useful, for example, if you are loading multiple items in your windows Startup folder, and the nature of the programs require that one be finished before the next starts loading. Put them all in a single batch file, using the <b class="violet">/wait</b> parameter, and only put a shortcut to the batch file in the Startup folder.) Command line parameters of the START command can be combined in a single line. Example:</p> <p align="center"><b class="violet">START /max /wait NOTEPAD.EXE SOME.TXT</b></p> <a name="ifcom"></a><br> <h4 class="med">IF and IF NOT Commands</h4> <br>There are three variations of the IF and IF NOT commands.</p> <ul> <li><b>IF EXIST:</b> Execute the commandline only if a particular file exists: <p align="center"><b class="violet">IF EXIST some.txt COPY c:/some.dll %windir%/SYSTEM/some.dll</b></p> <li><b>Compare two text strings,</b> and execute the commandline only if they are identical. <p align="center"><b class="violet">IF %HostWinBootDrv%==C SET WinDir=C:\WINDOWS</b></p> <li><b>Error testing:</b> Check the exit code of the most recently run program. If it is equal to or greater than the number specified, execute the command: <p align="center"><b class="violet">IF ERRORLEVEL 4 ERASE trashfile.tmp /P</b></p> </ul> <a name="gotocom"></a><br> <h4 class="med">GOTO Command</h4> <br>You can set a label in a batch file by beginning a line with a colon. You can then go directly to that label with the GOTO command. The GOTO command searches both forward and backward in the batch file; that is, it simply goes to the label location, regardless of where it is in the file.</p> <br>For example, in my batch file for removing the Happy99 virus, <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=../../downloads/unhappy.zip">UNHAPPY.BAT</a>, the following code was used to make sure a file was not deleted unless the original form of it (backed up by the virus under the name WSOCK32.SKA) is present:</p> <br><b class="violet">IF NOT EXIST WSOCK32.SKA GOTO SavedIt<br> DEL WSOCK32.DLL<br> RENAME WSOCK32.SKA WSOCK32.DLL<br> :SavedIt</b></p> <br> <a name="forcom"></a> <h4 class="med">FOR Command</h4> <br>The syntax for this command is: <b class="violet">FOR</b> <i class="violet">variable</i> <b class="violet">in (</b><i class="violet">set list</i><b class="violet">) DO</b> <i class="violet">command</i></p> <br>The <b class="violet"><i>variable</i></b> must be in the form of one alphabetic character preceeded by %%; <i>e.g.,</i> <b class="violet">%%v.</b></p> <blockquote> <br><b>NOTE:</b> The %% is necessary because this is in a batch file which, otherwise, would give a <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#commarg">special meaning</a> to a single %. However, if you run the FOR command outside of a batch file, simply from the system prompt, just use a single % in the variable name. <i>(Tip from Steve Wisdom)</i></p> </blockquote> <br>The <b class="violet"><i>set list</i></b> is enclosed within parentheses. These values will be assigned to the variable successively. You can use any text enclosed in quotes, batch file commandline parameters, environment variables, or DOS file wildcard expressions.</p> <br>The <b class="violet"><i>command</i></b> can be any valid command that otherwise could be entered into the batch file as a line of its own. example:</p> <p align="center"><b class="violet">FOR %%D in (SYSTEM, COMMAND, SHELLNEW, "Start Menu") DO DIR "%windir%\%%D" /W</b></p> <br> <a name="moreinfo"></a> <h4 class="med">More Information on These Commands</h4> <br>Each of these options (START, IF, GOTO, FOR) is an actual DOS command. At a system prompt, type the command name followed by <b class="violet">/?</b> to get further help on these items.</p> <br>Note that there may be particular capabilities that show up in one version of Windows, but not in another. For example, though DOS <i>per se</i> may well be dead in Windows XP, the commandline functions people most often associate with DOS are not dead at all! (We just don’t call them “DOS commands” anymore; we call them “command prompt commands”. But they’re the same thing.) In some cases, these commands have been made <b>more powerful</b> in Windows XP. In particular, if Win XP Command Extensions are enabled, each of these four has <b>very greatly enhanced</b> capabilities (see Win XP Help & Support to learn about Command Extensions). Take advantage of the opportunity to explore each of them with the <b class="violet">/?</b> help flag.</p> <blockquote> <br><b>SOME EXAMPLES (that don’t even require Command Extensions):</b> START has new flags that let you set the priority (/LOW, /NORMAL, /HIGH, etc.) of the application you are launching. IF has an ELSE option than greatly expands its power, but which requires a somewhat complicated syntax sometimes (which the <b class="violet">/?</b> help text covers reasonably well).</p> </blockquote> <br>Since these START, IF, GOTO, and FOR are actual OS commands, they can be used from a system prompt just like DIR, COPY, or any other DOS command. This means that they can be used outside of a batch file as well. There are small differences or issues that you can easily discover in use, and discussion of which would go beyond the purpose of the present page. For anyone comfortable working at a DOS system prompt, this should present no significant problem. </p>[/doHTML]
Unknown
Nov 09, 2004, 09:24 PM
Are batch files like memory chips of ram......or just damn ruminating my memory chips?
I mean, the more I get of either, the more I want to not understand what I just did, and forget what I learnt while I was back there anyway?
This is a very serious question btw?
Love Jasper
please explain