Compare using batch script

June 21, 2010 at 06:45:50
Specs: Windows XP
HI,

I have two files (fileA.log and fileB.out).
fileA.log contains a keyword Add1=252,70
where 252 is the position (column) and 70 is
the length which matches with the fileB.out. i
want to write a script which reads fileA, takes
the position and length and compares it with
fileB and displays the result in fileC.txt or in an
HTML format. Is it possible? Can anyone help?

Thanks,
Rakesh


See More: Compare using batch script

Report •


#1
June 21, 2010 at 07:02:52
Hmm. Sounds plausible. What do you have so far?

[EDIT]
Wait - I'm confused. Does FileA.log = FileB.out, except that FileA.log has ADD=<col>,<len> prefixed on each row?
[/EDIT]
Insomniac at large


Report •

#2
June 21, 2010 at 13:19:27
I went with the assumption that FileA.log only has
ADD=<col>,<len>. Here's my crack at it:

::21746.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

CALL :HTML_Head

SET /A row=0
:Read_Files
FOR /F "%skip%usebackq tokens=2,3 delims==," %%a IN ("\Your\Path\To\FileA.LOG") DO (
    FOR /F "%skip%usebackq tokens=* delims=," %%c IN ("\Your\Path\To\FileB.OUT") DO (
        CALL :Format_Data %%a %%b %%c
        >>"\Your\Path\To\FileC.html" ECHO.           ^<tr^>
        >>"\Your\Path\To\FileC.html" ECHO.              ^<td^>!value!^</td^>
        >>"\Your\Path\To\FileC.html" ECHO.              ^<td^>%%a^</td^>
        >>"\Your\Path\To\FileC.html" ECHO.              ^<td^>%%b^</td^>
        >>"\Your\Path\To\FileC.html" ECHO.           ^</tr^>
        SET /A row+=1
        SET skip=skip=!row! 
        GOTO Read_Files
    )
)
CALL :HTML_Tail
EXIT /B

:Format_Data
SET /A col=%1-1
SET /A len=%2
SET value=%3
SET value=!value:~%col%,%len%!
GOTO :EOF

:HTML_Head
 >"\Your\Path\To\FileC.html" ECHO.^<html^>
>>"\Your\Path\To\FileC.html" ECHO.  ^<head^>
>>"\Your\Path\To\FileC.html" ECHO.    ^<META http-equiv="Content-Type"
>>"\Your\Path\To\FileC.html" ECHO.    content="text/html; charset=UTF-8"^>
>>"\Your\Path\To\FileC.html" ECHO.      ^<title^>FileC Contents^</title^>
>>"\Your\Path\To\FileC.html" ECHO.      ^<body^>
>>"\Your\Path\To\FileC.html" ECHO.        ^<H1^>FileC Contents^</H1^>
>>"\Your\Path\To\FileC.html" ECHO.        ^<table border="1"^>
>>"\Your\Path\To\FileC.html" ECHO.            tr^>
>>"\Your\Path\To\FileC.html" ECHO.              ^<td^>Value^</td^>
>>"\Your\Path\To\FileC.html" ECHO.              ^<td^>Column^</td^>
>>"\Your\Path\To\FileC.html" ECHO.              ^<td^>Length^</td^>
>>"\Your\Path\To\FileC.html" ECHO.            </tr^>
GOTO :EOF

:HTML_Tail
>>"\Your\Path\To\FileC.html" ECHO.        ^</table^>
>>"\Your\Path\To\FileC.html" ECHO.      ^</body^>
>>"\Your\Path\To\FileC.html" ECHO.    ^</META^>
>>"\Your\Path\To\FileC.html" ECHO.  ^</head^>
>>"\Your\Path\To\FileC.html" ECHO.^</html^>
GOTO :EOF

My FileA.log:
-------------

ADD=2,3
ADD=1,2
ADD=5,6
ADD=7,5
ADD=3,7

My FileB.out:
-------------

1ABC5678901234567890 2,3
AB345678901234567890 1,2
1234ABCDEF1234567890 5,6
123456ABCDE234567890 7,5
12ABCDEFG01234567890 3,7

My (generated) FileC.html:
--------------------------

<html>
  <head>
    <META http-equiv="Content-Type"
    content="text/html; charset=UTF-8">
      <title>FileC Contents</title>
      <body>
        <H1>FileC Contents</H1>
        <table border="1">
            <tr>
              <td>Value</td>
              <td>Column</td>
              <td>Length</td>
            </tr>
            <tr>
              <td>ABC</td>
              <td>2</td>
              <td>3</td>
            </tr>
            <tr>
              <td>AB</td>
              <td>1</td>
              <td>2</td>
            </tr>
            <tr>
              <td>ABCDEF</td>
              <td>5</td>
              <td>6</td>
            </tr>
            <tr>
              <td>ABCDE</td>
              <td>7</td>
              <td>5</td>
            </tr>
            <tr>
              <td>ABCDEFG</td>
              <td>3</td>
              <td>7</td>
            </tr>
        </table>
      </body>
    </META>
  </head>
</html>

Insomniac at large


Report •

#3
June 23, 2010 at 02:12:57
Thanks for your effort orangeboy...

Let me try your suggestion first while for now...

Let me make it more precise...

FileA.log is not equal to FileB.out

probably I was not clear enough...

my "FileA.log" looks like this:

Firm = 143,40
Address 1 = 183,70
Address 2 = 253,70
City = 323,28
State = 351,2
Zip = 353,5
Zip+4 = 358,4

and "FileB.out" looks like:

MATTY COLLEGE PRINCETON UNIV
32 CAMPBELL HALL
PRINCETON NJ085440001

(all in one line and the line is 430 chars long in this case, the length of the row changes as per the requirement of the user)

that is:

