How Can I Save Output to a Text File?
Hey, Scripting Guy! Is there an easy way to save my script output to a text file rather than displaying it on screen?
— KP, Ogden, UT
Hey, KP. If you always want your script to save data to a text file rather than display it on screen, then you’re probably better off using the FileSystemObject and its file-writing capabilities. However, it appears as though you sometimes want to save data to a text file and sometimes want to display that data in a command window. So can you create a multi-purpose script like that?
Well, you could, but an easier way is to use the command shell redirection characters when you start the script. For example, you might typically start your script like this:
That runs the script, and causes any Wscript.Echo commands to display their output in the command window.
However, you could also start your script like this, using the > command shell redirection command:
cscript myscript.vbs > c:\scripts\log.txt
In that case, nothing will be echoed to the screen; instead, all your Wscript.Echo commands will be written to the text file C:\Scripts\Log.txt.
In the preceding example, should C:\Scripts\Log.txt already exist it will be overwritten with any new information generated by the script. If you’d prefer to have new information appended to C:\Scripts\Log.txt, then start the script using this command:
cscript myscript.vbs >> c:\scripts\log.txt