Strange output in call echo %var%: "This fail"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Strange output in call echo %var%: "This fail"

#1 Post by Aacini » 12 Jul 2015 10:47

I found a strange behaviour when I assemble an output string with nested percent signs that I later expanded via "call echo %var%":

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "answer=First SecondThird "
set "output=The answer is: %%answer:~^!num^!,6%%"
set /A num=(%random% %% 3) * 6

echo The base output:
echo "%output%"

echo The strange call output:
call echo "%output%"

The output:

Code: Select all

The base output:
"The answer is: %answer:~0,6%"
The strange call output:
This fail

I assume this error is caused by the parsing phases. Is there any way to achieve this output?

Antonio

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Strange output in call echo %var%: "This fail"

#2 Post by jeb » 12 Jul 2015 11:08

Hi Aacini,

I tested it (Win7x64) but I get the expected outputs.

The base output:
"The answer is: %answer:~6,6%"
The strange call output:
"The answer is: Second"

C:\temp>test
The base output:
"The answer is: %answer:~6,6%"
The strange call output:
"The answer is: Second"

C:\temp>test
The base output:
"The answer is: %answer:~0,6%"
The strange call output:
"The answer is: First "

C:\temp>test
The base output:
"The answer is: %answer:~0,6%"
The strange call output:
"The answer is: First "


Did you run it in a fresh cmd window?
Did you get the text "This fail", a syntax error or nothing?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Strange output in call echo %var%: "This fail"

#3 Post by aGerman » 12 Jul 2015 11:33

I don't have any problems on Win7 x86.

Code: Select all

C:\>test.bat
The base output:
"The answer is: %answer:~6,6%"
The strange call output:
"The answer is: Second"

C:\>test.bat
The base output:
"The answer is: %answer:~6,6%"
The strange call output:
"The answer is: Second"

C:\>test.bat
The base output:
"The answer is: %answer:~0,6%"
The strange call output:
"The answer is: First "

C:\>

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Strange output in call echo %var%: "This fail"

#4 Post by Aacini » 12 Jul 2015 12:01

Wow! I use Windows 8.1 64-bits Spanish version. I restarted the OS, open a new cmd.exe session and test my program. This is the output:

Image

:shock: :!:

Antonio

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Strange output in call echo %var%: "This fail"

#5 Post by jeb » 12 Jul 2015 12:05

Then you simply should delete your echo.bat file :D

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Strange output in call echo %var%: "This fail"

#6 Post by OperatorGK » 12 Jul 2015 12:10

jeb wrote:Then you simply should delete your echo.bat file :D

I think it's external program somewhere in %PATH%. Nobody in clear mind will create such a batch file!
To distinguish them, append "pause" to the end. If it's echo.bat, program won't pause, but if it's echo.exe, it will pause :!: .
Last edited by OperatorGK on 12 Jul 2015 12:15, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Strange output in call echo %var%: "This fail"

#7 Post by Aacini » 12 Jul 2015 12:14

jeb wrote:Then you simply should delete your echo.bat file :D

Yes of course!!!! This is the reason! :oops:

Thanks, jeb

Antonio

EDIT: PS - I created the echo.bat file precisely to test a certain point some time ago, and forgot it...
Last edited by Aacini on 12 Jul 2015 12:21, edited 1 time in total.

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Strange output in call echo %var%: "This fail"

#8 Post by OperatorGK » 12 Jul 2015 12:16

Aacini wrote:
jeb wrote:Then you simply should delete your echo.bat file :D

Yes of course!!!! This is the reason! :oops:

Thanks, jeb

Antonio

:?: :?
Please explain why you created this file. I thought nobody familiar with cmd.exe would do so.
What is the point?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Strange output in call echo %var%: "This fail"

#9 Post by jeb » 12 Jul 2015 14:11

Aacini wrote:Yes of course!!!! This is the reason! :oops:

I only guess it, as I have the same problems with incredibly behaviour so many times before :wink:

Now I use in my "echo.bat" and "set.bat" files always code like this

Code: Select all

@echo **** This is %0 ****


OperatorGK wrote:Please explain why you created this file. I thought nobody familiar with cmd.exe would do so.
What is the point?

Aacini is one of the persons who are very familiar with cmd.exe :!:

And this is the way to test how CALL works or which echo style creates disk accesses.

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

Re: Strange output in call echo %var%: "This fail"

#10 Post by dbenham » 12 Jul 2015 15:40

jeb wrote:Now I use in my "echo.bat" and "set.bat" files always code like this

Code: Select all

@echo **** This is %0 ****


You might try the following instead:

Code: Select all

@echo **** This is %~f0 ****


Dave Benham

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Strange output in call echo %var%: "This fail"

#11 Post by OperatorGK » 13 Jul 2015 01:37

Aah, thank you. I remember topic where you try to find safe echo method.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Strange output in call echo %var%: "This fail"

#12 Post by Meerkat » 24 Jul 2015 04:51

Hope this helps...

Code: Select all

@echo off
set "var=This worked"
set "caller=var"

call :do-the-thing echo %%%caller%%%
pause>nul
exit /b

:do-the-thing
%* & goto :EOF


Output:

Code: Select all

This worked


:wink:

Post Reply