at pos 143 is Firm which is: MATTY COLLEGE PRINCETON
UNIV (length 40 chars)
at pos 183 is Address1 which is: 32 CAMPBELL HALL
(length 70 chars)
at pos 323 is City which is: PRINCETON (length 28 chars)
at pos 351 is State which is: TX (length 2 chars)
at pos 353 is Zip which is: 08544 (len 5 chars)
at pos 358 is Zip+4 which is 0001 (length 4 chars)


===================

Actual content of log file:

;
; Driver log file: D:\bin\SLKCycleN.log
; Created Wed Jun 23 05:44:04 2010.
;
Processing job file: D:\bin\SLKCycleN.job

FINALIST Job options in effect:

fInput=SLKCycleN.nam
fDefinition=SLKCycleN.def
fOutAllFile=OUTPUT/SLKCycleN.out
fOutValidFile= Not specified in job file and will not be created.
fOutErrorsFile= Not specified in job file and will not be created.
fReport=OUTPUT/SLKCycleN.rpt
fConfigFile=SLKCycleN-pbfn.cfg
cCase not specified. Using value specified in fConfigFile file.
cCassMode not specified. Using value specified in fConfigFile file.
rMailerName not specified. Using value specified in fConfigFile file.
rMailerAddress not specified. Using value specified in fConfigFile file.
rMailerCity not specified. Using value specified in fConfigFile file.
cAddrScan=NO
cStartAft=0
cStopAft=EOF
jOverlay=NO
jAttach=NO
cRecsz=0
cRecszOut=0
cInputFileType=ASCIICRLF
cDelimiter was not defined.
cXTern=NO
jAbbrevIt=NO

Input record size is 142 bytes.

Firm = 143,40
Address 1 = 183,70
Address 2 = 253,70
City = 323,28
State = 351,2
Zip = 353,5
Zip+4 = 358,4
LOT Code = 362,5
DPV Flags = 367,3
LACS Code = 370,1
Delivery Pt = 371,3
Match Level = 374,1
Default Match = 375,1
Carrier Rt = 376,4
Congress Dist.= 380,2
LACSLink RetCd= 382,2
SuiteLink Return Code= 384,2
SuiteLink Match Code= 386,1
SuiteLink Match Fidelity Code= 387,1
Advncd Bar Cd = 388,14
Error Flag = 402,4
DPV Foot Note = 406,12
DPV No-Stat Flag= 418,1
Abbr Cityname = 419,13


Record # - Error Code


Report •

Related Solutions

#4
June 23, 2010 at 07:22:40
Ah. Gotcha. Yeah, my example won't work then (I don't believe).
My assumption was for every row in FileA was a corresponding
row in FileB. But it looks like FileA needs to be a vertical search,
and FileB a horizontal parse. Let me see if I can make some
adjustments...
:-)

Edit: A couple other questions arise:
Do all the .log and .out files have the same name, just the different extensions? I'm guessing yes, so they can be easily related. i.e.,
FileA.log has FileA.out,
FileB.log has FileB.out, etc.

Do the .out files have more than one row? As in, if it takes ~50 rows in the .log file to describe row 1 in .out, will the next ~50 rows in .log describe row 2 in out, etc.?

Insomniac at large


Report •

#5
June 24, 2010 at 01:13:42
Well... your guess for the first question is 100% correct.
And for the second question it is partly correct. What I mean to
say is it has millions of records and therefore multiple rows. All
rows have different addresses and in every row the "Firm"
starts at 143 (len 40 chars max) and so on...

Report •

#6
June 24, 2010 at 08:17:17
I'm not exactly pleased with the speed, but the following will
create HTML files with a table for each row in *.OUT, with the
.LOG field names in one cell, and .LOG field values in
another:

::21746.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

::---------------------------------------------------------------------------::
::                          Site specific variables                          ::
::---------------------------------------------------------------------------::
SET log_path=\Your\Path\To\Input\Log\Files
SET htm_path=\Your\Path\For\Output\HTML\Files
SET table_width=800
SET cell_width=400

::---------------------------------------------------------------------------::
::                                Main logic                                 ::
::---------------------------------------------------------------------------::
:Find all LOG files, set filename as %%a 
FOR /F "delims=" %%a IN ('DIR /S /B "%log_path%\*.log"') DO (
    CALL :Make_File_Vars "%%a"
    CALL :HTML_Head
    :Read LOG file, set name, column, length as %%b, %%c, %%d 
    FOR /F "usebackq tokens=1-3 delims==," %%b IN (!log_file!) DO (
        :Get only valid field descriptors, where length has a value
        IF "%%d" NEQ "" (
            SET field_%%b_name=%%b
            SET field_%%b_col=%%c
            SET field_%%b_col=!field_%%b_col: =!
            SET field_%%b_len=%%d
        )
    )
    ECHO. Processing !out_file!. Please wait...
    :Read OUT file, set each line as %%e
    FOR /F "usebackq tokens=* delims=," %%e IN (!out_file!) DO (
        SET /A var_counter=0
        >>!htm_file! ECHO.        ^<table border="1" width="%table_width%"^>
        :Retrieve the name, column, length variables, and process using their values
        FOR /F "tokens=2 delims==" %%f IN ('SET field_') DO (
            SET /A var_counter+=1
            IF !var_counter! EQU 1  SET value_col=%%f
            IF !var_counter! EQU 2  SET value_len=%%f
            IF !var_counter! EQU 3 (
                CALL :Trim value_field %%f
                CALL :Get_Value !value_col! !value_len! "%%e"
                >>!htm_file! ECHO.            ^<tr^>
                >>!htm_file! ECHO.              ^<td width="%cell_width%"^>!value_field!^</td^>
                >>!htm_file! ECHO.              ^<td width="%cell_width%"^>!value!^</td^>
                >>!htm_file! ECHO.            ^</tr^>
                SET /A var_counter=0
            )
        )
        >>!htm_file! ECHO.            ^<br^>
        >>!htm_file! ECHO.        ^</table^>
    )
    CALL :HTML_Tail
)
EXIT /B

