This is the code
echo https://www.bing.com/search?PC=U523&q=%random%&pglt=129&FORM=ANNTA1&DAF0=1|clip
The code copies the Microsoft Edge search link at random,using the %random%
tool and the clip
tool to copy the link to the clipboard.
The problem is that when I put the code in cmd I get this message
https://www.bing.com/search?PC=U523
'q' is not recognized as an internal or external command,
operable program or batch file.
'pglt' is not recognized as an internal or external command,
operable program or batch file.
'FORM' is not recognized as an internal or external command,
operable program or batch file.
'DAF0' is not recognized as an internal or external command,
operable program or batch file.
The main issue in your code is the presence of ampersands &
, which constitute command concatenation operators.
Usually escaping special characters (like ^&
) is enough, but in this situation a pipe |
is involved, which instantiates new cmd.exe
instances for either side1, which then receive unescaped characters. For this reason, you need to establish double-escaping, like this:
echo https://www.bing.com/search?PC=U523^^^&q=%random%^^^&pglt=129^^^&FORM=ANNTA1^^^&DAF0=1| clip
However, composing escape sequences depending on whether or not pipes are involved is quite nasty, but there is a nice option, namely delayed variable expansion. For this, the string first obviously needs to be assigned to a variable:
rem // Store string to variable; use quoted `set` syntax to protect special characters:
set "URL=https://www.bing.com/search?PC=U523&q=%random%&pglt=129&FORM=ANNTA1&DAF0=1"
rem // Explicitly instantiate a new `cmd.exe` instance with delayed expansion enabled:
cmd /V:ON /D /C echo(!URL!| clip
1) No new instance is created for each side of the pipe that just contains a pure external command (like clip.exe
).