Text with asterisk ignored in function For loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Text with asterisk ignored in function For loop

#1 Post by Jer » 16 Nov 2015 00:04

My working code gets user input that may include an asterisk in the text.
The code below demonstrates the issue of passing text that includes an asterisk
to a function and using a For loop to echo the text.

The function args (%*) echo okay, but the For loop ignores text with an asterisk.
Looking for a solution. Thanks.

Code: Select all

@Echo Off

setlocal EnableDelayedExpansion

Call:pFunc "Product List, *=Discontinued"
Call:pFunc "Aisle # & Shelf"

endlocal&goto:eof

rem function
:pFunc
 setlocal EnableDelayedExpansion

 For %%a In (%*) Do Echo %%~a

 endlocal&exit /b

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

Re: Text with asterisk ignored in function For loop

#2 Post by penpen » 16 Nov 2015 06:14

You could access the string via a variable content:

Code: Select all

@Echo Off

setlocal EnableDelayedExpansion

Call:pFunc "Product List, *=Discontinued"
Call:pFunc "Aisle # & Shelf"

endlocal&goto:eof

rem function
:pFunc
 setlocal EnableDelayedExpansion
 set args=%*
 For %%a In (args) Do Echo !%%~a!

 endlocal&exit /b


penpen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Text with asterisk ignored in function For loop

#3 Post by Jer » 16 Nov 2015 10:58

With this change:

Code: Select all

For %%a In (args) Do Echo !%%~a:~1,-1!

the surrounding quotes are removed from the text string and the solution works.
Thanks again penpen :)

Post Reply