1

I've generated a batch file that's a long list of ren "x.jpg" "y.jpg" type commands.

I'd like to have all of the results outputted to a .txt file, but when I've tried to cmd rename.bat > output.txt or rename.bat >> output.txt, it only prints the command performed and not if there was an error in finding file, which instead shows in the cmd window.

How can I have all of it put into the .txt file?

Edge
  • 2,456
  • 6
  • 32
  • 57
  • 1
    `2>&1` add this to the line. See http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 for what it means. Or ` 2> Error.log `. – ACatInLove Dec 13 '17 at 00:22

2 Answers2

3

When running this batch script from cmd, you can use:

script.bat > output.txt 2>&1

This will direct stdout to output.txt and stderr to redirect to stdout (thus appending the errors to the file). Essentially, this will give you exactly what you would normally see at the console when running the script.

ZooM SiX
  • 75
  • 4
  • 1
    That only prints the errors and not the command that caused the error as well – Edge Dec 13 '17 at 00:35
  • Do you have an example of the code you're trying to use? Rem doesn't output anything unless an error is raised. We may need to adjust the script a bit to gather the command as well. – ZooM SiX Dec 13 '17 at 00:54
  • About 5,000 lines of `ren "x.png" "y.png" 2>> output.txt`, and I run the bat from cmd. I'd like the output to show the command, and if there's an error, the error after that before showing the next command, so I can see exactly what files are going wrong. – Edge Dec 13 '17 at 01:08
  • 1
    Got it figured out: `script.bat > output.txt 2>&1` That will output what your cmd sees and any errors that raise (if any). – ZooM SiX Dec 13 '17 at 01:15
  • Fantastic, thank you so much. Edit your original answer and you've got yourself an acceptance – Edge Dec 13 '17 at 02:01
  • You got i! Sorry about the delay on the answer, I had to play around with it a bit! – ZooM SiX Dec 13 '17 at 02:07
1

You have to redirect both, the stdout output to one file (with the '1>>' redirection), and the error output to a different file with the '2>' redirection. It can't be the same output file.

Like this:

rename.bat 1>>output.txt 2>errors.txt

This way you have on one hand the results in one file and the errors, in case they are, in another log file.