1

I need help on the batch scripts as below.

I got a command to echo a string to a Java program

setlocal DisableDelayedExpansion

set InputData=RAY5557

echo %InputData%| java %Java_scripts%Javaout_2.java

The output will be as below:

!For Ray(^5557^)!

I want to set the output so that I can use the output to pass the result to another task from the command "echo %InputData%| java %Java_scripts%Javaout_2.java"

How can I do it?

If I set the output to tmp and echo %tmp%, it is missing the ^ symbol:

setlocal DisableDelayedExpansion

set InputData=RAY5557

for /f "delims="  %%G in ( '"echo %InputData%| java %Java_scripts%Javaout_2.java"') do (set tmp=%%G)

echo tmp: %tmp%

Output:

tmp: !For Ray(5557)!

I need the output as

!For Ray(^5557^)!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chiaomin
  • 13
  • 4
  • To make data and code blocks easier to read you should place a line of three backquotes before and after the block. That will render the block "literally" making it easier to interpret. Beyond that I can't really make any sense of your problem. You wrote "if i set the output to temp and echo %temp%" but nowhere else in your question does `%temp%` appear. I also don't see anything in your program that explains where the carets come from. – Kurtis Rader Jul 22 '23 at 06:08
  • Caret (`^`) is the escape character for shell, kind of like backslash in Unix. You might change your Java to produce two carets instead. – Tim Roberts Jul 22 '23 at 06:15
  • `tmp` and `temp` are special variable names that refer to where temporary files are located, and should not be changed. You should change that variable-name. Try using `set varname` instead of `echo` - it should show the carets. To use `echo`, try `echo %varname:^=^^%` – Magoo Jul 22 '23 at 06:23
  • Hi @Magoo , i follow your suggestion and its works . **echo %varname:^=^^%**. Can you advice, what the it means? if the output contain **^** or **|** and other symbol, how should i make the changes ? – chiaomin Jul 22 '23 at 08:39
  • Caret is an escape character - it turns off the special meaning of the following character (except for `%` for which the escape is another `%`). See `set /?` from the prompt for documentation about the `%var:...%` syntax. That particular application substitutes two carets for each single caret in the variable. Generally, <|>&^ require to be escaped where the character is used literally. – Magoo Jul 22 '23 at 12:11
  • How is this not a duplicate? – Peter Mortensen Jul 24 '23 at 09:10

1 Answers1

0

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143