::---------------------------------------------------------------------------::
::                                Subroutines                                ::
::---------------------------------------------------------------------------::
:Make_File_Vars from all files found
SET log_file="%~1"
SET out_file="%~dpn1.out"
SET htm_file="%htm_path%\%~n1.html"
SET htm_title=%~n1%
GOTO :EOF

:Get_Value from OUT file line
SET /A col=%1-1
SET /A len=%2
SET line=%~3
SET value=!line:~%col%,%len%!
CALL :Trim value !value!
GOTO :EOF

:Trim trailing blanks
SET var=%1
FOR /F "tokens=1*" %%a IN ("%*") DO (
    SET trimmed_value=%%b
)
SET %var%=%trimmed_value%
GOTO :EOF

:HTML_Head creates HTML file's header
 >!htm_file! ECHO.^<html^>
>>!htm_file! ECHO.  ^<head^>
>>!htm_file! ECHO.    ^<META http-equiv="Content-Type"
>>!htm_file! ECHO.    content="text/html; charset=UTF-8"^>
>>!htm_file! ECHO.      ^<title^>!htm_title! Contents^</title^>
>>!htm_file! ECHO.      ^<body^>
>>!htm_file! ECHO.        ^<H1^>!htm_title! Contents:^</H1^>
>>!htm_file! ECHO.        ^<table border="1" width="%table_width%"^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th width="%cell_width%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width%"^>Field Value^</th^>
>>!htm_file! ECHO.            ^</tr^>
GOTO :EOF

:HTML_Tail creates HTML file's tail
>>!htm_file! ECHO.        ^</table^>
>>!htm_file! ECHO.      ^</body^>
>>!htm_file! ECHO.    ^</META^>
>>!htm_file! ECHO.  ^</head^>
>>!htm_file! ECHO.^</html^>
GOTO :EOF

Of course, adjust the "site specific variables" to suit your
needs! :-)

Insomniac at large


Report •

#7
June 25, 2010 at 04:36:40
That is too close... Awesome... Thanks... You rock dude...

Just one thing...

I need to display the position at which these values are in the log and out file.

In the mean time, I'll try to understand the code and the flow of your program this weekend. This will help me modify as per my requirement.


Report •

#8
June 25, 2010 at 06:12:49
Whoops! Column and length were in your first post, but I
forgot. Added two more cell to contain those values as well:

::21746.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

::---------------------------------------------------------------------------::
::                          Site specific variables                          ::
::---------------------------------------------------------------------------::
SET log_path=\Your\Path\To\Input\Log\Files
SET htm_path=\Your\Path\For\Output\HTML\Files
SET total_table_width=1000
SET string_cell_width=400
SET int_cell_width=100

::---------------------------------------------------------------------------::
::                                Main logic                                 ::
::---------------------------------------------------------------------------::
:Find all LOG files, set filename as %%a 
FOR /F "delims=" %%a IN ('DIR /S /B "%log_path%\*.log"') DO (
    CALL :Make_File_Vars "%%a"
    CALL :HTML_Head
    SET /A field_count=0
    :Read LOG file, set name, column, length as %%b, %%c, %%d 
    FOR /F "usebackq tokens=1-3 delims==," %%b IN (!log_file!) DO (
        :Get only valid field descriptors, where length has a value
        IF "%%d" NEQ "" (
            SET /A field_count+=1
            SET field_count_string=00!field_count!
            SET field_count_string=!field_count_string:~-3!
            SET field_!field_count_string!_name=%%b
            SET field_!field_count_string!_col=%%c
            SET field_!field_count_string!_len=%%d
        )
    )
    SET field_count=
    SET field_count_string=
    ECHO. Processing !out_file!. Please wait...
    :Read OUT file, set each line as %%e
    FOR /F "usebackq tokens=* delims=," %%e IN (!out_file!) DO (
        SET /A var_counter=0
        >>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
        :Retrieve the name, column, length variables, and process using their values
        FOR /F "tokens=2 delims==" %%f IN ('SET field_') DO (
            SET /A var_counter+=1
            IF !var_counter! EQU 1  SET value_col=%%f
            IF !var_counter! EQU 2  SET value_len=%%f
            IF !var_counter! EQU 3 (
                CALL :Trim value_field %%f
                CALL :Get_Value !value_col! !value_len! "%%e"
                >>!htm_file! ECHO.            ^<tr^>
                >>!htm_file! ECHO.              ^<td width="%string_cell_width%"^>!value_field!^</td^>
                >>!htm_file! ECHO.              ^<td width="%string_cell_width%"^>!value!^</td^>
                >>!htm_file! ECHO.              ^<td width="%int_cell_width%"^>!value_col!^</td^>
                >>!htm_file! ECHO.              ^<td width="%int_cell_width%"^>!value_len!^</td^>
                >>!htm_file! ECHO.            ^</tr^>
                SET /A var_counter=0
            )
        )
        >>!htm_file! ECHO.            ^<br^>
        >>!htm_file! ECHO.        ^</table^>
    )
    CALL :HTML_Tail
)
EXIT /B

::---------------------------------------------------------------------------::
::                                Subroutines                                ::
::---------------------------------------------------------------------------::
:Make_File_Vars from all files found
SET log_file="%~1"
SET out_file="%~dpn1.out"
SET htm_file="%htm_path%\%~n1.html"
SET htm_title=%~n1%
GOTO :EOF

:Get_Value from OUT file line
SET /A col=%1-1
SET /A len=%2
SET line=%~3
SET value=!line:~%col%,%len%!
CALL :Trim value !value!
GOTO :EOF

:Trim trailing blanks
SET var=%1
FOR /F "tokens=1*" %%a IN ("%*") DO (
    SET trimmed_value=%%b
)
SET %var%=%trimmed_value%
GOTO :EOF

:HTML_Head creates HTML file's header
 >!htm_file! ECHO.^<html^>
