Home > database >  CMD: How to echo special command symbol to clipboard?
CMD: How to echo special command symbol to clipboard?

Time:06-02

I need to put this string >>> (it's just some handy copypaste) to clipboard.
Since > is a special cmd-character, I'm using ^ before it to mean all special characters literally.

So far, my Batch code looks like this
(&& pause here is used to see debug messages):

echo ^>^>^> && pause
echo ^>^>^>>"%~dp0foo.txt" && pause
echo foo|clip && pause
echo ^>^>^>|clip && pause

1st line works perfectly (not affecting clipboard though).
2nd line works perfectly (not affecting clipboard either though).
3rd line works perfectly (not using the symbols I need though).
4th line returns >> was unexpected at this time error.

Obviously, I need some syntax tips.

CodePudding user response:

It's a bit tricky, because the pipe creates two new cmd instances (like @aschipf mentioned).

You could use a variable and delayed expansion

set "var=>>>"
cmd /v:on /c "echo(!var!"| clip

Or you can use FOR-variable expansion

set "var=>>>"
( FOR %%X in ("%%var%%") DO @(echo(%%~X^) ) | clip

CodePudding user response:

Okay, I figured an almost decent workaround:

echo ^>^>^>>"%~dp0foo.txt"
type "%~dp0foo.txt"|clip
del "%~dp0foo.txt"
  1. puts >>> into foo.txt right next to your Batch
    (it also accounts for spaces in path to the file via ").
  2. returns >>> as a content from foo.txt and puts it into clipboard.
  3. deletes foo.txt right away.

Still hoping to meet a proper-syntax-based solution.

  • Related