使用VBscript将数据粘贴到txt文件中并保存

时间:2016-06-08 20:21:49

标签: vbscript hp-uft

我需要将剪贴板中的数据(Ctrl + V)粘贴到文本文件中并保存。

我已经做了一些研究,似乎text file没有类似SaveAs的方法。

使用下面的代码,我可以创建一个新的文本文件并将数据粘贴到其中,但我不能Save它:

Set WShshell = CreateObject("WScript.Shell")
WShshell.run "c:\WINDOWS\system32\notepad.exe",1
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V" 

我知道有一种名为CreateTextFile的方法,但似乎无法用paste执行此操作。

我也尝试将这两者结合起来:

Set WShshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "c:\1.txt",true
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V"

但什么都没发生......

2 个答案:

答案 0 :(得分:4)

UFT有自己的剪贴板访问方法可以使用。在这里,我创建了一个剪贴板实例,并将其内容提取到sText,然后在C:\ temp中创建了一个文本文件,并将剪贴板中的数据直接写入其中。 oFile.Close关闭文件并同时保存。

Dim oClipboard : Set oClipboard = CreateObject("Mercury.Clipboard") 
sText = oClipboard.GetText ' gets the current content of the clipboard

Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.CreateTextFile("C:\temp\myTextFile.Txt", True)

oFile.Write sText
oFile.Close

答案 1 :(得分:3)

我捅了一下......这是我成功的结果。

我还注意到,当另一个记事本已经打开时,这个剧本真的很生气。

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set WShshell = CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject")

'Gets user profile variable with CMD
UserProfile = CreateObject("WScript.Shell").Exec("cmd /c echo.%USERPROFILE%").Stdout.ReadAll

'Creates temporary files in appdata\local\temp
'--------------------------------------------------------------------------------------
tmpfilename = fso.GetTempName
tmpfile = Replace(UserProfile & "\AppData\Local\Temp\" & tmpfilename, vbcrlf, "")
Set CreateTempFile = FSO.OpenTextFile(tmpfile, forwriting, true) : CreateTempFile.Close

'Creates a new file for the pasted content
'--------------------------------------------------------------------------------------
MyFile = ThisFolder & "ClipBoard_Extract_" & _
    Replace(FormatDateTime(Cdate(now),2), "/", "-") & "_" & _
    Hour(now) & Minute(Now) & Second(now) & ".txt"

'Execute's the Main Sub but you could get along without it.
'       I usually build my scripts to scale and do logging and other magical things.
'--------------------------------------------------------------------------------------
MainScript

Sub MainScript()
    CaptureClipboardText
    ExtractTempFile
End Sub

Sub CaptureClipboardText
    WShshell.run "c:\WINDOWS\system32\notepad.exe " & tmpfile, 1
    WshShell.AppActivate "Notepad"
    wscript.sleep 1000
    WShshell.SendKeys "^V"
    WShshell.SendKeys "^S"
    Wshshell.SendKeys "%F"
    Wshshell.SendKeys "x"
    Wshshell.SendKeys "s"
    wscript.sleep 1000
End Sub

Sub ExtractTempFile
    Set Extract = fso.OpenTextFile(tmpfile, ForReading)
    Set Output = fso.OpenTextFile(MyFile, ForWriting, True)  'True on this syntax means - Create the file if it doesn't exist.
    Do Until Extract.AtEndofSTream
        line = Extract.Readline
        Output.Writeline Line
    Loop
    Extract.Close : Set Extract = Nothing
    fso.DeleteFile tmpfile, true
End Sub

Function ThisFolder
    ThisFolder = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName))
End Function

干杯!

相关问题