>>!htm_file! ECHO.  ^<head^>
>>!htm_file! ECHO.    ^<META http-equiv="Content-Type"
>>!htm_file! ECHO.    content="text/html; charset=UTF-8"^>
>>!htm_file! ECHO.      ^<title^>!htm_title! Contents^</title^>
>>!htm_file! ECHO.      ^<body^>
>>!htm_file! ECHO.        ^<H1^>!htm_title! Contents sorted by Field Column:^</H1^>
>>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th width="%string_cell_width%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%string_cell_width%"^>Field Value^</th^>
>>!htm_file! ECHO.              ^<th width="%int_cell_width%"^>Field Column^</th^>
>>!htm_file! ECHO.              ^<th width="%int_cell_width%"^>Field Length^</th^>
>>!htm_file! ECHO.            ^</tr^>
GOTO :EOF

:HTML_Tail creates HTML file's tail
>>!htm_file! ECHO.        ^</table^>
>>!htm_file! ECHO.      ^</body^>
>>!htm_file! ECHO.    ^</META^>
>>!htm_file! ECHO.  ^</head^>
>>!htm_file! ECHO.^</html^>
GOTO :EOF

Insomniac at large


Report •

#9
June 28, 2010 at 02:57:46
Great... It seems you have done lot of research on the batch
script... Yup... This is what I wanted.

What your script does is "Reads the file (LOG and OUT) from
one folder (say 'xx') and displays the result on an HTML
page". The HTML page displays the results in the below
format:
"LOG file info - OUT file info - Column - Length" i.e.
"Field Name - Field Value - Field Column - Field Length"

Can you make it in way that it reads the file from 2 folders (xx
and xy) considering that xx contains the results from build
F01 and xy contains the results from build F02 in order to
make the comparison easier? Also, the line which gets
printed is in alternate colors to identify easily.The HTML page
displays the results in the below format:
"LOG file 1 info - OUT file 1 info - Column - Length - LOG file 2
info - OUT file 2 info" i.e.
"Field Name (F01) - Field Value (F01) - Field Column - Field
Length - Field Name (F02) - Field Value (F02)".

Let me know if I confused you. :)


Report •

#10
June 28, 2010 at 03:53:52
Who doesn't like a challenge? :-D
I believe what you described is possible, with a few more lines of
code. I'm not well versed in HTML, but there are webpages I've
stumbled across that will help...

Insomniac at large


Report •

#11
June 28, 2010 at 05:21:48
Thanks-a-lot orangeboy...

Can I also become expert like you?
How can I get the knowledge about the scripting?
Where do I start from?
Is there any website or training material where I can start learning from the basic?


Report •

#12
June 28, 2010 at 08:25:11
Hmm... I'm hardly "expert". I'm sure some of the techniques I use
could be done more efficiently. But I will admit that I use some tricks
that I haven't seen others used! :-)

The way I learned was by doing. I would have a need, and
want to create my own solution without the requirement of
downloading some other executable. My "Program Files" folder is
usually big enough without adding yet another folder to it.

The first place to start learning command line scripting is by typing
HELP on the command line (Start > Run > CMD). HELP will list the
internal (and some external) commands it knows about. From there,
you can type HELP <listed command>. HELP ASSOC, for example.
That will list the syntax for the command, and a brief description of
how to use it.

Those commands are simply put into a text file, where variables can be created to make the final batch more flexible.

I also try to think about what needs to be done without thinking too much about what commands need to be used. Basically get a big picture of the problem, then work out each step of the way to get to the solution. Your example was that you had two files, and wanted a third file with data/elements/information from those files. Drilling down, the first problem was how to get that (useful) info from the first file. The next problem was how to get (useful) info from the second file. The last problem was how to put the two together into a third file. You drill down a little more with the first problem (which can or does lead to additional problems!) and think, "this command should do that". You try it out, and if it produces what you want, you move onto the next problem. If it doesn't, you take a look at what it did produce, and see if there are additional commands to fine tune the results. Wash. Rinse. Repeat. :-)

Other great places to learn (besides the HELP command) is right here! nbrane, Judago, IVO, Mechanix2Go, etc, have VERY good examples, most of the time in "one-liners" (or in very few lines).
Other places I recommend:

http://www.robvanderwoude.com/batch...
- comprehensive, has examples

http://ss64.com/nt/
- also comprehensive, with examples

http://www.google.com/
- everything under the sun

And by the way, the two folder source IS a challenge! :-)
I came back to this thread to get the code from "response 8",
because I'm now kinda lost, and need to see what I was doing! ;-)

Insomniac at large


Report •

#13
June 28, 2010 at 11:25:46
I peppered in a LOT of REM statements, in an attempt to document what is going on.
I also abused some of the batch labels (statements that start with ":") by adding
additional text.
See if this will work for you:

::21746.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

::---------------------------------------------------------------------------::
::                          Site specific variables                          ::
::---------------------------------------------------------------------------::
SET log_path1=\Your\Path\To\Input\Log\Files
SET log_path2=\Your\Other\Path\To\Input\Log\Files
SET htm_path=\Your\Path\For\Output\HTML\Files

SET total_table_width=1200
SET cell_width_field=200
SET cell_width_value=350
SET cell_width_integer=50
REM total_table_width should be equal to:
REM (cell_width_field x 2) + (cell_width_integer x 2) + (cell_width_value x 2)

SET file1_color=background-color:LightGreen
SET file2_color=background-color:Red
SET int_alignment=text-align:Center
REM Background color names found here:
REM <a href="http://www.w3schools.com/tags/ref_colornames.asp" target="_blank">http://www.w3schools.com/tags/ref_c...</a>

