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?
Best ways to redirect to log from within the batch
Moderator: DosItHelp
Re: Best ways to redirect to log from within the batch
3. Correct. Windows does not have a native TEE function like Unix and Linux do.
Re: Best ways to redirect to log from within the batch
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
Re: Best ways to redirect to log from within the batch
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.
Re: Best ways to redirect to log from within the batch
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.
Re: Best ways to redirect to log from within the batch
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