Copying to clipboard from .txt file via .bat file

13,365

Solution 1

@bgalea's answer is correct, you should use clip in your command.

However, please note that you should put double quotes around the filename, as your script will otherwise break if the filename contains spaces.

Also not that you can also use input < in your code, which instead of piping output from another command, immediately puts the contents into a command. To conclude, the following codes will both work:

@echo off
clip < "c:\Users\Test user\Desktop\paste scripts\ProgramTemplate\ProgramTemplate.txt"

or

@echo off
type "c:\Users\Test user\Desktop\paste scripts\ProgramTemplate\ProgramTemplate.txt" | clip

Solution 2

type file.txt | clip

See type /? and clip /? for help.

See Novice Batch Issue- Creating Files for a list of command prompt punctuation.

Share:
13,365
Evan
Author by

Evan

Updated on June 13, 2022

Comments

  • Evan
    Evan almost 2 years

    I was following this guide but I cannot get it to function properly. It's supposed to copy the text contained in ProgramTemplate.txt to the clipboard when you run the .bat file. Below is the .bat file code:

    C:\\Windows\System32\cmd.exe /k < c:\Users\Test user\Desktop\paste scripts\ProgramTemplate\ProgramTemplate.txt
    c:\exit
    
    • Squashman
      Squashman over 8 years
      You are missing the CLIP command in your code. It shows it plain as day in the website you linked to.
    • Evan
      Evan over 8 years
      I have clip in the file, forgot to type it into this question.
  • Dennis van Gils
    Dennis van Gils over 8 years
    You should put double-quotes around the filename, as I explain in my answer.