Windows cmd error to file

Redirect Windows cmd stdout and stderr to a single file

I’m trying to redirect all output (stdout + stderr) of a DOS command to a single file:

Is it possible, or should I just redirect to two separate files?

7 Answers 7

The syntax 2>&1 will redirect 2 (stderr) to 1 (stdout). You can also hide messages by redirecting to NUL , more explanation and examples on MSDN.

Anders Lindahl’s answer is correct, but it should be noted that if you are redirecting stdout to a file and want to redirect stderr as well then you MUST ensure that 2>&1 is specified AFTER the 1> redirect, otherwise it will not work.

Background info from MSKB

While the accepted answer to this question is correct, it really doesn’t do much to explain why it works, and since the syntax is not immediately clear I did a quick google to find out what was actually going on. In the hopes that this information is helpful to others, I’m posting it here.

From MSKB110930

Redirecting Error Messages from Command Prompt: STDERR/STDOUT

Summary

When redirecting output from an application using the ‘>’ symbol, error messages still print to the screen. This is because error messages are often sent to the Standard Error stream instead of the Standard Out stream.

Output from a console (Command Prompt) application or command is often sent to two separate streams. The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the «>» symbol, you are only redirecting STDOUT. In order to redirect STDERR you have to specify ‘2>’ for the redirection symbol. This selects the second output stream which is STDERR.

Example

The command dir file.xxx (where file.xxx does not exist) will display the following output:

If you redirect the output to the NUL device using dir file.xxx > nul , you will still see the error message part of the output, like this:

To redirect (only) the error message to NUL , use the following command:

Or, you can redirect the output to one place, and the errors to another.

You can print the errors and standard output to a single file by using the «&1» command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file:

Читайте также:  Как поменять значок windows при загрузке

» is not recognized as an internal or external command, operable program or batch file

Whenever I try and run mycommand.exe from my windows cmd.exe terminal, I get this error:

»mycommand.exe’ is not recognized as an internal or external command, operable program or batch file’

Then

I also experienced a similar error when I tried to run C:\Program Files\My-App\Mobile.exe

»C:\Program’ is not recognized as an internal or external command, operable program or batch file’

2 Answers 2

This is a very common question seen on Stackoverflow.

The important part here is not the command displayed in the error, but what the actual error tells you instead.

a Quick breakdown on why this error is received.

cmd.exe Being a terminal window relies on input and system Environment variables, in order to perform what you request it to do. it does NOT know the location of everything and it also does not know when to distinguish between commands or executable names which are separated by whitespace like space and tab or commands with whitespace as switch variables.

When Actual Command/executable fails

First we make sure, is the executable actually installed? If yes, continue with the rest, if not, install it first.

If you have any executable which you are attempting to run from cmd.exe then you need to tell cmd.exe where this file is located. There are 2 ways of doing this.

specify the full path to the file.

Add the location of the file to your environment Variables.

Goto:
——> Control Panel-> System-> Advanced System Settings->Environment Variables

In the System Variables Window, locate path and select edit

Now simply add your path to the end of the string, seperated by a semicolon ; as:

Save the changes and exit. You need to make sure that ANY cmd.exe windows you had open are then closed and re-opened to allow it to re-import the environment variables. Now you should be able to run mycommand.exe from any path, within cmd.exe as the environment is aware of the path to it.

When C:\Program or Similar fails

How to Redirect Command Output to a File

Use redirection operators to save a command’s results to a file

Use a redirection operator to redirect the output of a command to a file. It’s one of our favorite Command Prompt Tricks & Hacks.

All the information that’s displayed in the Command Prompt after running a command can instead be saved to a file which you can open in Windows to reference later or manipulate however you like.

While there are several redirection operators, which you can read in detail about here, two, in particular, are used to output the results of a command to a file: the greater-than sign, >, and the double greater-than sign, >>.

Читайте также:  Windows 10 a2dp не работает

How to Use Redirection Operators

The easiest way to learn how to use these redirection operators is to see some examples:

In this example, all the network configuration information normally seen on screen after running ipconfig /all, is saved to a file by the name of mynetworksettings.txt. It’s stored in the folder to the left of the command, C:\Users\jonfi in this case.

The > redirection operator goes between the ipconfig command and the name of the file. If the file already exists, it’ll be overwritten. If it doesn’t already exist, it will be created.

Although a file will be created if doesn’t already exist, folders will not. To save the command output to a file in a specific folder that doesn’t yet exist, first, create the folder and then run the command. Make folders without leaving Command Prompt with the mkdir command.

Here, when the ping command is executed, Command Prompt outputs the results to a file by the name of Ping Results.txt located on the jonfi user’s desktop, which is at C:\Users\jonfi\Desktop. The entire file path in wrapped in quotes because there was a space involved.

Remember, when using the > redirection operator, the file specified is created if it doesn’t already exist and is overwritten if it does exist.

The Append Redirection Operator

The double-arrow operator appends, rather than replaces, a file:

This example uses the >> redirection operator which functions in much the same way as the > operator, only instead of overwriting the output file if it exists, it appends the command output to the end of the file.

Here’s an example of what this LOG file might look like after a command has been exported to it:

The >> redirection operator is useful when you’re collecting similar information from different computers or commands and you’d like all of that data in a single file.

The above redirection operator examples are within the context of Command Prompt, but you can also use them in a BAT file. When you use a BAT file to pipe a command’s output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file.

Use Redirection Operators in Batch Files

Redirection operators work in batch files by including the command just as you would from the Command Prompt:

The above is an example of how to make a batch file that uses a redirection operator with the tracert command.

How to prevent auto-closing of console after the execution of batch file

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?

17 Answers 17

In Windows/DOS batch files:

This prints a nice «Press any key to continue . . . » message

Or, if you don’t want the «Press any key to continue . . .» message, do this instead:

Читайте также:  Data sources windows server

Depends on the exact question!

Normally pause does the job within a .bat file.

If you want cmd.exe not to close to be able to remain typing, use cmd /k command at the end of the file.

If you want cmd.exe to not close, and able to continue to type, use cmd /k

Just felt the need to clarify what /k does (from windows website):

/k : Carries out the command specified by string and continues.

So cmd /k without follow up command at the end of bat file will just keep cmd.exe window open for further use.

On the other hand pause at the end of a batch file will simply pause the process and terminate cmd.exe on first button press

If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL command in the script, besides just the ‘mvn clean install’.

Like this will close the console

Like this the console will stay open

If you dont use the CALL command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe

How do you redirect standard input to a file in the Windows command line?

On Unix I would do something like:

How can I do this on the Windows command prompt or batch file?

EDIT: Basically, I am looking for the functionality of cat with no arguments (it reads from stdin and spits it back out to stdout).

4 Answers 4

CON is the MS-DOS device for console input. You can redirect to a file as follows:

To terminate, hit Ctrl + C or Ctrl + Z , Enter ( Ctrl + Z = EOF).

If all you want is to read stdin and write what you read to stdout, then FINDSTR may work, depending on how you use it.

FINDSTR will output an exact binary image of the input as long as the input is specified as a single file name at the end of the argument list.

Pipes or redirection may also work, depending on the content of the input:

The output will be corrupted if any of the following occur while using redirected or piped input with FINDSTR:

  • Any input line > 8191 bytes
  • Last line of input is not terminated by \n. (command may hang if redirected input)

FINDSTR will not work if multiple input files are specified because in that case the name of the file will be used as a prefix to each line of output.

FINDSTR also differs from cat in that it cannot read from both stdin and a named file.

Оцените статью