Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a text file then copy content into clipboard

Status
Not open for further replies.

anarchoi

Technical User
Jun 29, 2009
12
CA
Hello,

Here's what i want to do :

i already have a script that will list all the files inside a folder then make a text file with the infos (i posted the code below)

But i want to copy the files list to the clipboard instead of writting the result in a text file.... could someone help me to do that??

Thanks a LOT !!!


On Error Resume Next

Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H0200
Const BIF_RETURNONLYFSDIRS = &H1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders("MyDocuments")
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)

Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True)
Set objFolder = objFSO.GetFolder(strFolderPath)
Set objColFiles = objFolder.Files

For Each file In objColFiles
objNewFile.WriteLine(file.Name)
Next
objNewFile.Close

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
Dim objFolder, objFolderItem

On Error Resume Next

Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
If (objFolder Is Nothing) Then
Wscript.Quit
End If
Set objFolderItem = objFolder.Self
Browse4Folder = objFolderItem.Path
Set objFolderItem = Nothing
Set objFolder = Nothing
End Function
 
I don't remember the syntax but you can use and IE object to copy content to the clipboard. I will be able to provide you with code tomorrow if need be.

Basically, you open and instance of IE and use the objects clipboardData.SetData method. The clipboard works kind of like an associative array. VBs equivalent is a Dictionary object.

Something like
Code:
set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", "Hello This Is A Test"
objIE.Quit

Alternatively, you could open the text file, issue a select all and copy
Code:
wscript.sendkeys ("^a")
wscript.sendkeys ("^c")

-Geates
 
i'm really newbie to vbscript.... could you help me to get it work with my script ?

I can't find how to replace "Hello This Is A Test" by the content of the text file (filelist.txt)
 
how to get it back out of clipboard (not my code, looks like tsuji due to lack of CAPS)


function getcbstring()
'getcbstring is very much tempered,
'empty string is returned if it is unable to handle data or nothing is there.
set oie=createobject("internetexplorer.application")
oie.navigate "about:blank"
do while oie.readystate<>4 : wscript.sleep 50 : loop
set oclip=oie.document.parentwindow.clipboarddata
on error resume next
sdata=oclip.getdata("text")
if err.number=0 then
if lcase(typename(sdata))="string" then
getcbstring=sdata
else
getcbstring=""
end if
else
getcbstring=""
end if
on error goto 0
set oclip=nothing
oie.quit
set oie=nothing
end function
 
thanks but that's not what i want to do

I only want to put the result of my script (what is usually written in filelist.txt) inside the clipboard

Or open filelist.txt after it has been created and put the content inside the clipboard
 
seemed logical to include how to retrieve stuff from the clipboard for roundness.

similar code as Geates posted but with a little do loop waiting for IE to be ready

sText = "some text to clipboard"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
Do Until objIE.ReadyState=4: WScript.Sleep 1: Loop
objIE.Document.ParentWindow.ClipboardData.SetData "Text", sText
objIE.Quit
 
Thanks,

But now i'm still wondering how i can replace sText = "some text to clipboard" with the content of filelist.txt
 
'do you still need the filelist.txt file?
Set objColFiles = objFolder.Files
For Each file In objColFiles
sText = sText & vbCrLf & CStr(file.Name)
Next
Set objCOlFiles = Nothing

'if you insist on the filelist.txt
Set objTS = objFSO.OpenTextFile("c:\folder\filelist.txt", 1, True) 'cant remember if 1 is for reading)
sText = objTS.ReadAll '???
objTS.Close
Set objTS = Nothing
 
mrmovie, you're code is right. The loop is a good idea.

anarchoi, if filelist.txt is MASSIVE, you may experience memory errors when using objTS.ReadAll. To get around this, read each line separately and append the data to a variable.

Code:
set objFile = objFSO.OpenTextFile("C:\filelist.txt", 1)
do while not (objFile.AtEndOfStream)
   strLine = objFile.ReadLine
   strText = strText & strLine
loop

objIE.Document.ParentWindow.ClipboardData.SetData "Text", strText
objIE.Quit

-Geates
 
thanks a lot !! it's working

Only one last question:

Is there another way to copy the infos to clipboard (without using IE)

the problem is that a window pop up every time saying

"Do you want to allow this webpage to access your Clipboard?
If you allow this, the webpage can access the clipboard and read information that you've cut or copied recently"

And i have to click "Allow access" every time


I know with AutoIt there is a simple way to put data in the clipboard, but i dont know if this is possible with vbscript :/
 
two other google's

Set WshShell = WScript.CreateObject("WScript.Shell")WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE

' Set what you want to put in the clipboard 'strMessage = "Imagine that, it works!"' Declare an object for the word application 'Set objWord = CreateObject("Word.Application")' Using the object 'With objWord .Visible = False ' Don't show word ' .Documents.Add ' Create a document ' .Selection.TypeText strMessage ' Put text into it ' .Selection.WholeStory ' Select everything in the doc ' .Selection.Copy ' Copy contents to clipboard ' .Quit False ' Close Word, don't save ' End With

Set WshShell = WScript.CreateObject("WScript.Shell")WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
 
i just tryed it, but it doesn't work :(

It says it can't create the activex object "word.application"





Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE

strMessage = "Imagine that, it works!"
Set objWord = CreateObject("Word.Application")

With objWord .Visible = False
.Documents.Add
.Selection.TypeText strMessage
.Selection.WholeStory
.Selection.Copy
.Quit False
End With

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
 
sorry, i posted two options, one using the Shell, the other the Word.Application,

how about just trying the Shell method, i.e. just

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE

saying that the shell method will be restricted to the amount of data you can post through it, i guess.

btw, why do you need to it to the clipboard? this suggests the information is being passed somewhere, what is done with the information in the clipboard?
 
Thank you all for your helpful information.
 
>the problem is that a window pop up every time saying

That's an IE security setting ("Allow Programmatic clipboard access") that is currently set to Prompt. You can change this to Enable, which eliminates the prompt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top