SET /A cell_width_header=%cell_width_field%+%cell_width_value%
::---------------------------------------------------------------------------::
::                                Main logic                                 ::
::---------------------------------------------------------------------------::
:: Find all LOG files in first log path, set filename as %%a
FOR /F "delims=" %%a IN ('DIR /S /B "%log_path1%\*.log"') DO (
    REM Create variables from found filenames
    CALL :Make_File_Vars "%%a"
    REM Create top of HTML file
    CALL :HTML_Head
    SET /A field_count=0
    REM Read LOG file, set name, column, length as %%b, %%c, %%d
    FOR /F "usebackq tokens=1-3 delims==," %%b IN (!log_file!) DO (
        REM Get only valid field descriptors, where length has a value
        IF "%%d" NEQ "" (
            SET /A field_count+=1
            SET field_count_string=00!field_count!
            SET field_count_string=!field_count_string:~-3!
            SET field_!field_count_string!_name=%%b
            SET field_!field_count_string!_col=%%c
            SET field_!field_count_string!_len=%%d
        )
    )
    REM Nullify field variables no longer needed
    SET field_count=
    SET field_count_string=
    REM Require a second file for comparison
    IF EXIST "%log_path2%\!out_file!" (
        ECHO. Processing !out_file!. Please wait...
        SET /A skip_amount=0
        SET skip= 
        REM Read original OUT file, one row at a time
        FOR /F "usebackq" %%e IN ("%log_path1%\!out_file!") DO (
            REM Create a table for this row
            >>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
            SET /A var_counter=0
            REM Interrogate field variables created from log file
            FOR /F "tokens=2 delims==" %%f IN ('SET field_') DO (
                REM Field variables come in triplets, so look for 3 variables at a time
                SET /A var_counter+=1
                IF !var_counter! EQU 1  SET value_col=%%f
                IF !var_counter! EQU 2  SET value_len=%%f
                IF !var_counter! EQU 3 (
                    REM Remove trailing spaces from value field
                    CALL :Trim value_field %%f
                    REM Read the same row of both OUT files
                    CALL :Read_File_Line "!skip!" "%log_path1%\!out_file!" file1_value
                    CALL :Read_File_Line "!skip!" "%log_path2%\!out_file!" file2_value
                    REM Build row in HTML table
                    >>!htm_file! ECHO.            ^<tr^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_field%" style="%file1_color%"^>!value_field!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_value%" style="%file1_color%"^>!file1_value!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_integer%" style="%int_alignment%"^>!value_col!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_integer%" style="%int_alignment%"^>!value_len!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_field%" style="%file2_color%"^>!value_field!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_value%" style="%file2_color%"^>!file2_value!^</td^>
                    >>!htm_file! ECHO.            ^</tr^>
                    REM All triplet variables found, reset counter
                    SET /A var_counter=0
                )
                REM Done with searching for triplet variables
            )
            REM Done with interrogating field variables,
            REM Close out the table for that row
            >>!htm_file! ECHO.            ^<br^>
            >>!htm_file! ECHO.        ^</table^>
            REM Set up batch to read only the next row of the OUT files
            SET /A skip_amount+=1
            SET skip=skip=!skip_amount!
        )
        REM Done with ALL rows in the OUT files,
        REM Finalize the HTML file
        CALL :HTML_Tail
    ) ELSE (
        REM Second OUT file was not found, say something about it!
        ECHO "%log_path2%\!out_file!" does not exist^!
        REM Clean up empty HTML file
        DEL !htm_file!
    )
    REM Done with that LOG file
)
REM Done with ALL LOG files
EXIT /B

::---------------------------------------------------------------------------::
::                                Subroutines                                ::
::---------------------------------------------------------------------------::
:Make_File_Vars from all files found, input is a LOG filename
SET log_file="%~1"
SET out_file=%~n1.out
SET htm_file="%htm_path%\%~n1.html"
SET htm_title=%~n1%
GOTO :EOF

:Get_Value from OUT file line, input is three "field" variables
SET /A col=%1-1
SET /A len=%2
SET line=%~3
SET value=!line:~%col%,%len%!
CALL :Trim value !value!
GOTO :EOF

:Trim trailing blanks from a value, input is a variable name and it's value
SET var=%1
FOR /F "tokens=1*" %%a IN ("%*") DO (
    SET trimmed_value=%%b
)
SET %var%=%trimmed_value%
GOTO :EOF

:Read_File_Line from an OUT file, at a specific row. Input is the last row to skip past, filename, and variable name.
SET skip=%~1
FOR /F "%skip%usebackq tokens=* delims=," %%g IN (%2) DO (
    CALL :Get_Value !value_col! !value_len! "%%g"
    SET %3=!value!
    @ECHO OFF
    GOTO :EOF
)
GOTO :EOF

:HTML_Head creates HTML file's header
 >!htm_file! ECHO.^<html^>
>>!htm_file! ECHO.  ^<head^>
>>!htm_file! ECHO.    ^<META http-equiv="Content-Type"
>>!htm_file! ECHO.    content="text/html; charset=UTF-8"^>
>>!htm_file! ECHO.      ^<title^>!htm_title! Contents^</title^>
>>!htm_file! ECHO.      ^<body^>
>>!htm_file! ECHO.        ^<H1^>!htm_title! Contents sorted by Field Column:^</H1^>
>>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_header%" style="%file1_color%"^>%log_path1%\!htm_title!^</th^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_integer%"^>Attributes^</th^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_header%"  style="%file2_color%"^>%log_path2%\!htm_title!^</th^>
>>!htm_file! ECHO.            ^</tr^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th width="%cell_width_field%" style="%file1_color%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_value%" style="%file1_color%"^>Field Value^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_integer%"^>^(col^)^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_integer%"^>^(len^)^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_field%" style="%file2_color%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_value%" style="%file2_color%"^>Field Value^</th^>
>>!htm_file! ECHO.            ^</tr^>
>>!htm_file! ECHO.        ^</table^>
GOTO :EOF

:HTML_Tail creates HTML file's tail
>>!htm_file! ECHO.        ^</table^>
>>!htm_file! ECHO.      ^</body^>
>>!htm_file! ECHO.    ^</META^>
>>!htm_file! ECHO.  ^</head^>
>>!htm_file! ECHO.^</html^>
GOTO :EOF

Insomniac at large


Report •

#14
June 29, 2010 at 23:58:30
That is mind blowing... It works great...

