Arithmetic operations
The command line is not designed for handling mathematical functions but it is possible to do some very simple integer arithmetic with variables. A switch ” /a” was added to the “set” command to allow for some basic functions. Primarily, the use is adding and subtracting.
For example, it is possible to increment or decrement counters in a loop. In principle, it is also possible to do multiplication and division.but only whole numbers can be handled so the practical use is limited. Although variables are stored as strings, the command interpreter recognizes strings that contain only integers, allowing them to be used in arithmetic expressions.
The syntax is set /a some_variable={arithmetic expression}The four arithmetic operators are shown in Table I.
| Symbol | Operation |
|---|---|
| Addition | |
| – | Subtraction |
| * | Multiplication |
| / | Division |
Here is an example of a variable %counter% being incremented:set /a counter=%counter% 1This can also be written as:set /a counter =1
How to combine two variables in cmd.exe script
In a cmd.exe script on Windows 7, I want to combine two variables to set a third.
Eg. (which doesn’t work)
SET THIRD=%FIRST%%SECOND%How can I do this ?
Apologies to everyone who even looked at this question. The example above works perfectly and I cannot reproduce the problem I was having. Thanks for pointing out my error.
Variables in comparison statements in batch files
Variables are often used in comparisons in conditional statements in batch files. Some of the comparison operators that are used are given in Table I of the page on “If” statements. Because of the somewhat loose way that the command line treats variables, it is necessary to be careful when comparing variables.
For strings, the safest way is to quote variables. For example: if “%variable1%” == “%variable2%” some_command
Back to top
Пример глобальной переменной (global variables):
Переменные объявлены в файле Batch, и не находятся в блоке команд setlocal .. endlocal, будут глобальными переменными. Они могут быть использованы в других файлах в одном и том же сеансе работы (Session).
В данном примере у нас есть 2 файла batchFile1.bat и batchFile2.bat. Переменная MY_ENVIRONMENT определяется в файле 1, и используется в файле 2.
Открыть окно CMD, и CD к папке содрежащей файл batchFile1.bat, batchFile2.bat, запустить поочередно каждый файл.
Windows позволяет вам создать переменные среды, данные переменные будут глобальными переменными, которые можно использовать в любых файлах batch.
В Windows выберите:
- Start Menu/Control Panel/System
- Advanced (Tab) > Environment Variables..
Пример локальной переменной (local variables)
Локальные переменные (Local variable) объявлены в блоке, начинаются с setlocal и заканчиваются на endlocal.
How do i get the result of a command in a variable in windows?
I would like to add a remark to the above solutions:
All these syntaxes work perfectly well IF YOUR COMMAND IS FOUND WITHIN THE PATH or IF THE COMMAND IS A cmdpath WITHOUT SPACES OR SPECIAL CHARACTERS.
But if you try to use an executable command located in a folder which path contains special characters then you would need to enclose your command path into double quotes (“) and then the FOR /F syntax does not work.
Examples:
$ for /f "tokens=* USEBACKQ" %f in ( `""F:GLW7DistribSystemShells and scriptingf2ko.defolderbrowse.exe"" Hello '"F:GLW7DistribSystemShells and scripting"'`
) do echo %f
The filename, directory name, or volume label syntax is incorrect.or
$ for /f "tokens=* USEBACKQ" %f in ( `"F:GLW7DistribSystemShells and scriptingf2ko.defolderbrowse.exe" "Hello World" "F:GLW7DistribSystemShells and scripting"`
) do echo %f
'F:GLW7DistribSystemShells' is not recognized as an internal or external command, operable program or batch file.or
`$ for /f "tokens=* USEBACKQ" %f in ( `""F:GLW7DistribSystemShells and scriptingf2ko.defolderbrowse.exe"" "Hello World" "F:GLW7DistribSystemShells and scripting"`
) do echo %f
'"F:GLW7DistribSystemShells and scriptingf2ko.defolderbrowse.exe"" "Hello' is not recognized as an internal or external command, operable program or batch file.In that case, the only solution I found to use a command and store its result in a variable is to set (temporarily) the default directory to the one of command itself :
pushd "%~d0%~p0"
FOR /F "tokens=* USEBACKQ" %%F IN ( `FOLDERBROWSE "Hello world!" "F:GLW7DistribSystemLayouts (print,display...)"`
) DO (SET MyFolder=%%F)
popd
echo My selected folder: %MyFolder%The result is then correct:
My selected folder: F:GLW7DistribSystemOS install, recovery, VM
Press any key to continue . . .Of course in the above example, I assume that my batch script is located in the same folder as the one of my executable command so that I can use the “%~d0%~p0” syntax. If this is not your case, then you have to find a way to locate your command path and change the default directory to its path.
NB: For those who wonder, the sample command used here (to select a folder) is FOLDERBROWSE.EXE. I found it on the web site f2ko.de (http://f2ko.de/en/cmd.php).
If anyone has a better solution for that kind of commands accessible through a complex path, I will be very glad to hear of it.
Gilles



