Solved

Save log file as date {batch script output file}

Posted on 2014-03-13
2
1,139 Views
Last Modified: 2014-03-13
Hi Experts!  I have another question since I can't seem to get it right and don't have the time to play with it.

I have a script that copies text from multiple text files and dumps it to a single "master" text file.  I need the single/master text file to have the name that matches the date that is being searched.

Example; if I am looking for the data for 03/13/2014, I need the master text file to be named Master_Log_File_03-13-2014.log.  The very first thing the script does is asks what date you are searching for:

How can I set the text file to be named what the search date is?  Thanks for your help.
@echo off
setlocal enabledelayedexpansion
set CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
set CopyBegin=SCHEDULEREC STATUS BEGIN
set CopyEnd=SCHEDULEREC STATUS END
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	set LogFile=%%a
	if "!LogFile:~0,2!"=="\\" (
		for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
	) else (
		set Server=%ComputerName%
	)
	echo Processing '!LogFile!' on !Server! ...
	set /a CopyFound = 0
	set /a Copy = 0
	for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
		set Line=%%b
		if !Copy!==0 (
			if not "!Line!"=="!Line:%CopyBegin%=!" (
				set /a CopyFound = 1
				set /a Copy = 1
				>>"%CentralLog%" echo ========== !Server! Start ============================================================
				>>"%CentralLog%" echo !Line!
			)
		) else (
			>>"%CentralLog%" echo !Line!
			if not "!Line!"=="!Line:%CopyEnd%=!" (
				set /a Copy = 0
				>>"%CentralLog%" echo ========== !Server! End ============================================================
			)
		)
	)
	if !CopyFound!==0 (
		echo No match found in '!LogFile!'.
	)
)
goto :eof

[DATA]
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

0
Comment
Question by:samiam41
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39927234
Try this; I took the freedom of creating the timestamp as yyyy-mm-dd, because then the files in the log folder will be correctly sorted by date.
@echo off
setlocal enabledelayedexpansion
set CentralLogPrefix=Z:\Logs\TSM_Backups\Combined_TSMLogs
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
for /f "tokens=1-3 delims=/" %%a in ("%FindDate%") do (set TimeStamp=%%c-%%a-%%b)
set CentralLog=%CentralLogPrefix%%TimeStamp%.log
echo Collecting logs in '%CentralLog%' ...
set CopyBegin=SCHEDULEREC STATUS BEGIN
set CopyEnd=SCHEDULEREC STATUS END
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	set LogFile=%%a
	if "!LogFile:~0,2!"=="\\" (
		for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
	) else (
		set Server=%ComputerName%
	)
	echo Processing '!LogFile!' on !Server! ...
	set /a CopyFound = 0
	set /a Copy = 0
	for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
		set Line=%%b
		if !Copy!==0 (
			if not "!Line!"=="!Line:%CopyBegin%=!" (
				set /a CopyFound = 1
				set /a Copy = 1
				>>"%CentralLog%" echo ========== !Server! Start ============================================================
				>>"%CentralLog%" echo !Line!
			)
		) else (
			>>"%CentralLog%" echo !Line!
			if not "!Line!"=="!Line:%CopyEnd%=!" (
				set /a Copy = 0
				>>"%CentralLog%" echo ========== !Server! End ============================================================
			)
		)
	)
	if !CopyFound!==0 (
		echo No match found in '!LogFile!'.
	)
)
goto :eof

[DATA]
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39927286
A-Mazing!

Thanks oBdA!

~Aaron
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Learn about cloud computing and its benefits for small business owners.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now