2

I am able to copy multiple lines of text to clipboard with the help of the following script and it works fine.

@echo off
(
echo aaa
echo bbb
echo ccc
)|clip

The only problem is that for some reason at the end of each line it also copies a single space which I than have to delete manually.

Is there any way to prevent this?

2

The answer for Prevent extra whitespace when piping data in a Windows batch script explains your problem perfectly. I will translate those answers to your code. Essentially there are two of the three code examples you can use since you are not dealing with environmental variables.

1) Add an extra CMD /C where you explicitly get the exact behavior you are looking for

cmd /c "echo aaa&echo bbb&echo ccc"|clip

3) Introduce delayed expansion of a linefeed variable - a mind blowing technique developed by jeb

set ^"LF=^
%= This creates a linefeed character =%
"
(echo aaa%%LF%%echo bbb%%LF%%echo ccc%%LF%%)|clip
| improve this answer | |
  • Thanks it worked perfectly. Due to my low reputation I couldn't upvote your answer but I will try to remember this and upvote it sometime later when I have a higher reputation. – Usman Sikander Dec 13 '17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.