Copy file contents to clipboard

To get you started, here is the updated code that will copy content of any selected file to clipboard :slight_smile:

Option Explicit

Function OnClick(ByRef ClickData)
   Dim fso, tfile, txt
   If ClickData.func.sourcetab.selected_files.Count > 0 Then
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set tfile = fso.OpenTextFile(ClickData.func.sourcetab.selected_files(0))
      txt = tfile.ReadAll
      DOpus.SetClip txt
   End If
End Function

[Script code updated, as per the next post below. --Leo]

Oops, replace the

If Len(ClickData.func.sourcetab.selected_files) > 0 Then

with

If ClickData.func.sourcetab.selected_files.Count > 0 Then

otherwise the script would throw error when nothing is selected :blush:

[Script code in the previous post has been edited and updated to include this change. --Leo]

Ok guys, thanks for help.

Hello. Is there a way to paste contents of certain file (ex., C:\1.txt) as new .txt files in {s}?

How would that be different from making a copy of 1.txt?

i want to copy its contents. for ex., file 1.txt contains this lines:
Directory Opus 1
Directory Opus 2
Directory Opus 3
...
Directory Opus 1000

i want to create 1000 new .txt files, a copy of "FileType NEW=.txt FROMCLIPBOARD" command, but from file, not clipboard...

I have test this code but it's doesn't works for me. where is my mistake?


I have select a .txt file and press this button. then open MS Word and press Ctrl+v. But its doesn't paste the text from the selected files.

That’s just a simple file copy which you can do via the copy command.

sorry, guys. just verified all COPY's arguments, but not found a suitable one.

What’s this got to do with “copy file contents to clipboard” from 8 years ago? The clipboard isn’t involved if you’re just trying to make a second copy of a file somewhere.

Please start new threads for new questions. I’ve had to ask a few times now.

It would only include the first file, and only if it's a text file.

I just tried it and it still works OK today.

it seemed to me that this topic is the closest to my question. very close...

It's really not. You don't want to do anything to the clipboard. You want to copy a file from one place to another. Please start a new thread if you want help with that.

abcd.txt (7.6 KB)
Copy Content.dcf (988 Bytes)
Here is my test file and button it's still not work here.

The FileSystemObject can't handle your file's encoding.

This might work better:

Or use this script:

// https://resource.dopus.com/t/copy-file-contents-to-clipboard/21051

function OnClick(clickData) {
  var tab = clickData.func.sourcetab;
  if (tab.selected_files.count == 0) return;
  var stt = DOpus.Create().StringTools();
  DOpus.SetClip(stt.Decode(tab.selected_files(0).Open().Read(), 'auto'));
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="label" textcol="none">
	<label>21051</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/copy-file-contents-to-clipboard/21051</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>  var tab = clickData.func.sourcetab;</instruction>
		<instruction>  if (tab.selected_files.count == 0) return;</instruction>
		<instruction>  var stt = DOpus.Create().StringTools();</instruction>
		<instruction>  DOpus.SetClip(stt.Decode(tab.selected_files(0).Open().Read(), &apos;auto&apos;));</instruction>
		<instruction>}</instruction>
	</function>
</button>
1 Like

Its not worked for me.

image

@khalidhosain @galaxyhub

The script has always worked here. Try replacing auto with utf-16-le. Maybe your system locale settings somehow confuse the StringTools.

1 Like

lol - that yields chinese text, using utf-8 works. Many thanks.

1 Like