Batch File Commands

4/5/2017

Batch File Commands Average ratng: 7,5/10 1275votes

BATCH FILE COMMANDS Simple programming commands in a batch environment Revised May 6, 2011 Click here to refresh this page & its menu bar. Yeah, yeah, I know that.

Batch File Commands. BATCH FILE COMMANDSSimple programming commands in a batch environment. Revised May 6, 2. Click here to refresh this page & its menu bar. Yeah, yeah, I know that 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.

  • A batch file is an unformatted text file that contains one or more commands and has a.bat or.cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file. You can include any command in a batch file.
  • Batch Commands, an overview of the DOS, Windows and OS/2 commandlines.

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. This article summarizes the most important of these. Commandline 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. DOS Environment Variable Names . For example: COPY %windir%\filename a: Where does one get a list of DOS environment variables?

I have never found a comprehensive list; but a partial but lengthy list of existing environment variables can be gotten by typing SET at a command prompt. And 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. They do not exist for any other process and they do not survive the completion of the batch file’s execution. You can turn this setting off by issuing an ENDLOCAL command later in the batch file.

Silly example: To change the current logged drive to D: , do the following: SET GONEXT=D: %GONEXT%More practical example: You want to copy a file to the desktop of each user the next time they log into Windows. Each user logs into a different user profile, and the Desktop folder is in a unique location for each user. 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. START %1% %2%. One particular use of the START command is to launch the default browser and go directly to a URL, for example: START http: //google. You may use any of four command line parameters with the START command. These go after the word START, but before the program name: /minimized or /m/maximized or /max/restored or /r/wait or /w. 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. Put them all in a single batch file, using the /wait 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: START /max /wait NOTEPAD. EXE SOME. TXTIF and IF NOT Commands.

There are three variations of the IF and IF NOT commands. IF EXIST: Execute the commandline only if a particular file exists. IF EXIST some. txt COPY c: \some. SYSTEM\some. dll. Compare two text strings, and execute the commandline only if they are identical. If it is equal to or greater than the number specified. IF ERRORLEVEL 4 ERASE trashfile.

PGOTO Command. 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. For example, in my batch file for removing the Happy. UNHAPPY. BAT, 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 WSOCK3. SKA) is present: IF NOT EXIST WSOCK3.

SKA GOTO Saved. It. DEL WSOCK3. 2. DLLRENAME WSOCK3. SKA WSOCK3. 2. DLL: Saved. It. FOR Command. The syntax for this command is: FORvariablein (set list) DOcommand.

The variable must be in the form of one alphabetic character preceeded by %%; e. NOTE: The %% is necessary because this is in a batch file which, otherwise, would give a special meaning 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. 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. The command can be any valid command that otherwise could be entered into the batch file as a line of its own. This is especially helpful when you have several related batch processes and would like to combine them into a single application.

Back in DOS days, a common approach was to construct menus with multiple batch files. For example, you could create one batch file called MENU.

BAT that displayed the menu (a series of text lines), inviting a user to type a 1, 2, 3, etc. That menu batch file would end, delivering the user back to a command prompt.

You would then have a series of other batch files called 1. BAT, 2. BAT, 3. BAT, etc. It’s really terribly simple.)Today, when users don’t live in a DOS command prompt world, we want something slightly more sophisticated — and, fortunately, we have it. There is a pretty cool way to allow user input in Windows 2.

XP, and even better ways that work in Windows Vista. In Windows 2. 00. XP, the SET command has new /A and /P flags that allow user input.

The latter is especially helpful for our present purposes. You can accept user input and assign it to a system variable with the following code: SET /P variable=Prompt.

String. The Prompt. String is optional. If you include one, it will be displayed on the screen. It will then assign whatever character the user types to the system variable %M%, which you can use in other batch file commands. Windows Vista has added the CHOICE command. This is pretty cool! It lets you build simple menus just from this one command.

On a Windows Vista computer, open a command prompt and type CHOICE /? At the present, this might not be so useful if yo uare writing batch files that also will be run on Windows XP computers, because the CHOICE command will not work on those computers — and the SET /P approach above still works in Vista. Here is an example of a batch file I recently wrote for my office.

It uses many of the features discussed on this page, including menu creation. The problem to be solved was that (for reasons too tedious for the present article) users accessing our network remotely no longer had access to their browser Favorites.

Additionally, it was useful (when swapping out computers) to migrate a user’s Favorites from the old computer to the new. Both of these could be solved by moving the Favorites (which are simply shortcut files) up onto a personal network drive (let’s call it P: ) to which they always had access.

I wanted to allow the user, with a single file that I could email them, to be able both to cache their Favorites on the network drive and to pull these back down to another computer. Here is a slightly edited version of the batch file.

ECHO OFFCLS: MENUECHO. ECHO ........................

ECHO PRESS 1 or 2 to select your task, or 3 to EXIT. ECHO ........................

Batch file - Wikipedia. A batch file is a kind of script file in DOS, OS/2 and Microsoft 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 interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as . When a batch file is run, the shell program (usually COMMAND.

COM or cmd. exe) reads the file and executes its commands, normally line- by- line. Windows NT and OS/2 also added . Batch files for other environments may have different extensions, e. DOS, 4. OS2 and 4. NT related shells. The detailed handling of batch files has changed.

Some of the detail in this article applies to all batch files, while other details apply only to certain versions. Variants. When DOS loads, the file AUTOEXEC. Basketball Playbook Software Free Download. BAT, when present, is automatically executed, so any commands that need to be run to set up the DOS environment may be placed in this file.

