Echoing a Url in Batch

Add plus sign to batch file url

That's because + is the url encoding for space.

To encode a plus sign you have to use %2b.

But in batch-files the percent sign is also a special character, therefore it has to be escaped itself by another percent sign.

 https://website.com?phone=%%2B555-123

And the url should be quoted, because when more than one get parameter is present, then these parameters are separated by & signs, that collides again with the special meaning in batch files for command separation.

start "Chrome" chrome --app="https://website.com?phone=%%2B%1&name=John"

Read url of online-link(.url) with batch

There is no need to use the type command; use input redirection (<) to read the file and pass it into the find command.

To capture the last line of the standard output of a command line and store it into a variable (LINE), use a for /F loop:

for /F "tokens=1* delims== eol=" %%I in ('^< "Craft The World.url" find "URL"') do set "LINE=%%J"
echo Last line: "%LINE%"

To process multiple lines of output, do that within the body of the loop, using the for variable reference %%J directly:

for /F "tokens=1* delims== eol==" %%I in ('^< "Craft The World.url" find "URL"') do (
echo Line string: "%%J"
)

The above approaches both split off the URL= prefix by making use of the tokens and delims options of the for /F command.


With your string URL=steam://rungameid/248390, you can kind of misuse the ~ modifies of the for variable reference %%J:

for /F "tokens=1* delims== eol=" %%I in ('^< "Craft The World.url" find "URL"') do (
echo Line string : "%%J"
echo Last element: "%%~nxJ"
)

This works, because the / becomes converted to the standard path separator \, so steam://rungameid/248390 becomes steam:\\rungameid\248390 internally, whereof the last path element is extracted by the ~n and ~x modifiers. The other path modifiers do not return anything useful, because they work reliable only when no / but only \ occur.

How can I echo a newline in a batch file?

echo hello & echo.world

This means you could define & echo. as a constant for a newline \n.

Turn on echo for a single command in a batch file

You can use a small macro (%+@%), it will show the command and then executes it.

@echo off    
call :define_macro

setlocal EnableDelayedExpansion
set var=content

%+@% ver
%+@% ver /?
%+@% echo %var% !var!
exit /b

:define_macro
(set \n=^^^

)
set ^"+@=for %%# in (1 2) do if %%#==2 (%\n%
setlocal EnableDelayedExpansion %\n%
for /F "tokens=1,*" %%1 in ("!time: =0! !argv!") do (%\n%
endlocal%\n%
echo %%1 Execute: %%2%\n%
endlocal%\n%
%%2%\n%
)%\n%
) ELSE setlocal DisableDelayedExpansion ^& set argv="

exit /b

Outputs:

14:04:08,05 Execute: ver

Microsoft Windows [Version 10.0.17134.1069]

14:04:08,05 Execute: ver /?

Displays the Windows version.

VER

14:04:08,05 Execute: echo content !var!

content content

Edited: From the suggestions of sst, I removed the helper function, but to be able to use a custom and nice prefix, I decided against the echo on inside the macro.



Related Topics



Leave a reply



Submit