How to redirect output when using wscript.shell in vbscript
- 3 Comment
Welcome back!
Google Query: Wscript.Shell Application redirection output
I was trying to run ipconfig in one of my vbscripts using wscript.shell and the run method. However, I was trying to redirect the output to a text file. It just wasn’t working. It was a combination of those tricky double quotes and me not knowing the proper syntax to accomplish this from inside a vbscript. Thankfully I found the answer quickly after some googling.
There was a post at the ScriptingAnswers.com forum that had a solution to my problem. Basically, it can be summed up with the following code:
Set objShell = WScript.CreateObject("WScript.Shell")
strRun = "%comspec% /c ipconfig.exe > " & AddQuotes("C:\temp\ipconfig.txt")
objShell.Run strRun, 1, True
Function AddQuotes(strInput)
AddQuotes = Chr(34) & strInput & Chr(34)
End Function
The function AddQuotes takes care of the double quoting issues by surrounding whatever is input (using the built in strInput argument of the AddQuotes function.) into the AddQuotes function at the beginning of the script with additional quotes using the built in Chr(34) function.
As for the %compspec% /c syntax. That is covered in this Microsoft KB Article.
That’s it! You should now be able to redirect the output of any shell command using this technique. Happy Scripting!
Technorati Tags: VBScript, Microsoft, Windows, Scripting
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
3 Comments on this post
Trackbacks
-
Rodney said:
strInput is just the argument for the AddQuotes function. It’s not a built in function.
August 21st, 2008 at 9:34 am -
hstagner said:
Hello Rodney,
Thank you for the correction. I have edited the post accordingly. Thank you for reading!
August 21st, 2008 at 10:28 am -
Bal said:
Hi,
In this how can I pass command line arguments to the bat (or exe) along with logging
March 17th, 2010 at 1:02 pm

