?
Solved

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

Posted on 2011-03-09
8
Medium Priority
?
981 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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 57

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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

764 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