I visited the links you provided... That is awesome... So much
of information...

What I'm doing right now is executing the commands given on
the links provided by you, checking the help, trying to
understand what it does and thinking how to implement it.
Also looking for the info about "SETLOCAL
ENABLEDELAYEDEXPANSION".

You are Genius... Be my Guru... will trouble you later...


Report •

#15
June 30, 2010 at 04:17:59
What I'm doing right now is executing the commands given on
the links provided by you, checking the help, trying to
understand what it does and thinking how to implement it.

Sounds like the right thing to do! Think of the commands as
tools in a toolbox. Get an idea about what each tool does, and
how they may work together (see Rob's page on "Redirection"). Then when you're working on a problem, you'll be able to reach into the toolbox and grab the right tool or tools to
get to the solution. Continuing with the "toolbox" concept, other
languages such as python and java expands your toolbox
astoundingly.


Also looking for the info about "SETLOCAL
ENABLEDELAYEDEXPANSION".

HELP SETLOCAL and
SET /?

I think ENABLEDELAYEDEXPANSION was/is the hardest
concept for me to understand, and half the time I still don't get
it right. Using ECHO in key spots to display variables can help
tremendously, such as:

ECHO variable="%variable%" or "!variable!"
or
ECHO.%variable%

Using a dot immediately after ECHO prevents the "ECHO is
ON/OFF" message if the variable is undefined.

Insomniac at large


Report •

#16
June 30, 2010 at 08:20:57
orangeboy wrote: ECHO.%variable%

Using a dot immediately after ECHO prevents the "ECHO is
ON/OFF" message if the variable is undefined.

That's a really good tip, actually! It also seems to work
for a variable that only contains spaces. Apparently,
such a variable is considered to be defined but it won't echo properly...



Report •

#17
July 1, 2010 at 03:13:14
Dude... Any idea as to why "Advncd Bar Cd" is not getting
printed?

My outfile shows the Advncd Bar Cd = !085440001008!

It should not truncate the special character as well.


Report •

#18
July 1, 2010 at 07:16:28
Ouch.
Because EnableDelayedExpansion is set, the batch is
interpreting !085440001008! as a variable named
085440001008, which doesn't have a value. In the
:Read_File_Line routine, %%g represents the row from the
OUT file, with what the batch thinks has an embedded
undefined !<variable>! in it, and nullifies those 14 positions.
That's why Error Flag's value is now in Advncd Bar Cd's field.

Ouch.
I'm going to have to think about this one... :-(

Insomniac at large


Report •

#19
July 1, 2010 at 11:36:01
<explative deleted>!
I think I'm close. I wrote the following to help me get a grasp of what needs
to happen. It's pretty ugly, with more CALLs to try to accomplish the
solution:

@ECHO OFF
SETLOCAL

REM offset ----+----1----+----2----+----3----+----4----+----5
REM offset          0         0         0         0         0
SET string=123456789!This is the Text I want!5678901234567890
REM length          ----+----1----+----2----+
REM length                   0         0     

SET offset=9
SET len=25

FOR /F "delims=" %%a IN ("%string%") DO (
    CALL :String_1 "%%a"
)
ECHO.%text%
EXIT /B

:String_1
SET text1=%~1
CALL :String_2 %offset% %len%
GOTO :EOF

:String_2
CALL SET text=%%text1:~^%1,^%2%%
GOTO :EOF

I'm pretty sure I'm going to have to add at least one of each of these
statements to get the final values into the HTML files:

    ENDLOCAL
    SETLOCAL DISABLEDELAYEDEXPANSION

Bleh. ;-)

EDIT: What a PITA DelayedExpansion can be!

Insomniac at large


Report •

#20
July 1, 2010 at 16:44:52
This one made my head hurt.
"Advncd Bar Cd" now accurately represents it's field, and all
special characters remain intact:

::21746.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

::---------------------------------------------------------------------------::
::                          Site specific variables                          ::
::---------------------------------------------------------------------------::
SET log_path1=\Your\Path\To\Input\Log\Files
SET log_path2=\Your\Other\Path\To\Input\Log\Files
SET htm_path=\Your\Path\For\Output\HTML\Files

SET total_table_width=1200
SET cell_width_field=200
SET cell_width_value=350
SET cell_width_integer=50
REM total_table_width should be equal to:
REM (cell_width_field x 2) + (cell_width_integer x 2) + (cell_width_value x 2)

SET file1_color=background-color:LightGreen
SET file2_color=background-color:Red
SET int_alignment=text-align:Center
REM Background color names found here:
REM <a href="http://www.w3schools.com/tags/ref_colornames.asp" target="_blank">http://www.w3schools.com/tags/ref_c...</a>

SET /A cell_width_header=%cell_width_field%+%cell_width_value%

REM Angry this has to happen:
SET temp_bat="%~dp0\Read_File_Line.bat"
 >%temp_bat% TYPE NUL
