Link to home
Start Free TrialLog in
Avatar of lasthope
lasthope

asked on

Issue Running a Batch file and piping to a text Log file

I want to run a scheduled task that executes a .bat file. The .bat file works fine.  The problem I'm having is naming the .txt log file I create when running this .bat.

I hope this is clear - Here is what's happening:

From Command Prompt I run my batch file and pipe (>) to a .txt log file as follows:

                  batch.bat > logfile-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s.txt

This produces my logfile named appropriately with date and time as "logfile-03-10-2011_@_11h10m17s.txt" - this is perfect

But if I run this batch file as a Scheduled Task the log file is created as "logfile-03-10-2011_@_" No file extension and without the time stamp. Not acceptable.

I would really like to have the Time Stamp (and .txt extension) in the logfile name - the date wouldn't have to be in the exact time format.

How can this be accomplished?

Thanks in advance!!
 
Avatar of OrenRozen
OrenRozen
Flag of Israel image

try doing the same with the following change

batch.bat > logfile-%%date:~4,2%%-%%date:~7,2%%-%%date:~10,4%%_@_%%time:~0,2%%h%%time:~3,2%%m%%time:~6,2%%s.txt

Avatar of Steve Knight
Please check your time format for the default user, I imagine it is possibly adding a leading space when the hour is 0x and therefore there would be a space in the filename.  

In which case add " " around the whole filename would be the simplest solution:

batch.bat > "logfile-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s.txt"

Or change the default date format to hh:mm:ss etc. or change your batch file to work out the date in a different way such as VBScript like in my article here:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

and then in your batch file wrap it all in ( ) and redirect, i.e.

@echo off
set datetime=date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s
rem change space for 0
set datetime=%datetime: =0%
rem batch.bat
(echo your existing batch
xcopy this
move that
echo or whatever
)>"%logfile%"
Avatar of lasthope
lasthope

ASKER

I'm going to try both of the suggestions and let you know what works.

DRAGON-IT  -  Clarify for me.  If I enclose my batch file commands in the parans where is your code directing going to include the date,  time, and .txt extension? I'm not making the connection.
Sorry it wasn't clear.  In the example batch above I missed out a line setting the logfile name, copy/paste error sorry!

@echo off
rem set variable to rough date and time.
set datetime=%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s
rem change space for 0
set datetime=%datetime: =0%
set logfile=logfile-%datetime%.txt
(echo Your existing batch
)> %logfile%

That for instance just made a file called logfile-10-03-2011_@_14h00m39s.txt with the contents "Your existing batch".

Anything between the ( ) will be output to the logfile.

I would generally use a VBScript approach for the date if it may be on a different machine or login though to make sure the date formats agree as per:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

but give it a go like this and see if it works for you.

Steve
Steve,

I like the idea of setting the date & time variable in the batch file. The problem I'm having is with the line:

> %logfile%

The following are the end of my script where the error occurs using "ECHO ON" so i get this info from the command prompt.  NO BackupLog-date@time.txt is created.  Any input on this?

Maybe I'm confused about your batch commands.  My interpretation is it will allow the ability to place my XCOPY commands between the ( ..... ) and the batch will run and create the log file in the format requested.  If I use the ( .... ) I get the same "The syntax of the command is incorrect." error on the first ( .


I included the code of my batch file for your reference.

S:\BackupScripts>GOTO END

S:\BackupScripts>ECHO END ROUTINE
END ROUTINE
The syntax of the command is incorrect.
S:\BackupScripts>> BackupLog-03-12-2011_@_09h35m03s.txt
S:\BackupScripts>
ECHO On

ECHO *
ECHO *

MOVE /Y S:\BackupScripts\Logs\backuplog*.txt S:\BackupScripts\Logs\OLDLOGS\

ECHO *
ECHO *

ECHO MOVE COMMAND %ERRORLEVEL%


rem set variable to rough date and time.

set datetime=%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s

rem change space for 0

set datetime=%datetime: =0%


set logfile=BackupLog-%datetime%.txt


REM Batch Commands Start Here

:SERVER1

ECHO *
ECHO *
ECHO **** Copy SERVER1 STUFF Backup Files ****
ECHO *
ECHO SERVER1 %ERRORLEVEL%

Xcopy \\SERVER1\E$\stuff\*.* \\SERVER7\BACKUP$\SERVER1\stuff\ /Y /E /O

ECHO SERVER1 %ERRORLEVEL%

IF %ERRORLEVEL% NEQ 0 GOTO SERVER1ERR

:A0ERR

ECHO ********************************************************************
ECHO ****                                                            ****
ECHO **** CHECK LOG FOR ERRORS COPYING STUFF FOLDER from 1 to 7      ****
ECHO ****                                                            ****
ECHO ********************************************************************

set VAR=BAD


:DONE

IF "%VAR%"=="BAD" GOTO TROUBLE

ECHO "%VAR%"

IF %ERRORLEVEL% NEQ 0 GOTO TROUBLE

ECHO EXITING DONE ROUTINE


:END

ECHO END ROUTINE

> %logfile%

PAUSE

EXIT

Open in new window

Given what you are trying to accomplish, and the fact that this is a scheduled job you are running, I'd like to suggest a different approach.  Rather than embed the logging file name and output redirection in the batch script itself, I often take the approach for scheduled jobs of capturing ALL output from the batch script at the task scheduler command line level.

The basic idea here is we want to run the batch script just like we would from the command line, except we also want to capture all the output from the job to a log file.  This would include any ECHO commands in the script as well as all the output from any programs run.  This is easy to do and typically gives me the best audit trail if things go wrong.

In the Task Scheduler where you are scheduling the job, add the following to the end of the command line you use to run the batch script.  Here's an example.

c:\util\myjob.cmd >"c:\logs\backuplog-%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.txt" 2>&1

Open in new window

Notice we are redirecting all output (both standard output and error output) to a log file of our naming, and adding a date and time stamp to it.  You will likely have spaces in front of the hour for hours less than 10, but that isn't a huge problem really.  I also prefer the date in YYYYMMDD format for sortability.  If this job only runs once a day, you might only need the date in the log file name, not the time.

Let me know if this makes sense, I think you'll find it a better way to capture the output of the scheduled job.

~bp
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All working now. Just needed to get everything in the right place.

Thanks again for the great assistance with this!