Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Copy portion from one log file (fileA) into a new log file (fileB)

Hi Experts.  I need to copy a portion of text from fileA.log to fileB.log.  The portion of text lies between two headers in the log file (fileA.log).

--- SCHEDULEREC STATUS BEGIN
text
text
--- SCHEDULEREC STATUS END

I would need the script to copy the headers and text to a new log file (fileB.log).  How would one go about this?  Thanks for the help!
Avatar of oBdA
oBdA

If you don't mind losing empty lines, and assuming the log file doesn't contain characters that make batch files unhappy, try this:
@echo off
setlocal enabledelayedexpansion
set LogA=C:\Temp\test.log
set LogB=C:\Temp\testB.log
set StartLine=--- SCHEDULEREC STATUS BEGIN
set StopLine=--- SCHEDULEREC STATUS END
set Copy=False
for /f "delims=" %%a in ('type "%LogA%"') do (
	if /i "%%a"=="%StopLine%" (goto CopyDone)
	if !Copy!==True (
		REM >>"%LogB%" echo %%a
	) else (
		if /i "%%a"=="%StartLine%" (
			echo Starting copy.
			set Copy=True
		)
	)
)
:CopyDone
echo Done.

Open in new window

Sorry, missed the part that you wanted the header lines as well in the copied log:
@echo off
setlocal enabledelayedexpansion
set LogA=C:\Temp\test.log
set LogB=C:\Temp\testB.log
set StartLine=--- SCHEDULEREC STATUS BEGIN
set StopLine=--- SCHEDULEREC STATUS END
set Copy=False
for /f "delims=" %%a in ('type "%LogA%"') do (
	if /i "%%a"=="%StartLine%" (
		echo Starting copy.
		set Copy=True
	)
	if !Copy!==True (
		>>"%LogB%" echo %%a
	)
	if /i "%%a"=="%StopLine%" (
		goto CopyDone
	)
)
:CopyDone
echo Done.

Open in new window

Avatar of samiam41

ASKER

Thank you.  Will test shortly and post back in the morning (4:14pm here).
Good morning.  I've modifed the second script you posted to reflect the location of log A and B and ran the script.  I'm getting the error:  "The system cannot find the file specified.  Done."
Then either the file for LogA doesn't exist (more likely if you only got one error message, or the folder for LogB doesn't exist.
Note: do not use quotes around the path when you set the variable, even if it contains a space. Quotes are added later as required.
Good call.  I went back and reviewed it again which allowed me to find the error in the file name.

The script is running and completing but nothing is being copied.  I found the source log file has a date before the header.  

04/11/2016 19:40:45 --- SCHEDULEREC STATUS BEGIN
text
text
04/11/2016 19:40:45 --- SCHEDULEREC STATUS END

Any way to get the script to ignore the date and find just the header?  The script works if I put the date in front of the header.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Perfect!!  Thanks oBdA!