R
R
Rinat Norezov2019-08-16 10:34:46
cmd/bat
Rinat Norezov, 2019-08-16 10:34:46

How to execute CMD commands in one line?

Script

set /a M=%NUMBER_OF_PROCESSORS%/2
echo %M% > 1.txt

It is executed, but if you try with one line:
set /a M=%NUMBER_OF_PROCESSORS%/2 & echo %M% > 1.txt

Writes in 1.txt "The mode of displaying commands on the screen (ECHO) is disabled."
How to make it work with one line?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wisgest, 2019-08-16
@Renoz

Environment variables are fully supplied in a compound command before it is executed (to see this, you can leave the command output mode on):

set a=1
set a=2& echo %a%

or
set a=1
(
 set a=2
 echo %a%
)

- displays 1.
For delayed variable expansion, use
- in this case, before that, a command must be present in the batch file
or cmd.exemust be launched with a key /v, or the mode of delayed variable expansion by default must be set in the registry (see set /?, setlocal /?, cmd /?).
For delayed expansion of variables, even if the corresponding mode is not enabled, you can also use the command call:
By the way. In arithmetic assignment set /afor delayed expansion of variables, they can be written not as %var%or !var!, but simply var:
set /a M = NUMBER_OF_PROCESSORS / 2

S
Saboteur, 2019-08-16
@saboteur_kiev

SETLOCAL EnableDelayedExpansion & set /a M=%NUMBER_OF_PROCESSORS%/2 & echo !M!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question