How Can I Save Output to a Text File?

How Can I Save Output to a Text File?

  • Comments 10
  • Likes
Hey, Scripting Guy! Question

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

SpacerHey, Scripting Guy! AnswerScript Center

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:

cscript myscript.vbs

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • can v save the data on the o/p screen as it is? along with the data tht is present in our program?

    in the sense: if im writing a prog to say

    the current value is : ____ and the user enters the values there.. is it possible to save all of this as a text file?

  • Hi all.

    This option doesn't work when used on scheluded task.

    I need to put any command in the script to do this.

    Tks.

  • Strange, I ran this in a .bat file with a script that I run to see if PCs are responding on the network.

    When I run the script manually, all 3 PCs come back as responding to pings.

    When I run the .bat file, the txt file indicates that the PCs are not responding.

    Why would this command return the opposite result of what is expected?

  • How can I output only the failed pings to the log.txt file?

  • okay, how can I change the color of the failed pings on that outputted >>log.txt file?

  • txt files don't have color attributes, try RTF but I don't know if a batch file will do that (or even VBScript, maybe it will)

  • I did a similar ping -> log  file thing but what I did was display the results in a auto refresh html file and color coded the DIV behind the text based on the ping, I did it in powershell but VBScript will definitely work

  • Hi,

    I would appreciate if you could share a power-shell script through which I can simply multiple perform telnet from an internal server and generate a report or a log file which informs me which telnet commands were successful and which failed.

    For example:

    telnet 10.135.2.1 443

    telnet 10.135.2.1 22

    telnet 10.135.2.1 21

    and then a report

    443 passed

    22 failed.

    Cheers!

  • I put this at the top of myscript.vbs and got a compilation error

  • not work for may telnet., it only save the title before the telnet., what i need is to save all logs in telnet