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).
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).
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}];
LoadNETType["System.Windows.Forms.Clipboard"];
Clipboard`SetDataObject[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:
FileNameToClipboard[fname__String]:=...FileNameToClipboard[{fnames__String}]:=FileNameToClipboard[fnames][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 04 '16 at 18:40
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.
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 03 '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).