For parameters that can be passed when the batch file is called and other variables via the set on Windows 11, 10, ... and MS Server OS!Contents: 1.) ... command line arguments in the command prompt!
|
(Image-1) Command line arguments in the command prompt example |
2.) Use variables in the command prompt cmd.exe!
The other way variables can be initialized is with the 'set' command. The following is the syntax of the set command .EXAMPLE 1:
@echo off
set message=Hello how are you
echo %message%
In the example above, a variable called message is defined and given the value "Hello how are you".
Note that the variable must be enclosed in % signs in order to display the value of the variable.
EXAMPLE 2: Working with Numerical Values
In the batch script it is also possible to define a variable that contains a numerical value. This can be done with the switch / A.
The following code shows an easy way how numeric values can be used with.
@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%
pause
(Image-2) Command line arguments Use numerical values! |
3.) Local and global variables, understand the difference!
In every programming language there is an option to mark variables with a range, that is, the section of code that can be accessed. Typically, variables with a global scope can be accessed anywhere from a program, while variables with a local scope have a defined limit at which they can be accessed.
Example of global and local variables
@echo off
set globalvar = 5
SETLOCAL
set var = 13145
set / A var =% var% + 5
echo% var%
echo% globalvar%
ENDLOCAL
pause
(Image-3) Local and global variables! |
3.) Local and global variables, understand the difference!
In every programming language there is an option to mark variables with a scope, that is, the section of code that can be accessed. Typically, variables with a global scope can be accessed anywhere within a program, while variables with a local scope have a defined boundary at which they can be accessed.
Example global and local variables
@echo off
set globalvar = 5
SETLOCAL
set var = 13145
set /A var = %var% + 5
echo %var%
echo %globalvar%
ENDLOCAL
pause
(Image-3) Local and global variables! |
4.) More about command line arguments in the command prompt etc.?
In the command prompt, also known as the command line or command line, arguments can be used to provide programs with additional information or instructions. These arguments are usually specified after the command and can be used to control the behavior of the program.
Here is a general syntax of how arguments are used in the command prompt:
css:
command [argument1] [argument2] ... [argumentN]
An example of this would be the echo command in Windows:
bash:
echo Hello, world!
In this example, "Hello, world!" the argument passed to the echo command and it simply prints to the screen.
Another example would be a fictional Python script that adds two numbers:
bash:
python add.py 5 10
Here "add.py", "5" and "10" are the arguments passed to the Python script. The script could be written to add these numbers and print the result.
The command prompt can also use special characters such as quotation marks (" or ') to group arguments or allow spaces in a single argument. For example:
bash:
echo "Hello, world!"
Here "Hello, world!" treated as a single argument even though it contains spaces.
It is important to note that argument handling depends on the specific command line environment. In Windows, the syntax and rules may differ from those in Unix-based systems such as Linux or macOS.
5.) Why should you use variables in the command prompt?
In the Windows Command Prompt (cmd.exe), variables can be used to store and exchange values between commands or scripts. Here are some common uses for variables in the Command Prompt:
Manage paths and filenames: You can use variables to store commonly used paths or filenames, which can make typing shorter and improve readability. For example:
set BASE_DIR=C:\MyProject
cd %BASE_DIR%\Subdirectory
Pass parameters for batch scripts: You can use variables to pass parameters to batch scripts. These parameters can then be used within the script to perform specific actions based on the values passed. For example:
@echo off
echo The parameter passed is: %1
Conditional Execution: You can use variables to check certain conditions and execute different commands based on them. For example:
@echo off
set OPTION=ja
if "%OPTION%"=="ja" (
echo The option is activated.
) else (
echo The option is deactivated.
)
Saving results of commands: You can use variables to save results of commands and reuse them later in the script. For example:
@echo off
for /f %%i in ('dir /b *.txt') do (
echo Found file: %%i
)
Capture user input: You can use variables to capture user input during the execution of a script and then use that input in the script. For example:
@echo off
set /p NAME=Enter your name:
echo Hello, %NAME%!
Variables are extremely useful in the Command Prompt to perform complex tasks, manage parameters, check conditions, and increase interactivity with the user.
6.) Further technical information about local and global variables!
Local and global variables are concepts in programming that relate to how variables are defined and used within a program or function.Local Variables:
Local variables are variables that are defined within a specific scope, usually within a function or block of code.
They can only be used within this limited area and are not visible or accessible outside this area.
When a function is executed, local variables are usually created and initialized when the function is called and only last for the duration of the function.
When going out of scope (for example, when exiting the function), local variables are usually destroyed and their memory freed.
Local variables can have the same name as global variables, but within the scope of the function, the local variable takes precedence.
Global Variables:
Global variables are variables that are defined outside of functions or code blocks and are visible and accessible throughout program code.
They are usually defined at the beginning of the program or in a global scope.
Global variables can be read or modified by any function in the program unless they are explicitly declared in the function using the global keyword.
Unlike local variables, global variables exist throughout the program's runtime and are only destroyed when the program exits.
It is important to be careful with global variables because changes to them can be made from anywhere in the code, which can make the code difficult to trace and maintain.
In summary, the main difference between local and global variables is where they are defined, how long they exist, and the scope in which they are visible and accessible. Local variables are limited to the limited scope (e.g. a function), while global variables are visible throughout the program code.
FAQ 103: Updated on: 23 February 2024 06:11