Search code examples
windowspowershellechofile-get-contents

Hot to paste a text copied from a file using cmd " Get-Content -Raw file_name | clip " to a new file in another directory


I copy a text from .cpp file using the command-line using this command Get-Content -Raw file_name | clip in Windows 10 and I'm trying to paste that text into another file in another directory

I've tried using the cat command to redirect the output to a new file like
cat -Raw file_name > path_to_new_file
it works , but I want to paste the same text to more than one path

the point , is there any keyword or alias that store the text copied earlier , to paste it where I want or is there any keyword or alias return the last copy in the clipboard

Can someone please help me with this issue? How can I paste the copied text to a file in another directory using the command prompt ?

I've tried using the cat command to redirect the output to a new file like
cat -Raw file_name > path_to_new_file
it works , but I want to paste the same text to more than one path


Solution

  • To recall the clipboard contents, use Get-Clipboard:

    # put content in clipboard
    Get-Content -Raw file_name |Set-Clipboard
    
    # retrieve it as many times as you like
    1..10 |ForEach-Object {
        Get-Clipboard
    }
    

    That being said, you don't need to use the clipboard - simply store the file contents in a variable and reference that:

    # put content in variable
    $rawContent = Get-Content -Raw file_name
    
    Get-ChildItem .\path\to\target\files -Filter *.ext |ForEach-Object {
        # append to target file(s)
        $rawContent |Add-Content -LiteralPath $_.PSPath
    }