WishMesh http://wishmesh.com Real men don't make backups Sat, 21 Feb 2015 15:39:00 +0000 en-US hourly 1 http://wordpress.org/?v=4.1.1 MS-DOS command redirection operators http://wishmesh.com/2015/02/ms-dos-command-redirection-operators/ http://wishmesh.com/2015/02/ms-dos-command-redirection-operators/#comments Sat, 21 Feb 2015 15:38:15 +0000 http://wishmesh.com/?p=1359 ) and the input redirection operator ( is … Continue reading MS-DOS command redirection operators ]]> This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: IF statement in DOS batch file.

console-dos-2-small

The two most commonly used redirection operators used in console are the output redirection operator (>) and the input redirection operator (<). The output redirection operator > is used to send the command output to somewhere other than the screen. A plain text file would be an example. The next example shows how to put the results of the DIR command into a text document. Let us name the text file file-list.txt:

C:\>dir >file-list.txt

When this command is entered either at a Windows CMD prompt or in a batch file/script you do not see the directory listing like you would if you had simply entered the DIR command. Instead, a file is created and in this case the file is named file-list.txt. This can be used to capture practically any command.

Run this file several times and notice the changes. Each time you run this program (DIR >file-list.txt), the result of the DIR command gets saved to that file-list.txt file. Note that “>” will erase the preceding contents and save the new DIR listing information. But what if we wanted to create a type of log that would append and save each new occurrences of the above program? We can use what is called the append redirection operator. This is done by adding another greater than sign (>>).

C:\>DIR >>file-list.txt

Next we have the input redirection operator (<). The input redirection operator is used to send the contents of a file to a DOS command (normally the contents of keyboard input get sent to the DOS command).

An example of when you would want to do this would be if you have a text file already prepared, or for use in the batch script, when run in unattended mode. You can enter the following command sequence:

C:\>more <file-list.txt

If your DIR listing was very long, then you would be prompted to “Press any key to continue…” after a screen-full (one page) of data was displayed. Once you pressed any key you would see the next page of text. If it was short then the MORE command will simply display the whole text as if you typed the TYPE command. The TYPE command is used to display the contents of a file. You do not need to use a redirection operator to use the TYPE command.

C:\>type file-list.txt

This will type out the contents of file-list.txt to screen. And if the file contains more than one screen, it will display the text very fast, and you may only see the last page.

The next command we will learn is the DOS piping command. The pipe | can usually be inserted to the command prompt by holding down the SHIFT key and pressing the key to the direct left of the backspace key (or under backspace on some keyboards). DOS piping is a technique that combines both the output and input redirection operators. We can capture the output of one command and send it as the input to another command. Example:

C:\>dir | more

In this example the pipe command captures the output of the DIR command and sends it as input to the MORE command which will display one screen-full of results at a time.

You will come to find that batch scripting really does have a great amount of power. However, there will be a times when you will find that batch scripting is not powerful enough. When you find yourself in a such situations, you may want to look at other scripting possibilities such as multi-platform Python, Perl, Lua, or if you need all native Windows way, PowerShell is the answer.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

]]>
http://wishmesh.com/2015/02/ms-dos-command-redirection-operators/feed/ 0
IF statement in DOS batch file http://wishmesh.com/2015/02/if-statement-in-dos-batch-file/ http://wishmesh.com/2015/02/if-statement-in-dos-batch-file/#comments Sat, 21 Feb 2015 15:00:18 +0000 http://wishmesh.com/?p=1343 Continue reading IF statement in DOS batch file ]]> This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: Loops in the Batch files with examples.

console-dos-1-small

The IF command in the batch scripting is very similar to the IF statement in other programming languages. The IF command allows you to branch the execution of the batch file into two separate paths depending on some condition.

The basic syntax of IF command is:
IF {Statement} {If statement is TRUE, perform this action}

If the statement being tested is TRUE, then perform the action on the same line (to the right side of IF). If the statement is FALSE, then continue to the next line. The TRUE action statement is, essentially, ignored in case of FALSE. A GOTO command could perhaps be present on this line to send processing of the batch file to another location in the script. The IF command is great for testing three types of conditions including replaceable parameters, system ERRORLEVEL, and it can compare two strings.

Remember that several IF commands can be stacked one after another, to test more conditions, or to create tree like algorithms. An algorithm is simply your solution to the problem at hand.

Here is an example that tests replaceable parameters (Note that on some versions of Windows or a different configuration of your command prompt might change how this example is run. Play around with it and your CMD interpreter settings):
:: LOOP.BAT
:: Compares Replaceable parameters.
@ECHO OFF
:TOP
   IF (%1)==() GOTO END
   ECHO Value is "%1" and still running...
   SHIFT
GOTO TOP
:END

Loop.bat executed with 3 parameters on the Windows 8.1
Loop.bat executed with 3 parameters on the Windows 8.1

