2

If I have a file in the clipboard I can paste it to a directory using ctrl+v or right click + paste. Is there a way to execute the paste through command line?

7
  • What exactly do you want to execute through the command line? How is right mouse click not executing a paste?
    – Ramhound
    Commented Feb 21, 2020 at 12:08
  • I want to execute a paste command through MATLAB using a system command
    – Afzal
    Commented Feb 21, 2020 at 12:52
  • So you want to take the contents of the clipboard and execute a paste command thus creating a file?
    – Ramhound
    Commented Feb 21, 2020 at 13:01
  • Yes, I can do this in VBA as CreateObject("Shell.Application").Namespace(CVar(DestinationFolder)).self.InvokeVerb "Paste"
    – Afzal
    Commented Feb 21, 2020 at 13:36
  • I'm trying to do the same through MATLAB objShell = actxserver('shell.application'); objFolder = objShell.NameSpace(DestinationFolder); objFolderItem = objFolder.Self; but 'Self' is not recognised
    – Afzal
    Commented Feb 21, 2020 at 13:40

2 Answers 2

2
  • Option #1

Why not at the // to run the one-line command to do this...

1) Copy one or more files to ClipBoard

2) Set destination drive\folder: Copy-Item -Destination D:\Folder_Target

  • For paste your file in %temp% folder:
powershell --NoProfile -command "Get-Clipboard -Format FileDropList | Copy-Item -Destination $env:temp"

powershell --NoProfile -command "Get-Clipboard -Format FileDropList | Copy-Item -Destination $env:temp"
  • Or using alias
powershell -nOp -c "gcb -Format FileDropList | cpi -Destination $env:temp -PassThru"

  • Option #2
  • You can also do this with the paste code in a hybrid / and file.

Where / with paste the code, it will be compiled and executed at run time.

  • Usage: paste.bat D:\folder\target\
/* & @cls & @echo off & title <nul & title %~nx0: Past File to: "%~1" & setlocal enabledelayedexpansion

2>nul >nul del /q /f "%tmp%\TSPaste2.exe" & for /f tokens^=* %%c in ('%__APPDIR__%where.exe /r "c:\Windows\Microsoft.NET" csc.exe
')do "%%~c" /t:exe /out:"%tmp%\TSPaste2.exe" "%~f0" /platform:anycpu /unsafe+ /w:0 /o /nologo && goto :next

echo/Error: Check/edit ccs.exe command line/flags^!! && endlocal && goto :EOF

:next 
"%tmp%\TSPaste2.exe" "%~1" & del /q /f "%tmp%\TSPaste2.exe" & endlocal & goto :EOF && rem./ 2>nul >nul */

// C# code by @Andy Brown  https://www.experts-exchange.com/
// 
using System;
using System.IO;
using System.Windows.Forms;

namespace TSPaste2
{
    class Program
    {
        //Getting destination foler :: note: from argument %~1 ::
        String[] args = Environment.GetCommandLineArgs();
        [STAThread] static void Main(string[] args)
        {
            //Setting Destination foler:
            string DestFolder = args[0];

            if (Clipboard.ContainsFileDropList())
            {
                //copy to D:\test ( note: note: C# args[0] == bat/cmd == "%~1" )
                foreach (string source in Clipboard.GetFileDropList())
                {
                    string Dest = DestFolder + "\\" + Path.GetFileName(source);
                     File.Copy(source, Dest, true); 
                }
            }
        }
    }
}
  • This is the command line used to compile the code:
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /t:exe out:"%tmp%\TSPaste2.exe" "%tmp%\TSPaste2.cs" /platform:anycpu /unsafe+ /w:0 /o nologo
  • This code was compiled/tested on csc.exe versions:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
  • This is the command line used to compile the code:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:"%tmp%\TSPaste2.exe" "%tmp%\TSPaste2.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo
  • Usage: TSPaste2.exe C:\destination\folder

To keep the TSPaste2.exe compiled file, edit the code by adding this line in bold/italics:

:next
copy /y "%tmp%\TSPaste2.exe" "c:\some\folder"
"%tmp%\TSPaste2.exe" "%~1" & del /q /f "%tmp%\TSPaste2.exe" & endlocal & goto :EOF && rem./ 2>nul >nul */ 

Obs.: 1) code /by @Andy Brown / Experts-Exchange C Paste files from clipboard

Obs.: 2) code overwrites files, if they exist in the destination folder.

Read more: File.Copy Method

0
0

Below are steps on how to copy a single file from one directory to another directory.

Copying a single file

Using the cd command, move to the directory that contains the file you want to copy.

Type a command similar to the following command.

copy myfile.txt c:\my\location

In the example above, you would substitute "myfile.txt" with the name of the file you want to copy, and "c:\my\location" with the destination directory. To see files available in the current directory use the dir command.

Copying multiple files to another location

Below are the steps on how to copy multiple files from one directory to another directory.

Using the cd command, move to the directory that contains the files you want to copy.

Once in the directory that contains the files you want to copy, type a command similar to one of the following commands.

copy *.* c:\mydir

In the example above, the command would copy every file in the current directory to the "mydir" directory.

copy *.txt c:\mydir

In the example above, the command would copy every txt, or text file, in the current directory into the "mydir" directory.

Source: https://www.computerhope.com/issues/ch000766.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .