VBScript到记事本/写字板

VBScript到记事本/写字板

问题描述:

我想从VBScript输出到记事本/写字板实时。什么是最好的方法来做到这一点?我知道sendkeys,但它需要我解析输入的特殊命令。VBScript到记事本/写字板

+0

为什么不使用filesystemobject创建文本流? – Fionnuala 2012-02-22 00:10:43

+1

解析特殊字符并不是真正的火箭科学。使用像Regex.Replace(myString,“([\ + \^\%\〜\ {\} \ [\] \(\)])”,“{$ 1}”)'这样的正则表达式应该可以做到。也许你会想要替换'Tab'和'Newline'字符,这会变成像'Regex.Replace(myString,“\ t”,“{TAB}”)'和'Regex.Replace(myString,“ \ r \ n“,”{ENTER}“)'。这些是您必须替换的唯一字符,因为所有其他特殊输入都是键盘输入,如“{SHIFT}”,“{F1}”等。 – AutomatedChaos 2012-02-23 06:45:47

的SendKeys是写在实时第三方应用程序的唯一方法。为什么不使用CScript并将其写入标准输出?这就是它的意思。

' Force the script to run in the CScript engine 
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then 
    strPath = WScript.ScriptFullName 
    strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34) 
    CreateObject("WScript.Shell").Run(strCommand) 
    WScript.Quit 
End If 

For i = 1 to 10 
    For j = 0 to 25 
    WScript.StdOut.WriteLine String(j, " ") & "." 
    WScript.Sleep 50 
    Next 

    For j = 24 to 1 Step - 1 
    WScript.StdOut.WriteLine String(j, " ") & "." 
    WScript.Sleep 50 
    Next 
Next 

试试这个

Const fsoForWriting = 2 

    Dim objFSO 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    'Open the text file 
    Dim objTextStream 
    Set objTextStream = objFSO.OpenTextFile("C:\SomeFile.txt", fsoForWriting, True) 

    'Display the contents of the text file 
    objTextStream.WriteLine "Hello, World!" 

    'Close the file and clean up 
    objTextStream.Close 
    Set objTextStream = Nothing 
    Set objFSO = Nothing