Solved

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

Posted on 2011-03-09
8
976 Views
Last Modified: 2012-05-11
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!!
 
0
Comment
Question by:lasthope
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:OrenRozen
ID: 35092187
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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35092516
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:

http://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%"
0
 

Author Comment

by:lasthope
ID: 35094183
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.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 35094754
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:

http://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
0
 

Author Comment

by:lasthope
ID: 35114593
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

0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 35115953
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
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 35118415
Bill,

I agree is easier, but that is pretty well what the guy is already doing from his original question, the only reason I digressed was to show how we could go if he wants to get rid of the space in dd or mm then to run it through another batch to amend the date/time format.

In my post http:#35092516 I said to continue this way he most likely just needs to add " " around the filename that he is already using.

In http:#35114593 lasthope, if you add ( before line 1 and before the > on line 68 that should capture anything from inbetween.  Adding 2 >&1 after the existing >%logfile% will also capture error output like Bill says.

Steve
0
 

Author Closing Comment

by:lasthope
ID: 35349335
All working now. Just needed to get everything in the right place.

Thanks again for the great assistance with this!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Determining the an SCCM package name from the Package ID
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question