1

I'd like to write output from VBScript to notepad/wordpad in realtime. What's the best way to do this? I'm aware of sendkeys, but it requires that I parse the input for special commands.

Eugene
  • 10,957
  • 20
  • 69
  • 97
  • Why not use the filesystemobject to create a textstream? – Fionnuala Feb 22 '12 at 00:10
  • 1
    Parsing the special characters is not really rocketscience. Using a regexp like `Regex.Replace(myString, "([\+\^\%\~\ {\}\[\]\(\)])", "{$1}")` should do the trick. Maybe you would want to replace the `Tab` and `Newline` character too, that would become something like `Regex.Replace(myString, "\t", "{TAB}")` and `Regex.Replace(myString, "\r\n", "{ENTER}")`. These are the only characters you have to replace, because all other special input are keyboard input, like `{SHIFT}`, `{F1}` etc. – AutomatedChaos Feb 23 '12 at 06:45

2 Answers2

2

SendKeys is the only method for writing to a third-party application in realtime. Why don't you use CScript and write to the standard output instead? That is what it is meant for.

' 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
Nilpo
  • 4,675
  • 1
  • 25
  • 39
1

Try this

 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
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32