Computer users would have the AUTOEXEC. BAT file set up the system date and time, initialize the DOS environment, load any resident programs or device drivers, or initialize network connections and assignments. A . In order to start it, the WIN command was used, which could be added to the end of the AUTOEXEC. BAT file to allow automatic loading of Windows. In the earlier versions, one could run a . Windows in the MS- DOS Prompt. Windows 3. 1x and earlier, as well as Windows 9x invoked COMMAND.

COM to run batch files. The IBMOS/2 operating system supported DOS- style batch files. It also included a version of REXX, a more advanced batch- file scripting language. IBM and Microsoft started developing this system, but during the construction of it broke up after a dispute; as a result of this, IBM referred to their DOS- like console shell without mention of Microsoft, naming it just DOS, although this seemingly made no difference with regard to the way batch files worked from COMMAND. COM. OS/2's batch file interpreter also supports an EXTPROC command. This passes the batch file to the program named on the EXTPROC file as a data file.

The named program can be a script file; this is similar to the #! Windows NT introduced an enhanced 3. CMD or . BAT extension. Cmd. exe added additional commands, and implemented existing ones in a slightly different way, so that the same batch file (with different extension) might work differently with cmd. COMMAND. COM. In most cases, operation is identical if the few unsupported commands are not used. Cmd. exe's extensions to COMMAND.

COM can be disabled for compatibility. Microsoft released a version of cmd. Windows 9x and ME called WIN9. CMD to allow users of older versions of Windows to use certain cmd. As of Windows 8. This extension runs with DOS and all versions of Windows, under COMMAND.

COM or cmd. exe, despite the different ways the two command interpreters execute batch files. Used for batch files in Windows ME family and sent to bat. COMMAND. COM does not recognize this file name extension, so cmd. Windows environment by mistake. In addition, . These scripts are faster, especially with longer ones, as the script is loaded entirely ready for execution, rather than line- by- line. Non- existent parameters are replaced by a zero- length string.

They can be used similar to environment variables, but are not stored in the environment. Microsoft and IBM refer to these variables as replacement parameters or replaceable parameters, whereas Digital Research, Novell and Caldera established the term replacement variables. JP Software calls them batch file parameters. The @ symbol at the start of any line prevents the prompt from displaying that command as it is executed. The command ECHO OFF turns off the prompt permanently, or until it is turned on again.

The combined @ECHO OFF is often as here the first line of a batch file, preventing any commands from displaying, itself included. Then the next line is executed and the ECHO Hello World! The next line is executed and the PAUSE command displays Press any key to continue . After a key is pressed, the script terminates, as there are no more commands. In Windows, if the script is executed from an already running command prompt window, the window remains open at the prompt as in MS- DOS; otherwise, the window closes on termination. Limitations and exceptions.

This can lead to syntax errors or bugs. For example, if %foo% is empty, this statement: parses as the erroneous construct: Similarly, if %foo% contains .

The text that is being compared to the variable must also be enclosed in quotes, because the quotes are not special delimiting syntax; these characters represent themselves. IF! In this case, null or multi- word variables do not fail syntactically because the value is expanded after the IF command is parsed: Another difference in Windows 2. As described in previous examples, previous batch interpreter behaviour would have resulted in an empty string. Example: C: \> set. My. Var=C: \> echo%My.

Var%%My. Var%C: \> if. A single quotation mark (. However, an escaped quotation mark (.

If quotes are included they become part of the string. This can cause conflicts where a string contains quotation marks, and is to be inserted into another line of text that must also be enclosed in quotation marks: C: \> setfoo=.

They cannot be printed as text using the ECHO command unless escaped using the caret ^ symbol: C: \> Echo foo . The variable ends up containing a live pipe command when merely echoed.

It is necessary to escape both the caret itself and the escaped character for the character display as text in the variable: C: \> setfoo=bar ! All rights reserved. C: \> setfoo=bar ^. No solution was available from Microsoft, but a number of small utility programs, could be installed from other sources. A commercial example would be the 1. Norton Utilities Batch Enhancer (BE) command, where BE DELAY 1.

WAIT. COM. Most such programs are 1. COM files, so are incompatible with 6. Windows. Text output with stripped CR/LF.

In this example, the CR/LF does not follow Message 1, but does follow Line 2 and Line 3: batchtest. While a batch file can be run from a UNC file path, the working directory default is . When an administrator is logged on and UAC is enabled, and they try to run a batch file as administrator from a network drive letter, using the right- click file context menu, the operation will unexpectedly fail. This is because the elevated UAC privileged account context does not have network drive letter assignments, and it is not possible to assign drive letters for the elevated context via the Explorer shell or logon scripts. However, by creating a shortcut to the batch file using the above PUSHD / POPD construct, and using the shortcut to run the batch file as administrator, the temporary drive letter will be created and removed in the elevated account context, and the batch file will function correctly. The following syntax does correctly expand to the path of the current batch script.

CD command will refuse to change but placing a UNC path in Default Directory in a shortcut to Cmd or by using the Start command. Code page 4. 37. The non- ASCII parts of these are incompatible with the Unicode or Windows character sets otherwise used in Windows so care needs to be taken.

File names with characters outside this set won't work in batch files. To get output in Unicode into file pipes from an internal command such as dir, one can use the cmd /U command. For example, cmd /U /C dir > files. Windows characters, in the UTF- 1. LE encoding. Batch viruses and malware. Simple trojans and fork bombs are easily created, and batch files can do a form of DNS poisoning by modifying the hosts file.

Batch viruses are possible, and can also spread themselves via USB flash drives by using Windows' Autorun capability. COMMAND. EXE, which interprets . BAT files, was supported in all 1.