>>%temp_bat% ECHO @ECHO OFF
>>%temp_bat% ECHO :Read_File_Line from an OUT file, at a specific row. Input is the last row to skip past, filename, and variable name.
>>%temp_bat% ECHO SETLOCAL DISABLEDELAYEDEXPANSION
>>%temp_bat% ECHO SET skip=%%~1
>>%temp_bat% ECHO SET value_col=%%3 
>>%temp_bat% ECHO SET value_len=%%4
>>%temp_bat% ECHO SET file_color=%%5 
>>%temp_bat% ECHO SET htm_file=%%6
>>%temp_bat% ECHO FOR /F "%%skip%%usebackq tokens=* delims=," %%%%g IN (%%2) DO (
>>%temp_bat% ECHO     CALL :String_1 "%%%%g" 
>>%temp_bat% ECHO     GOTO :EOF
>>%temp_bat% ECHO )
>>%temp_bat% ECHO GOTO :EOF
>>%temp_bat% ECHO.
>>%temp_bat% ECHO :String_1
>>%temp_bat% ECHO SET text1=%%~1
>>%temp_bat% ECHO CALL :String_2 %%value_col%% %%value_len%% %%2
>>%temp_bat% ECHO GOTO :EOF
>>%temp_bat% ECHO.
>>%temp_bat% ECHO :String_2
>>%temp_bat% ECHO CALL SET text2=%%%%text1:~^^%%1,^^%%2%%%%
>>%temp_bat% ECHO CALL :Trim text %%text2%%
>>%temp_bat% ECHO ^>^>%%htm_file%% ECHO.              ^^^<td width="%%cell_width_value%%" style="%%file_color%%"^^^>%%text%%^^^</td^^^>
>>%temp_bat% ECHO GOTO :EOF
>>%temp_bat% ECHO.
>>%temp_bat% ECHO :Trim trailing blanks from a value, input is a variable name and it's value
>>%temp_bat% ECHO SET var=%%1
>>%temp_bat% ECHO FOR /F "tokens=1*" %%%%a IN ("%%*") DO (
>>%temp_bat% ECHO     SET trimmed_value=%%%%b
>>%temp_bat% ECHO )
>>%temp_bat% ECHO SET %%var%%=%%trimmed_value%%
>>%temp_bat% ECHO GOTO :EOF

::---------------------------------------------------------------------------::
::                                Main logic                                 ::
::---------------------------------------------------------------------------::
:: Find all LOG files in first log path, set filename as %%a
FOR /F "delims=" %%a IN ('DIR /S /B "%log_path1%\*.log"') DO (
    REM Create variables from found filenames
    CALL :Make_File_Vars "%%a"
    REM Create top of HTML file
    CALL :HTML_Head
    SET /A field_count=0
    REM Read LOG file, set name, column, length as %%b, %%c, %%d
    FOR /F "usebackq tokens=1-3 delims==," %%b IN (!log_file!) DO (
        REM Get only valid field descriptors, where length has a value
        IF "%%d" NEQ "" (
            SET /A field_count+=1
            SET field_count_string=00!field_count!
            SET field_count_string=!field_count_string:~-3!
            SET field_!field_count_string!_name=%%b
            SET field_!field_count_string!_col=%%c
            SET field_!field_count_string!_len=%%d
        )
    )
    REM Nullify field variables no longer needed
    SET field_count=
    SET field_count_string=
    REM Require a second file for comparison
    IF EXIST "%log_path2%\!out_file!" (
        ECHO. Processing !out_file!. Please wait...
        SET /A skip_amount=0
        SET skip= 
        REM Read original OUT file, one row at a time
        FOR /F "usebackq" %%e IN ("%log_path1%\!out_file!") DO (
            REM Create a table for this row
            >>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
            SET /A var_counter=0
            REM Interrogate field variables created from log file
            FOR /F "tokens=2 delims==" %%f IN ('SET field_') DO (
                REM Field variables come in triplets, so look for 3 variables at a time
                SET /A var_counter+=1
                IF !var_counter! EQU 1  SET value_col=%%f
                IF !var_counter! EQU 2  SET value_len=%%f
                IF !var_counter! EQU 3 (
                    SET /A value_off=!value_col!-1
                    REM Remove trailing spaces from value field
                    CALL :Trim value_field %%f
                    REM Build row in HTML table
                    >>!htm_file! ECHO.            ^<tr^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_field%" style="%file1_color%"^>!value_field!^</td^>
                    REM Get unmolested field from first OUT file
                    CALL Read_File_Line "!skip!","%log_path1%\!out_file!",!value_off!,!value_len!,%file1_color%,!htm_file!
                    >>!htm_file! ECHO.              ^<td width="%cell_width_integer%" style="%int_alignment%"^>!value_col!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_integer%" style="%int_alignment%"^>!value_len!^</td^>
                    >>!htm_file! ECHO.              ^<td width="%cell_width_field%" style="%file2_color%"^>!value_field!^</td^>
                    REM Get unmolested field from second OUT file
                    CALL Read_File_Line "!skip!","%log_path2%\!out_file!",!value_off!,!value_len!,%file2_color%,!htm_file!
                    >>!htm_file! ECHO.            ^</tr^>
                    REM All triplet variables found, reset counter
                    SET /A var_counter=0
                )
                REM Done with searching for triplet variables
            )
            REM Done with interrogating field variables,
            REM Close out the table for that row
            >>!htm_file! ECHO.            ^<br^>
            >>!htm_file! ECHO.        ^</table^>
            REM Set up batch to read only the next row of the OUT files
            SET /A skip_amount+=1
            SET skip=skip=!skip_amount!
        )
        REM Done with ALL rows in the OUT files,
        REM Finalize the HTML file
        CALL :HTML_Tail
    ) ELSE (
        REM Second OUT file was not found, say something about it!
        ECHO "%log_path2%\!out_file!" does not exist^!
        REM Clean up empty HTML file
        DEL !htm_file!
    )
    REM Done with that LOG file
)
REM Done with ALL LOG files,
REM Delete temp bat that reads OUT files
DEL %temp_bat%
EXIT /B

::---------------------------------------------------------------------------::
::                                Subroutines                                ::
::---------------------------------------------------------------------------::
:Trim trailing blanks from a value, input is a variable name and it's value
SET var=%1
FOR /F "tokens=1*" %%a IN ("%*") DO (
    SET trimmed_value=%%b
)
SET %var%=%trimmed_value%
GOTO :EOF

:Make_File_Vars from all files found, input is a LOG filename
SET log_file="%~1"
SET out_file=%~n1.out
SET htm_file="%htm_path%\%~n1.html"
SET htm_title=%~n1%
GOTO :EOF

:HTML_Head creates HTML file's header
 >!htm_file! ECHO.^<html^>
>>!htm_file! ECHO.  ^<head^>
>>!htm_file! ECHO.    ^<META http-equiv="Content-Type"
>>!htm_file! ECHO.    content="text/html; charset=UTF-8"^>
>>!htm_file! ECHO.      ^<title^>!htm_title! Contents^</title^>
>>!htm_file! ECHO.      ^<body^>
>>!htm_file! ECHO.        ^<H1^>!htm_title! Contents sorted by Field Column:^</H1^>
>>!htm_file! ECHO.        ^<table border="1" width="%total_table_width%"^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_header%" style="%file1_color%"^>%log_path1%\!htm_title!^</th^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_integer%"^>Attributes^</th^>
>>!htm_file! ECHO.              ^<th colspan="2" width="%cell_width_header%"  style="%file2_color%"^>%log_path2%\!htm_title!^</th^>
>>!htm_file! ECHO.            ^</tr^>
>>!htm_file! ECHO.            ^<tr^>
>>!htm_file! ECHO.              ^<th width="%cell_width_field%" style="%file1_color%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_value%" style="%file1_color%"^>Field Value^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_integer%"^>^(col^)^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_integer%"^>^(len^)^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_field%" style="%file2_color%"^>Field Name^</th^>
>>!htm_file! ECHO.              ^<th width="%cell_width_value%" style="%file2_color%"^>Field Value^</th^>
>>!htm_file! ECHO.            ^</tr^>
>>!htm_file! ECHO.        ^</table^>
GOTO :EOF

:HTML_Tail creates HTML file's tail
>>!htm_file! ECHO.        ^</table^>
>>!htm_file! ECHO.      ^</body^>
>>!htm_file! ECHO.    ^</META^>
>>!htm_file! ECHO.  ^</head^>
>>!htm_file! ECHO.^</html^>
GOTO :EOF

It took me awhile to figure out how to escape a redirect when trying to
redirect into the temp batch file...

Insomniac at large


Report •

#21
July 2, 2010 at 00:57:51
Great... This handles the outfile completely. Dude... can you
please tell whether it can handle a huge out file? Coz the outfile I
gave you was in KB. I do have files that generates huge outfile,
approx 2 GB. I can understand that it will take huge time to
process. The idea is that it should not crash and should produce
correct HTML.

However, Loads of thanks for the effort and help to write and
guide.


Report •

#22
July 2, 2010 at 06:29:23
can you please tell whether it can handle a huge out file?

In theory it should handle any size OUT file, since it's reading
only one line at a time from those types of files. Issues might
arise with very large LOG files, since the schema(?) is being
stored as variables. I thought about fetching an entire OUT file in
a single read operation to reduce I/O operations, but against that
to keep things simple.

Insomniac at large


Report •

#23
July 12, 2010 at 04:33:46
Dude... Ummm... Getting some new idea... Here's a new
challenging part... Can it be possible with the script that it
reads the "file1.def" to get the input position and length, reads
"file1.nam" to get the input data at the position and length specified in "file1.def"
then read "file1.log" to get the output position and length,
reads "file1.out" to get the output data at the position and
length specified in "file1.out". Then print the same in the "file1.html" which will have
the columns designed as:

iColumn - iLength - iField Name - iField Value - oColumn - oLength - oField Name - oField Value


Report •

#24
July 12, 2010 at 07:45:03
I'll have to think about that one.

When your only tool is a hammer, every problem looks like a nail.


Report •

#25
July 21, 2010 at 01:14:43
Dude... Any update on my query...

Report •

#26
July 21, 2010 at 03:07:15
Gosh, why do people like to use batch for parsing files and manipulating strings ? @OP, if you can, try to learn to use a language, eg Python/Perl to do such things. First, they are created to do such tasks like what you want easily. Plus the abundance of modules/libraries which can make your programming/admin task easier. You won't even need to wait 1 month for a batch solution.

@OP, i would very much like to help you, but i still don't know what you really want. Getting those positions in fileA is easy, but how do you want to compare them with fileB , you did not state clearly.


GNU win32 packages | Gawk


Report •

#27
July 21, 2010 at 06:58:41

Gosh, why do people like to use batch for parsing files and manipulating strings ? @OP, if you can, try to learn to use a language, eg Python/Perl to do such things. First, they are created to do such tasks like what you want easily. Plus the abundance of modules/libraries which can make your programming/admin task easier. You won't even need to wait 1 month for a batch solution.

It's possible that there are some situations where corporate policy disallows downloading of said modules and libraries, and a user is limited to what is at hand. I'll let the OP fill in those blanks.

As far as waiting a little over a week*, I'd have to say that's my fault. I had/have been focused on a project of my own, and this one slipped my mind...

This is voluntary after all, and at least I know I'm not getting a paycheck for the help I provide! :-D

When your only tool is a hammer, every problem looks like a nail.


Report •

#28
July 21, 2010 at 08:07:43
In a corporate environment where such policies are in place, the user has no business creating their own batch files in their workplace. These are left to in house or contract programmers. Further, in the real business world where IT technologies are used for everyday operations, its only wise to employ the use of whatever tools a programmer/administrator needs to make their job easier and more efficient (productivity) to achieve their business goals. Those draconian policies that you mention, ie "cannot download modules/libraries" only serve to hinder business competitiveness. A sane management would certainly abolish these kind of thinking.

GNU win32 packages | Gawk


Report •

#29
July 22, 2010 at 15:04:42
ghostdog: sane management
<Insert oxymoron joke here>

Report •

#30
July 22, 2010 at 18:25:51
@Razor2.3 - I think you just did!

When your only tool is a hammer, every problem looks like a nail.


Report •

#31
July 22, 2010 at 20:59:01
Well... First thing, it's a long story. In short, this software has not been tested since 1978 and nothing is in place. That's the reason we got hired in 2008. This was due to the fact that most of the customers complained about lots of bugs. That's when they decided to QA this product. Second, comparison of two files were to compare two builds. Third, the one I've asked for now, is to check the health of the build. Fourth, we are forced to use batch script as it can communicate with AIX, HPUX, Solaris, Redhat, zSUSE, Mainframe and Windows.

Report •


Ask Question