Batch file

From Conservapedia
Jump to: navigation, search

A Batch File is a set of command-line instructions for a system to perform. Batch files are usually processed by the operating system's shell processor.

Windows Batch

Windows Batch Files are probably the most common type of batch file, since Windows itself is quite common. These are processed by Command Prompt. Creating a Windows batch file is fairly simple, but one must know the commands they need to be executed. However, there are many options so someone experienced with command-line will generally be able to create a better batch file.

Example

A simple example of a Windows Batch File is:

@echo off
title Example
color F1
echo This is an example
::This is a comment, which will be ignored when run. It is for use in editing only.
pause
cls
echo Goodbye!

exit

Making a Windows Batch File

To create a Windows Batch file, only a plain text editor is required. Windows Notepad is sufficient, but programs such as Notepad++ can be easier to use. As soon as possible, the new batch file should be saved. The name can be just about anything, but the file extension must be .bat or .com which can be done right in the Save As dialog box of older systems. In newer systems (Windows Vista and later) this can be more difficult. The creator must either set their operating system to show all file extensions, or use a program to rename this file. Often, the "ren" command in command prompt is used for this purpose. Once the file is created and associated, the commands must be entered in order of execution. Note that there is not way for a single CMD process to perform multiple commands simultaneously. To find appropriate commands, the help command may be entered. Once an appropriate command is found, that command may be typed into command prompt followed by /? to retrieve further information and options.

Variables

Variables are essential to almost every program. While a batch file is not actually a program, it still offers them. To set a variable, use something like set varibleName=hi. Now to access that variable, use %varibleName%, which will be replaced with "hi" in this case.

Passed Variables

When a batch file is started, it may be passed up to nine variables. The shortcut or commandline start command would look something like "C:\batchFile.bat variable1 variable2 variable3 variable4 variable5 var6 var7 var8 var9" Each space separates one variable from each other. Of course, not all nine must be used. Inside this batch file, use these passed values by typing a single percent sign followed by the position number. If someone wants to use the second variable, they can enter simply %2. Note that these passed variable cannot be changed once the batch file has been started. In most cases, %0 holds the file path and name of the currently running batch file.

Useful Tips

  • To prevent CMD from displaying the command entry line as well as the output, an @ sign before the command. To disable this extra text for a series of commands or the entire file, enter echo off on its own line. To re-enable this at any time, just use echo on
  • The output of most commands can be sent to another location. At the end of a command, place >C:\file.txt to place the output on the first line of file.txt, or >>C:\file.txt to place the output on the last line of file.txt. To simply hide the output, try sending it to null space with >nul (with only one "L").
  • If the screen gets to crowded, simply use cls to clear the screen

GNU/Linux Batch

Windows is certainly not the only system to support batch files. Many GNU/Linux systems also support them,[1] as long as they have Wine installed.[2] As with Windows batch, these are step-by-step lists of commands to be executed.

Obsoletion?

Command Line used to be standard. However, with the modern GUI systems, its usefulness is limited. Command-line is continuing to become less popular, but some still find it useful. While Command line skills are not in high professional demand, they are helpful in certain cases. However, if it is being learned as a sort of programming language, an individual's time would probably be better spent learning C, C#, C++, or Java. Technology has advanced such that plain command-line is "old-fashioned."

References

External links