# How do I copy files to the clipboard?

Is there a way to mimic the Ctrl + V for a given file?

I've got Mathematica to locate a certain file path, but I don't bother to copy the file in File Explorer (I will do this many times).

• Clipboard? What platform? Windows? – Peter Mortensen Jan 3 '16 at 21:12
• If you are using OS X I would recommend using apple script (or java script) – Sascha Jan 3 '16 at 22:58
• Similar (but not necessarily a duplicate): Clipboard with transparency – Peter Mortensen Jan 3 '16 at 23:18
• To make it more clear what you are asking, can you add an example of what you are trying to achieve? – Peter Mortensen Jan 16 '16 at 19:07

Building on Todd Gayleys code from the answer that Peter Mortensen pointed to in his comment here is code that I think does what the OP wants: it copies a filename to the clipboard in such a way that you can use Ctrl-V in windows explorer to copy that file to the directory which is shown in the explorer. This code of course is limited to Windows boxes:

 Needs["NETLink"]

FileNameToClipboard[fname_String] := Module[{dataObject},
InstallNET[];
NETBlock[
dataObject = NETNew["System.Windows.Forms.DataObject"];
dataObject@SetData["FileDrop", {fname}];
ClipboardSetDataObject[dataObject]]]


I'm not a .NET expert so I don't know if I really am using these object/methods correctly, but for me the code did what I expected...

You may have noticed that one needs to submit a list of strings to the DataOPject's SetData method. This will automatically be transformed to a collection/array that the method seems to understand and makes it trivial to provide a list of files to put to the clipboard (as Peter Mortensen has suggested). All you need to change to make this work with a list of filenames is:

1. change the pattern for the definition to FileNameToClipboard[fname__String]:=...
2. add another definition: FileNameToClipboard[{fnames__String}]:=FileNameToClipboard[fnames]
• This is not asked for by the OP, but perhaps a generalisation could be demonstrated (as a variation to the already-existing code) - working for a list of files? SetDataObject will probably also work for this, but I have used the .NET function [Windows.Clipboard]::SetFileDropList($fileSet); (PowerShell syntax. It may require import of assembly "PresentationCore") from PowerShell. $fileSet (PowerShell syntax) must be of type System.Collections.Specialized.StringCollection. I created an object of that type and used Add (there is probably a more elegant way to do it). – Peter Mortensen Jan 4 '16 at 18:40
• cont': (I don't have access to Mathematica on Windows, only on Raspberry Pi, so I can't test it (but I have tested it from PowerShell and it works as expected).) (SetFileDropList (MSDN).) – Peter Mortensen Jan 4 '16 at 18:47
• @PeterMortensen: in fact it is almost trivial to add this, as the SetData method wants a list of strings even for a single filename (which was the only tricky part to find out in the original code). For details how you can make the function work for multiple files see my edit. I don't fully understand the magic which makes this work automatically, but most probably a list of strings is just transfered into something that happens to implement the interface which the method expects... – Albert Retey Jan 4 '16 at 20:50

One way to approach this is to ask the OS to do it. For example, I have a file "test.txt". I can copy this to "test2.txt" using:

Run["cp test.txt test2.txt"]


and the OS will create (copy) the file to the new name. You can programmatically change the file names and paths in the string. Run also works to run other OS functions as well.

You can, of course, also use CopyFile.

• It seems that I need to elaborate more... I'm fully aware of CopyFile, but my intention is copying the file to clipboard (just like selecting a file and press Ctrl-V) for later use in another software. – mmjang Jan 3 '16 at 15:16

Your question is not very clear and as it stands I'm not sure Mathematica is the right tool for the job but your comment regarding CopyFile to bill's answer seems to indicate that you wish to copy the contents of your file to the system clipboard in which case something like:

CopyToClipboard[Import[file, "Text"]]


where file is the path to your file, may be what you require. Depending on the contents of your file importing in "Text" format may or may not work for you.

As I understand it, this is not quite the same as using Ctrl+C and then Ctrl+V in File Explorer which is essentially an "intelligent" version of CopyFile (i.e. it handles renaming files if pasted in the same directory).