簡體   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