Best ways to redirect to log from within the batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Best ways to redirect to log from within the batch

#1 Post by JDAIII » 17 Dec 2014 16:53

So New question. I know that there are a few ways to do this, but I'd like to find an easier way. So far I have the items listed below that I know of, but I would like something simple that I can add to the beginning of the .bat file without too much trouble.

I would like all of the echo, user input, and program output to be redirected to a log file.

These options are not preferred due to reasons listed:
1. redirect when executing .bat file: c:\batch.bat >> c:\batch.log (our customers are too dumb to get that one right)
2. append >>%LogFile% 2>&1 to the end of each line in the batch (annoying to write on 400 lines, and doesn't get the output from external applications named in batch.bat.
3. Surround each method in () and at the end write >>%LogFile% 2>&1 (I tried and the echo only went to the log file and not the terminal. Possibly there is another way?)
4. exec another batch.bat file with the .bat file. (This means sending customers two files and they will F it up)


Any ideas of a way to do this without cluttering the batch (since we will be editing it on the fly for customers) and without requiring a redirect when run (as customers inevitably screw this up).

Ideas, questions, comments?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Best ways to redirect to log from within the batch

#2 Post by Squashman » 17 Dec 2014 17:20

3. Correct. Windows does not have a native TEE function like Unix and Linux do.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Best ways to redirect to log from within the batch

#3 Post by Yury » 17 Dec 2014 17:51

JDAIII wrote:I would like something simple that I can add to the beginning of the .bat file without too much trouble.



Code: Select all

@rem:>&3 3>"example.log"

echo Hello, world!
timeout 3
start "" notepad "example.log"
exit /b

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: Best ways to redirect to log from within the batch

#4 Post by JDAIII » 17 Dec 2014 18:05

Squashman wrote:3. Correct. Windows does not have a native TEE function like Unix and Linux do.


Hej Squashman,

Can you think of something else I can do? I'd really like to avoid appending the >> logfile to each line. And I can't include two files or the customers will screw it up somehow.

I tried the other @rem suggestion but it wasn't logging to the file and I'm not too familiar with that command.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Best ways to redirect to log from within the batch

#5 Post by Squashman » 17 Dec 2014 18:16

Yury wrote:
JDAIII wrote:I would like something simple that I can add to the beginning of the .bat file without too much trouble.



Code: Select all

@rem:>&3 3>"example.log"

echo Hello, world!
timeout 3
start "" notepad "example.log"
exit /b

Neat piece of code but will not allow you to see any of the Interactive Menus that he needs.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Best ways to redirect to log from within the batch

#6 Post by foxidrive » 18 Dec 2014 00:00

JDAIII wrote:1. redirect when executing .bat file: c:\batch.bat >> c:\batch.log (our customers are too dumb to get that one right)


Add this at the start of your batch file:

Code: Select all

@echo off
if "%~1"=="cat" goto :begin
copy /y "%~f0" "%temp%\dog.bat" >nul
call "%temp%\dog.bat" cat >"logfile.txt"
del "%temp%\dog.bat"
goto :EOF
:begin

Post Reply