Base on the idea & method from npocmaka & jeb.
Another way was found to solve the error of IF command in piped code.
TO ADD AN ESCAPE SPACE AT RIGHT POSITION
For EQU NEQ LSS LEQ GTR or GEQ,
ONE ^ has to be added after 1st compared string/number,
At least TWO SPACE have to be added between this ^ and EQU NEQ LSS LEQ GTR or GEQ.
Example,
echo pipe|IF 1234^ GTR 124 echo true
true can be replaced by %^cmdcmdline% to see the parser.
(IF 1234^ NEQ 124 echo right)|more
For IF DEFINED & IF EXIST
- At least ONE SPACE has to be added after
IF DEFINED or IF EXIST,
- At least TWO SPACE have to be added before
variable or file/folder,
- ONE ^ has to be added between above added SPACE.
Example,
echo pipe|IF DEFINED ^ username echo true
echo pipe|IF EXIST ^ c:\windows echo true
true can be replaced by %^cmdcmdline% to see the parser
(IF DEFINED ^ username echo right)|more
(IF EXIST ^ c:\windows echo right)|more
But so far I didn't know why all of them have to work like above.
Something interesting,
echo pipe|if 123^ equ ====== 123 ====== echo %^cmdcmdline%
All = are ignored.
Updated (2016-04-23),
As npcmaka mentioned, =,, are also delimiters.
CASE 1
echo pipe|if not defined^===neq === echo %^cmdcmdline%
Outputs,
C:\Windows\system32\cmd.exe /S /D /c" if not defined=== neq echo %cmdcmdline%"
CASE 2
echo pipe|if not defined^ === neq === echo %^cmdcmdline%
Outputs,
C:\Windows\system32\cmd.exe /S /D /c" if not defined == = neq === echo %cmdcmdline%"
One space appears between ===.
CASE 3
echo pipe|if not defined^,,,neq ,,, echo %^cmdcmdline%
Outputs,
C:\Windows\system32\cmd.exe /S /D /c" if not defined,NEQ echo %cmdcmdline%"
Some phenomenon can be found, which may be known how to parse...
neq is identified as variable in all CASE. (do set neq=blah before will occur no output for all CASE)
In CASE 2, == in Delimiter === seems be identified as comparison and auto be added one space after
According to the rule in CASE 2, in CASE 3, input neq is lowercase and output NEQ is uppercase. But in CASE 1 & 2, due to existed == before neq, this time, neq is kept in lowercase, which is not identified as comparison.
So it has several steps in parser period. But has some bugs of adding or deleting delimiters. Right?
CASE 4
The next code seems trigger cmd executing infinitely,
echo pipe|if not defined^ == echo %^cmdcmdline%
it runs like if not "defined" == "echo" recursive command