The first two lines are remark statements indicating the name of the program as well as a brief description of what the program does. The third line brings command echoing to a standstill. Line four marks off the label for sub program that a GOTO command could hop to. Line 5 tests for a blank (empty) replaceable parameter. When %1 contains any information then that information gets placed in the first set of parentheses (parameter)==() and evaluates to FALSE. This is what we want. A common mistake is to omit the parentheses, however we need them so we do not create an infinite loop.

When replaceable parameter %1 is empty, the test becomes ()==() and appropriately evaluates to TRUE. This line is the core code of this batch script. When the expression evaluates to TRUE the GOTO END command is executed. This essentially ends the program. When the expression (condition) is FALSE, the next line of code is executed. The sixth line displays the value of parameter combined in message, Value is “something” and still running… using the ECHO command. That next line of code contains the SHIFT command. We know that the SHIFT command shifts the values of replaceable parameters over. The last line is the END label symbolizing the end of the program.

Note that the indents do not change your code and are not mandatory, however it is easier to read code if we indent everything inside the IF block.

The IF command is a handy command by itself, however it yields great possibilities when combined with other commands. Such as the EXIST command. The EXIST command is used to test the existence of a file. This can be done using the following syntax scheme:

IF EXIST command syntax:
IF EXIST {filename} {If filename exist, perform this action}

Lets consider example, where we pass 3 parameters to the batch file, and assume that each of the params is a filename:
:: ExistFile.BAT
:: Example of IF EXIST command sequence.
@ECHO OFF
IF EXIST %1 ECHO File %1 exists!
IF EXIST %2 ECHO File %2 exists!
IF EXIST %3 ECHO File %3 exists!

Example output when passed 3 parameters, and only first file exists. Tested under Windows 8.1
Example output when passed 3 parameters, and only first file exists. Tested under Windows 8.1

Lines 1 and 2 are remark statements indicating the name of the batch file and a brief description of what the batch file does. Line 3 turns off command echoing. Lines 4 through line 6 tests the existence of whatever values are in the replaceable parameters 1, 2, and 3 respectively. This program is rather trivial, but servers as a good example.

Read more in Microsoft help site.

Next article: MS-DOS command redirection operators.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

]]>
http://wishmesh.com/2015/02/if-statement-in-dos-batch-file/feed/ 0
Loops in the Batch files with examples http://wishmesh.com/2015/02/loops-in-the-batch-files-with-examples/ http://wishmesh.com/2015/02/loops-in-the-batch-files-with-examples/#comments Fri, 20 Feb 2015 17:35:03 +0000 http://wishmesh.com/?p=1329 Continue reading Loops in the Batch files with examples ]]> This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed). Previous article: Replaceable parameters in batch file.

WinXP-dir-p

The very simple loop in the batch script can be created by using GOTO command and a label. The syntax is – GOTO followed by the name of the line/label. The label is defined by the colon character at the beginning of the line. The label can be above or below GOTO statement. The code fragment below shows example of infinite loop.

:: LOOP.BAT
:: A batch file example using a infinite loop.

:TOP
DIR
GOTO TOP

The first two lines are remark statements giving the details of the program including the filename and a brief description of the program. The third line marks the line (creates a label) that a GOTO command can possibly link to. The fourth line will perform a directory listing. This is just an example command to illustrate a task that can be performed using the batch loop. You might like to indent the DIR command to emphasize that it is inside a loop. The last line is the command to jump to the line with the name TOP and to continue processing. Since there is no IF statement to test some condition and possibly to jump out of this loop, the batch file will continue to run indefinitely or until you press the CTRL+C or the CTRL+Break combinations on your keyboard.

Loop.bat output under Windows 8.1, Ctrl+C pressed
Loop.bat output under Windows 8.1, Ctrl+C pressed

:: LOOP2.BAT
:: Loop reads multiple replaceable parameters
:: with a SHIFT command inside the loop.

@Echo Off
:TOP
Copy %1 E:\
Shift
Goto TOP

LOOP2.BAT is a more useful program that copies a list of files specified after batch file to the E: drive. When the LOOP2.BAT program runs out of files to copy, the program will not stop. The batch script will attempt to continue until you use CTRL+C or CTRL+Break to terminate the program. To stop the batch script automatically, you need to add IF statement, but we will not discuss it here.

The first three lines are remark statements indicating the name of the batch file and a brief description of the batch file. The fourth line turns off command echoing. Remember that the @ sign turns off the echoing of the ECHO OFF command. The fifth line is the label that the GOTO command will jump to. The sixth line copies the file defined by the first replaceable parameter to the E: (usually flash drive) drive. The seventh line utilizes the SHIFT command to replace %0 with %1, %1 with %2, and so forth. The last line, GOTO TOP, is used to jump to the line labeled TOP and continue processing. Obviously the downside to this looping structure is that it needs more control. As mentioned above, one way to add this control is through the IF statement.

Next article: IF statement in DOS batch file.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

]]>
http://wishmesh.com/2015/02/loops-in-the-batch-files-with-examples/feed/ 0