Monday 8 October 2018

windows - Insert line break/special character in string in command line


I would like to do something like this:


set a=dupa;jasiu;karuzela;
set a=%a:;=\n%
echo %a%

to get this:


dupa
jasiu
karuzela

instead of this:


dupa\njasiu\nkaruzela\n

How do I embed line breaks or special characters in string?



The set b=%a:;=^&echo.% solution does not put line breaks in your string but actual &echo. commands.



Answer



The txtechhelp's advice is close:


==> set a=dupa;jasiu;karuzela;

==> echo %a:;=&echo.%
dupa
jasiu
karuzela


==>

However, you need to escape the & ampersand character in a set command using either the general escape character (^ caret):


==> set b=%a:;=^&echo.%

==> echo %b%
dupa
jasiu
karuzela

or using double quotes:


==> set "c=%a:;=&echo.%"

==> echo %c%
dupa
jasiu
karuzela

You could loop over the %a% variable in a for loop.
It's simple if %a% does not contain other delimiter(s) like space, tab, comma or equals sign:


==> for %f in (%a%) do @echo %f
dupa
jasiu
karuzela

==>

Otherwise, if %a% contains some other delimiter(s) like space, tab, comma or equals sign:


==> set a=dupa;jasiu;karu zela;

==> for %f in (%a%) do @echo %f
dupa
jasiu
karu
zela

==> for %f in ("%a:;=";"%") do @if not "%~f"=="" echo.%~f
dupa
jasiu
karu zela

==>

Please note the %f loop parameter (above examples copied & pasted from an open cmd command window).


In a batch file, denominate it properly using doubled percent sign as %%f:


for %%f in (%a%) do echo %%f

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...