繁体   English   中英

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

[英]Use VBscript to paste data into a txt file and save

我需要将剪贴板(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"

但是什么也没发生...

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

我刺了一下……这是我成功的结果。

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

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

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM