Reputation: 3241
In unix command line you can do this
tempvar=ABC mybinary
apparently you can do the same in Windows batch, but in case you need multiple assignments to pass, export
in unix would do the trick. How could you pass multiple variables to mybynary
in a Windows Batch script?
Upvotes: 0
Views: 416
Reputation: 49097
There can be executed help
in a command prompt to get output an incomplete list of Windows commands with a brief description. Any Windows command can be executed with /?
as argument to get output the usage help for the command. The execution of cmd /?
results in output of the usage help of Windows command processor cmd.exe
which is the Windows equivalent for man sh
(or man bash
, man dash
, man ksh
, man zsh
, etc.) in a Linux terminal window.
For most Windows commands, but not all of them, it is also possible to run help
with the command name as argument. help set
and set /?
output both the usage help of the command to define environment variables.
There is additionally:
SS64 offers also comprehensive documentations for other scripting languages on Windows, Linux and Mac.
There are two types of Windows commands:
cmd.exe
like echo
, copy
, set
chcp
, find
, robocopy
The external Windows commands are executables in directory %SystemRoot%\System32
with file extension .com
like chcp.com
(rare) or file extension .exe
like find.exe
, robocopy.exe
and where.exe
(most external Windows commands).
There are on 64-bit Windows two system directories:
%SystemRoot%\System32
contains the 64-bit executables and dynamic linked libraries.%SystemRoot%\SysWOW64
contains the 32-bit executables and dynamic linked libraries.The Windows commands usually exist in both system directories as 32-bit and 64-bit executable on 64-bit Windows. But there are some executables existing only as 64-bit executables.
That is important to know if a 32-bit application starts on 64-bit Windows the Windows command processor cmd.exe
to process a batch file because of the File System Redirector starts in this case 32-bit %SystemRoot%\SysWOW64\cmd.exe
and all commands found via the environment variables PATHEXT
and PATH
in folder %SystemRoot%\System32
being by default the first folder path in system environment variable PATH
are in real searched and executed from directory %SystemRoot%\SysWOW64
in the 32-bit execution environment on 64-bit Windows.
In a command prompt window are entered by a user a command usually with just its file name and so the Windows command processor has to search for a suitable file to execute. For details about this process read: What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
In a batch file can be used also only the file name of an external Windows command, but it is advisable to specify them with full qualified file names for the following reasons:
%SystemRoot%\System32\find.exe
instead of just find
are more fail safe and secure as the batch file does not depend on the environment variables PATHEXT
and PATH
defined outside of the batch file which are quite often modified by programs and users, especially the environment variable PATH
.The external Windows command where
can be used to find out if a Windows command is an internal command or an external command. The execution of where set
in a command prompt window results in output of an error message (hopefully) while where find
results in output of file qualified file name of find
(hopefully found first in %SystemRoot%\System32
with file extension .exe
).
The command to define environment variables on Windows is SET. So this command must be used to define an environment variable which an executable evaluates on execution. An environment variable like tempvar
can be defined as follows:
set tempvar=ABC
set "tempvar=ABC"
The second syntax is highly recommended, but requires enabled command extensions which is the case by default, but which does not mean that command extensions are always enabled. Please read my answer on: Why is no string output with 'echo %var%' after using 'set var = text' on command line? There is explained in full details why set "variable=value"
is the recommended syntax.
The environment variable(s) evaluated by an executable can be defined on separate lines before execution of the executable or on same command line.
Definition of the environment variable and execution of the executable on separate command lines:
set "tempvar=ABC"
mybinary
Definition of the environment variable and execution of the executable on one command line:
set "tempvar=ABC" & mybinary
Single line with multiple commands using Windows batch file explains the operator &
used in the single command line variant.
A batch file writer should avoid replacing one of the predefined Windows environment variables, except a predefined environment variable should be intentionally redefined in a batch script.
Environment variables defined inside a batch file for an executable are still defined after the executable terminated itself. Therefore the environment variables are readable for any other executable started next by cmd.exe
in same batch file or even after batch file processing finished depending on how cmd.exe
was started for processing the batch file and the execution environment defined by the batch file itself.
The Windows command processor uses the Windows kernel library function CreateProcess to run an executable like any other Windows application capable running an executable. cmd.exe
uses always NULL for the CreateProcess
function parameter lpEnvironment
which results in CreateProcess
making a copy of the current environment variables list of cmd.exe
for the executable started next.
The commands SETLOCAL and ENDLOCAL can be used in a batch file to define the environment variables evaluated by an executable just for the executable as the code below demonstrates.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem The two command lines above define completely the execution environment for the
rem batch file and so the batch file does not depend anymore on settings outside of
rem the batch file.
rem Define MyVariable1 and undefine MyVariable2
set "MyVariable1=Demo"
set "MyVariable2="
rem Verify that with an output of all environment variables of
rem which name starts with the string MyVariable with their values.
echo(
echo My variables defined in batch script at top:
echo(
set MyVariable
setlocal
set "MyVariable1=Other value for execuable"
set "MyVariable2=Defined also for executable"
echo(
echo My variables defined for mybinary:
echo(
set MyVariable
if exist mybinary mybinary
endlocal
echo(
echo My variables defined after execution of mybinary:
echo(
set MyVariable
endlocal
echo(
echo My variables as defined outside of the batch file:
echo(
set MyVariable
echo(
pause
I recommend to read this answer in addition to execution of setlocal /?
and endlocal /?
in a command prompt window to get full knowledge on what is done by these two commands in background.
The Windows command processor cmd.exe
can be executed for processing a batch file from within any directory which means the current working directory can be any directory. For that reason it is advisable to reference other files in the directory of the batch file with full path and with file extension instead of just the file name.
The usage of just mybinary
results in searching in current working directory for a file with name mybinary
having one of the file extensions as listed in environment variable PATHEXT
if the environment variable NoDefaultCurrentDirectoryInExePath
is not defined. In other words the successful execution of mybinary
depends on execution environment settings defined outside of the batch file which is not good.
So it is advisable to use "%~dp0mybinary.exe"
in the batch file to reference the executable with full qualified file name whereby %~dp0
references the drive and path (can be also a UNC path) of argument 0 which is the full folder path of the batch file always ending with a backslash and therefore concatenated without an additional backslash with file name mybinary.exe
.
The usage of "%~dp0mybinary.exe"
has following advantages regarding to execution of this executable:
PATHEXT
is defined.NoDefaultCurrentDirectoryInExePath
is defined or not.For completeness regarding usage of %~dp0
see also:
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
Upvotes: 1