Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows command prompt log to a file

Tags:

I'd like to easily save the log of the Windows command prompt into a file. Is there any alternative to select everything and right-click on copy?

like image 340
Luis Andrés García Avatar asked Jan 24 '13 11:01

Luis Andrés García


People also ask

How do I output to a file in Command Prompt?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I pipe a command to a text file?

To redirect the output of a command to a text file instead of printing it to the screen in the command window, we simply need to execute the command and append it with the “>” angle bracket symbol—called, appropriately enough, a redirection.

How do I redirect in CMD?

On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands. Redirection can be done by using the operators > and >> .


3 Answers

You can redirect the output of a cmd prompt to a file using > or >> to append to a file.

i.e.

echo Hello World >C:\output.txt echo Hello again! >>C:\output.txt 

or

mybatchfile.bat >C:\output.txt 

Note that using > will automatically overwrite the file if it already exists.

You also have the option of redirecting stdin, stdout and stderr.

See here for a complete list of options.

like image 63
Bali C Avatar answered Oct 13 '22 09:10

Bali C


First method

For Windows 7 and above users, Windows PowerShell give you this option. Users with windows version less than 7 can download PowerShell online and install it.

Steps:

  1. type PowerShell in search area and click on "Windows PowerShell"

  2. If you have a .bat (batch) file go to step 3 OR copy your commands to a file and save it with .bat extension (e.g. file.bat)

  3. run the .bat file with following command

    PS (location)> <path to bat file>/file.bat | Tee-Object -file log.txt

This will generate a log.txt file with all command prompt output in it. Advantage is that you can also the output on command prompt.

Second method

You can use file redirection (>, >>) as suggest by Bali C above.

I will recommend first method if you have lots of commands to run or a script to run. I will recommend last method if there is only few commands to run.

like image 25
Mr.Hunt Avatar answered Oct 13 '22 11:10

Mr.Hunt


In cmd when you use > or >> the output will be only written on the file. Is it possible to see the output in the cmd windows and also save it in a file. Something similar if you use teraterm, when you can start saving all the log in a file meanwhile you use the console and view it (only for ssh, telnet and serial).

like image 32
oscar_ceja Avatar answered Oct 13 '22 09:10

oscar_ceja