The character ^
is interpreted by the Windows Command Processor cmd.exe
as escape character on being outside a double quoted argument string.
There can be used following code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "InputData=RAY5557"
set "OutputData="
for /F delims^=^ eol^= %%G in ('"echo %InputData%| java.exe %Java_scripts%Javaout_2.java"') do set "OutputData=%%G"
setlocal EnableDelayedExpansion & echo OutputData: !OutputData!& endlocal
endlocal
delims^=^ eol^=
is interpreted by cmd.exe
as delims= eol=
which defines an empty list of delimiters and no end of line character. That special syntax is necessary to avoid that the captured output is neither split up into substrings using normal space and horizontal tab as delimiters and ignore the line on first substring begins with a semicolon.
There cannot be used "delims= eol="
as that would result in definition of an empty list of delimiters, but "
as end of line character. "eol= delims="
would define a space character as end of line character and an empty list of delimiters.
A horizontal tab, a normal space, a comma, a semicolon, an equal sign, and an OEM encoded no-break space with decimal code point value 255 are interpreted as argument string separators outside of a double quoted argument string. That is the reason for the definition of the two for /F
options unusually without the surrounding "
with escaping the two equal signs and the normal space for getting them interpreted as literal characters and not as argument string separators.
Please read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains in full details why the syntax set "variable=value"
is highly recommended on (re)definition of an environment variable. The captured output data is assigned completely to the loop variable G
which is used to define the environment variable OutputData
with the output data which was explicitly undefined before. The entire argument string of command SET is enclosed in "
resulting in interpreting ^
in the output data as literal character.
It is important that delayed expansion is disabled as otherwise the part set "OutputData=%%G"
would be parsed a second time after %%G
is replaced by the string assigned to the loop variable which would result in any exclamation mark being interpreted as beginning/end of a delayed expanded variable reference causing an unwanted modification of the string value finally assigned to the environment variable OutputData
.
The output of the string assigned to the environment variable OutputData
without usage of surrounding "
, which the command ECHO would also output, requires enabled delayed variable expansion to prevent replacing first the variable reference %OutputData%
by the string value assigned to the environment variable and interpreting next ^
as escape character. See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
There could be used also just set OutputData
instead of setlocal EnableDelayedExpansion & echo OutputData: !OutputData!& endlocal
to output all environment variables of which name begins case-insensitive with OutputData
with name, equal sign and the assigned value on one such an environment variable is defined at all.
Note: FOR /F respectively cmd.exe
processing the batch file runs in this case one more cmd.exe
in background using %ComSpec% /c
and the command line inside '
of the FOR /F command line as additional arguments. That is important to know here because this means the entire for /F
command line must be of valid syntax for two cmd.exe
– the one processing the batch file and the other one started in background which executes the command line and of which output to handle STDOUT (standard output) is captured by cmd.exe
processing the batch file for processing it with the FOR loop on additionally executed cmd.exe
closed itself finally after finishing the command line execution.
There is not shown how the environment variable Java_scripts
is defined by the real batch file. The posted code may not work if the string value of the variable Java_scripts
contains a space or one of these characters &(){}^=;!'+,`~
.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
cmd /?
echo /?
endlocal /?
for /?
set /?
setlocal /?
I recommend to read also: