1

I want to enable logging to a batch file which should log all the contents of command prompt including input/output/error. So I have tried something like the following but it results in a empty log file without any contents. It looks, I am missing something. Below is the batch file.

@echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root% >> %LOGFILE%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
1
  • 3
    call :logit >> "%LOGFILE%" 2>&1 ? Commented Jan 7, 2019 at 18:58

2 Answers 2

4

The log file is empty because you suppressed the output with the command

echo off

Use echo on instead before commands that shall be logged. The command cd usually doesn't send anything to stdout. That's why you got an empty file.

Instead of this ping thingy I recommend

timeout 6

Try this one:

@echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
echo on
call :logit >>%LOGFILE%
exit /b 0 ​ 

:logit 
set root=C:\ORACLE\ORA_DRIVERS 
cd %root%
UPDATE.EXE E:\class.ora 
@timeout 6 >nul

Edit: (beacause asked in a comment) The log file name can also include the date and time. You need to change the SET LOGFILE command appropriately. You can experiment, starting with this:

SET LOGFILE=C:\Users\xason\Desktop\Logs\log-%date%-%time%.txt
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to add timestamp (date & time including seconds) to the log file?
Try time /t >>%LOGFILE%.
but i believe the above wont append timestamp to the log_file name. Please correct me if I am wrong.
Give it a try. That's better than believing.
1

cd command has not STDOUT. You probably need to append STDOUT of your batch file to a text file. Run your batch file from cmd:

batch_file.bat >> "C:\Users\xason\Desktop\Logs\logs.txt"

Or, inside your batch file:

@echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit >>"%LOGFILE%"
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul

Or even:

@echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora >> %LOGFILE%
ping 127.0.0.1 -n 6 > nul

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.