Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Command Prompt - Windows 11, 10, etc. ► ««« »»»

Command line arguments in the command prompt with examples!


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!
2.) ... use variables in the command prompt cmd.exe!
3.) ... Local and global variables, understand the difference!



4.) ... More about command line arguments in the command prompt etc.?
5.) ... Why should you use variables in the command prompt?
6.) ... Further technical information about local and global variables!




Everyone who likes to use variables and work with arguments is familiar with these possibilities and likes to benefit from the advantages under all operating systems and not only under Microsoft Windows 11/10 and MS Server OS!  


1.) Command line arguments in the command prompt!

Batch Scripts / Command Prompts support command line arguments where arguments can be passed to the batch file when invoked. The arguments can be called from the batch files via the variables %1,%2,%3, ... etc.



@echo off 
echo %1 
echo %2 
echo %3
pause


If the above batch script is saved in a file called test.bat and execute the batch, for example via a Windows shortcut and use arguments! 
 

Test.bat one two three



When the batch file is executed, these values ​​can be output 

( ... see Image-1 Point 1 to 3)

(Image-1) Command line arguments in the command prompt example
Command line arguments in the command prompt example

-
▲ Back to the top ▲




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!
Command line arguments Use numeric values!

-
▲ Back to the top ▲




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!
Local and global variables!

-
▲ Back to the top ▲




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!
Local and global variables!

-
▲ Back to the top ▲


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 Windows
Windows-Console

Connection to remote desktop service via command prompt!


It is easy to start a connection to the Remote Desktop Service via the command prompt with arguments on Windows 11, 10, and MS Server OS   Contents:
Windows-Console

Waiting for keyboard input Command prompt Script?


Often you want to stop the script in the batch file and wait for the user to press Enter, or variables to process on Windows 11, 10, and MS Server OS
Windows-Console

Files that are younger than the date can be deleted via the command line!


It is quite easy to delete files that are younger than a certain date via the command line under Windows 11, 10, and MS Server OS Everyone probably
Windows-Console

Delete all files except the most recent via command line, script or CMD.EXE?


It is easy to delete all files except the most recent via command line, script or CMD.EXE for Windows 11, 10, and MS Server The most popular script
Windows-Console

Ability to batch rename files in lower and upper case?


It is easy to rename files in batches using a script or command prompt in lower and / or upper-case letters on Windows 11, 10, and MS Server OS Everyone
Windows-Console

Serial number of the hard disk under Windows 11, 10, ... via command prompt!


It is quite easy to find out the serial number of your hard disk under Windows 11, 10, or MS Server 2022, at a command prompt There are free hard
Windows-Console

Find out the PC serial number and manufacturer via command prompt?


Using the command prompt, it is easy to find out the PC serial number and manufacturer under Windows 11, 10, and MS Server OS Everyone has ► read out

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: windows, 11, 10, console, command, line, arguments, prompt, examples, parameters, passed, batch, file, variables, contents, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + 4ur-Windows-8-Mouse-Balls
  + 12-Ants
  + Q-Dir
  + PaintOkay
  + DirPrintOK
  + FontViewOK
  + MeinPlatz
  + DesktopOK
  + IsMyMemoryOK
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | gc24b | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu


► Who wants to see soccer ball as mouse tracks on the desktop! ◄
► Auto Save the Desktop Symbol for Windows! ◄
► A Purple File Explorer on Windows-10/11 Example! ◄
► Auto power options adjustment, increase energy efficiency on MS Windows 11, 10, ... ◄


This website does not store personal data. However, third-party providers are used to display ads,
which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF).
The CMP ID is 300 and can be individually customized at the bottom of the page.
more Infos & Privacy Policy

....