?
Solved

Save log file as date {batch script output file}

Posted on 2014-03-13
2
Medium Priority
?
1,177 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 85

Accepted Solution

by:
oBdA earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

807 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