Dos Batch Read File and Pass Arguments

Batch Script - Variables


There are two types of variables in batch files. 1 is for parameters which can be passed when the batch file is chosen and the other is washed via the set command.

Command Line Arguments

Batch scripts support the concept of command line arguments wherein arguments can be passed to the batch file when invoked. The arguments can be called from the batch files through the variables %1, %2, %3, and so on.

The following example shows a batch file which accepts 3 control line arguments and echo's them to the command line screen.

@repeat off  echo %1  repeat %ii  repeat %iii        

If the above batch script is stored in a file called examination.bat and we were to run the batch as

Test.bat 1 2 three        

Following is a screenshot of how this would expect in the command prompt when the batch file is executed.

Command Line Arguments

The above control produces the following output.

1  2  three        

If we were to run the batch as

Example 1 2 3 4        

The output would still remain the same as above. Nonetheless, the fourth parameter would exist ignored.

Set Command

The other manner in which variables tin can be initialized is via the 'prepare' command. Following is the syntax of the set command.

Syntax

set /A variable-name=value        

where,

  • variable-name is the name of the variable you want to set.

  • value is the value which needs to be gear up against the variable.

  • /A – This switch is used if the value needs to be numeric in nature.

The following case shows a simple fashion the set command can be used.

Instance

@echo off  fix message=Hello Globe  echo %message%        
  • In the above code snippet, a variable called message is defined and set with the value of "Hullo World".

  • To display the value of the variable, note that the variable needs to exist enclosed in the % sign.

Output

The higher up command produces the post-obit output.

Hello World        

Working with Numeric Values

In batch script, it is also possible to ascertain a variable to hold a numeric value. This can be done by using the /A switch.

The following code shows a uncomplicated fashion in which numeric values can exist set with the /A switch.

@echo off  Prepare /A a = v  SET /A b = 10  Prepare /A c = %a% + %b%  echo %c%        
  • Nosotros are outset setting the value of 2 variables, a and b to 5 and 10 respectively.

  • We are adding those values and storing in the variable c.

  • Finally, we are displaying the value of the variable c.

The output of the above programme would be 15.

All of the arithmetic operators work in batch files. The following instance shows arithmetic operators can exist used in batch files.

@echo off  SET /A a = 5  SET /A b = 10  SET /A c = %a% + %b%  repeat %c%  SET /A c = %a% - %b%  echo %c%  Set up /A c = %b% / %a%  echo %c%  Set up /A c = %b% * %a%  echo %c%        

The above command produces the following output.

fifteen  -5  2  50        

Local vs Global Variables

In any programming language, there is an option to mark variables as having some sort of scope, i.e. the section of code on which they tin can be accessed. Normally, variable having a global scope tin can exist accessed anywhere from a program whereas local scoped variables take a defined boundary in which they tin can be accessed.

DOS scripting too has a definition for locally and globally scoped variables. By default, variables are global to your unabridged command prompt session. Call the SETLOCAL command to make variables local to the scope of your script. Afterwards calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL, calling Go out, or when execution reaches the end of file (EOF) in your script. The following example shows the divergence when local and global variables are set in the script.

Example

@echo off  set globalvar = 5 SETLOCAL prepare var = 13145 gear up /A var = %var% + 5 repeat %var% echo %globalvar% ENDLOCAL        

Few key things to note about the above program.

  • The 'globalvar' is defined with a global scope and is available throughout the entire script.

  • The 'var' variable is divers in a local scope because information technology is enclosed betwixt a 'SETLOCAL' and 'ENDLOCAL' block. Hence, this variable will be destroyed as before long the 'ENDLOCAL' statement is executed.

Output

The above command produces the post-obit output.

13150 5        

Y'all will observe that the command echo %var% will non yield anything because later on the ENDLOCAL statement, the 'var' variable volition no longer exist.

Working with Environment Variables

If you have variables that would be used across batch files, and so information technology is always preferable to use environment variables. Once the environment variable is defined, it tin be accessed via the % sign. The following case shows how to see the JAVA_HOME defined on a system. The JAVA_HOME variable is a key component that is normally used by a broad variety of applications.

@echo off  repeat %JAVA_HOME%        

The output would bear witness the JAVA_HOME directory which would depend from system to system. Following is an case of an output.

C:\Atlassian\Bitbucket\four.0.1\jre        

hillwhight.blogspot.com

Source: https://www.tutorialspoint.com/batch_script/batch_script_variables.htm

0 Response to "Dos Batch Read File and Pass Arguments"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel