Trailing Spaces in code block

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Trailing Spaces in code block

#1 Post by Squashman » 07 Dec 2017 18:00

This is in reference to this StackOverFlow Question.

It seems that using a parenthesized code block causes a trailing space in the output.

Code: Select all

@Echo off
(echo 1
echo AZ)|findstr "1 AZ">>spaces.txt
(echo AZ)|findstr "1 AZ">>spaces.txt
echo AZ|findstr "1 AZ">>Nospaces.txt
pause
If we watch the code execute we see this.

Code: Select all

C:\Users\Squashman\Desktop>so

C:\Users\Squashman\Desktop>(
echo 1
 echo AZ
)  | findstr "1 AZ" 1>>spaces.txt

C:\Users\Squashman\Desktop>(echo AZ )  | findstr "1 AZ" 1>>spaces.txt

C:\Users\Squashman\Desktop>echo AZ  | findstr "1 AZ" 1>>Nospaces.txt

C:\Users\Squashman\Desktop>pause
Press any key to continue . . .
The user in the SO Question is trying to pipe answers to their program to automate it but the problem arises because it sees the space and can't find the file because of it.

I am thinking that their only other possible solution would be to create a temp file with the 1 and AZ on each line and execute the program like so:

Code: Select all

Z:\Models\LossCalc.exe<tempfile.txt
I can't think of another way to create a standard input stream without the spaces occurring because of the code block.

Or am I just completely wrong on this?
Would creating the Carriage Return and Line Feed variables work?

Code: Select all

echo 1!CR!!LF!AZ!CR!LF!|Z:\Models\LossCalc.exe

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Trailing Spaces in code block

#2 Post by penpen » 07 Dec 2017 21:05

This is a pretty overcomplicated approach...:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "prompt=$_1$_AZ$_ "
set "space= "
(
	echo on
	set "prompt=$_1$_AZ$_ "
	for %%a in ("") do (@rem:)
	echo off

) | > spaces.txt findstr /v /r /c:"^$" /c:"^ "
endlocal
goto :eof
penpen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Trailing Spaces in code block

#3 Post by dbenham » 07 Dec 2017 21:56

The problem is indeed an artifact of the pipe parser when dealing with a parenthesized block of code.

Remember that each side of the pipe is executed in a new process via CMD /C, as described by jeb at https://stackoverflow.com/a/8194279/1012053.

When a multi-line parenthesized block gets piped, the parser must repackage the entire block into a single line so that it can be executed via CMD /C

Code: Select all

(
  echo line 1
  echo line 2
) | findstr "^"
The left side of the pipe is executed as:

Code: Select all

C:\WINDOWS\system32\cmd.exe  /S /D /c" ( echo line 1 & echo line 2 )"
You can see the extra spaces that are introduced.
Even if your code is already on a single line, it still goes through the same parser that introduces those pesky spaces.

Code: Select all

(echo line 1&echo line 2)|findstr "^"
Gives the same result.

I know of three relatively simple solutions that eliminate the unwanted spaces without the need for a temporary file.

1) Add an extra CMD /C where you explicitly get the exact behavior you are looking for

Code: Select all

cmd /c "echo line 1&echo line 2"|findstr "^"
2) Store part of the command in a variable and delay expansion until execution of CMD /C

Code: Select all

set "cmd=&echo line 2"
echo line 1%%cmd%%|findstr "^"
3) Introduce delayed expansion of a linefeed variable - a mind blowing technique developed by jeb that he describes in that same SO link that I provided

Code: Select all

set ^"LF=^
%= This creates a linefeed character =%
"
(echo line 1%%LF%%echo line 2%%LF%%)|findstr "^"

Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Trailing Spaces in code block

#4 Post by Squashman » 07 Dec 2017 22:19

Thanks Dave,
Was bothering me all day. Kept searching and testing stuff all day in my free time but could not figure it out. I was close though. I was using something similar to Jeb's solution but was trying it with Delayed Expansion and that was not working.

Hope you post your answer over on SO.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Trailing Spaces in code block

#5 Post by dbenham » 07 Dec 2017 22:21

Done :)

Post Reply