How to redirect output when using wscript.shell in vbscript
- 4 Comment
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
AddQuotes = Chr(34) & strInput & Chr(34)
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!
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.