Reading Columns & Output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#31 Post by abc0502 » 13 Feb 2013 01:34

OK, Fixed it, Thanks to jeb the HTML code now is working fine, i will just add a head for the Table, then will post it.
His post is here

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#32 Post by abc0502 » 13 Feb 2013 01:50

Edited:
>There was a problem when echo the <DIR> word to the HTML but solved now
>The time was trimmed from the start by one character and also fixed now


This work now perfectly.
Gave it a try :D

You don't have to delete the "myResult.csv" and the "Report.html" every time, the batch will delete before each run

Code: Select all

:: beginning of batch file
@Echo off
dir c:\windows\ /t:caw /x /q /a /o:-g-ds |find "/" > WinDir1.txt

:: code start to identify number of lines in a text
Set FileName=WinDir1.txt
Set /a LineNumb=0
for /f %%a in ('find /c /v "" ^< %FileName%') do set /a LineNumb=%%a
Echo %FileName% has %LineNumb% lines.
:: code end to identify number of lines in a text

setlocal enabledelayedexpansion

SET /A maxlines=116
SET /A linecount=0

del myResults.csv 2>nul
del report.html 2>nul

FOR /F "delims=" %%b IN (WinDir1.txt) DO (
IF !linecount! EQU %maxlines% GOTO ExitLoop
set "field=%%b"
set "type=!field:~21,17!"
if "!type!" == "   <DIR>         " ( set "third=DIR"
) else ( set third=!type! )
call :next "!field:~0,10!" "!field:~12,8!" "!third!" "!field:~39,12!" "!field:~52,23!" "!field:~75!"
SET /A linecount+=1
)

:ExitLoop
del WinDir1.txt


:: end of subroutine
:: end of batch file

REM =============== HTML =======================
REM html file part 1
CALL :HTML_P1
REM html file rows

   
setlocal DisableDelayedExpansion
for /f "EOL=: delims=" %%L in ('type "myResults.csv"') do (
  set "line=%%L"
  setlocal EnableDelayedExpansion
  set "preparedLine=#!line:;=;#!"
  FOR /F "tokens=1-6 delims=;" %%a in ("!preparedLine!") DO (
    endlocal
    set "param1=%%a"
    set "param2=%%b"
   set "param3=%%c"
   set "param4=%%d"
   set "param5=%%e"
   set "param6=%%f"
    setlocal EnableDelayedExpansion
    set "param1=!param1:~1!"
    set "param2=!param2:~1!"
   set "param3=!param3:~1!"
   set "param4=!param4:~1!"
   set "param5=!param5:~1!"
   set "param6=!param6:~1!"
    call :Row "!param1!" "!param2!" "!param3!" "!param4!" "!param5!" "!param6!"
    endlocal
  )
)
REM html file part 2
CALL :HTML_P2

pause
Exit /B
:HTML_P1
(
Echo ^<HTML^>
Echo    ^<head^>
Echo       ^<style media="screen" type="text/css"^>
Echo          html {
Echo             background-color: Black;
Echo             color: Orange;
Echo             }
Echo.
Echo          table {
Echo             border: 2px solid red;
Echo             padding: 2px;
Echo             text-align: center;
Echo             }
Echo.         
Echo          td {
Echo             width: 200px;
Echo             height: 20px;
Echo             border: 1px solid gray;
Echo             }
Echo.         
Echo          th {
Echo             badding-botton: 5px;
Echo             color: Red;
Echo             }
Echo       ^</style^>
Echo    ^</head^>
Echo    ^<body^>
Echo       ^<h2^ Style="text-align: center"^>Report^</h2^>
Echo       ^<h3^ Style="text-align: center"^>%Date% - %time%^</h3^>
Echo       ^<table^>
Echo         ^<thead^>
Echo           ^<tr^>
Echo          ^<th^>Date^</th^>
Echo          ^<th^>Time^</th^>
Echo          ^<th^>Size^</th^>
Echo          ^<th^>Short Name^</th^>
Echo          ^<th^>File Owner^</th^>
Echo          ^<th^>File Name^</th^>
Echo          ^</tr^>
Echo       ^</thead^>
Echo       ^<tbody^>       
)>>"Report.html"
GOTO :EOF

:Row <take_the_6_data_fields>
(
Echo          ^<tr^>
Echo             ^<td^>%~1^</td^>
Echo             ^<td^>%~2^</td^>
Echo             ^<td^>%~3^</td^>
Echo             ^<td^>%~4^</td^>
Echo             ^<td^>%~5^</td^>
Echo             ^<td^>%~6^</td^>
Echo          ^</tr^>
)>>"Report.html"
GOTO :EOF

:HTML_P2
(
Echo       ^</tbody^>
Echo       ^</table^>
Echo       ^<h5^ style="text-align: center; color:white"^>DosTips. com^</h5^>
Echo    ^</body^>
Echo ^</html^>
)>>"Report.html"
GOTO :EOF

:next
set "line="
set c=0
:next2
set /a c=c+1
set tokens="tokens=*"
if %c% EQU 3 set tokens=
for /f %tokens% %%a in ("%~1") do call set line=%%line%%;%%a
shift
if not "%~1"=="" goto :next2
>> myResults.csv echo %line:~1%

I added extra CSS styling to the HTML, you can control the color and cells width and hight from this section

Code: Select all

      <style media="screen" type="text/css">
         html {
            background-color: Black;
            color: Orange;
            }

         table {
            border: 2px solid red;
            padding: 2px;
            text-align: center;
            }
         
         td {
            width: 200px;
            height: 20px;
            border: 1px solid gray;
            }
         
         th {
            badding-botton: 5px;
            color: Red;
            }
      </style>
Last edited by abc0502 on 13 Feb 2013 19:11, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#33 Post by foxidrive » 13 Feb 2013 04:16

I also rejigged the code - here is what I ended up with:

EDIT 1 : Simplified the 6 :ROW entries

Code: Select all

:: beginning of batch file
@Echo off
dir c:\windows\ /t:caw /x /q /a /o:-g-ds |find "/" > WinDir1.txt

:: code start to identify number of lines in a text
Set FileName=WinDir1.txt
Set /a LineNumb=0
for /f %%a in ('find /c /v "" ^< %FileName%') do set /a LineNumb=%%a
Echo %FileName% has %LineNumb% lines.
:: code end to identify number of lines in a text

setlocal enabledelayedexpansion

SET /A maxlines=116
SET /A linecount=0

del myResults.csv 2>nul
del Report.html 2>nul

FOR /F "delims=" %%b IN (WinDir1.txt) DO (
IF !linecount! EQU %maxlines% GOTO ExitLoop
set "field=%%b"
call :next "!field:~0,10!" "!field:~13,7!" "!field:~21,17!" "!field:~39,12!" "!field:~52,23!" "!field:~75!"
SET /A linecount+=1
)

:ExitLoop
del WinDir1.txt

REM =============== HTML =======================
REM html file part 1
CALL :HTML_P1
REM html file rows
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In ('Type "myResults.csv"') Do (
   CALL :Row %%A
   )
REM html file part 2
CALL :HTML_P2

pause
Exit /B
:HTML_P1
(
Echo ^<HTML^>
Echo    ^<head^>
Echo       ^<style media="screen" type="text/css"^>
Echo          html {
Echo             background-color: Black;
Echo             color: Orange;
Echo             }
Echo.
Echo          table {
Echo             border: 2px solid red;
Echo             padding: 2px;
Echo             }
Echo.         
Echo          td {
Echo             width: 120px;
Echo             height: 30px;
Echo             border: 1px solid gray;
Echo             }
Echo       ^</style^>
Echo    ^</head^>
Echo    ^<body^>
Echo       ^<table^>
)>>"Report.html"
GOTO :EOF

:Row <take_the_6_data_fields>

>>"Report.html" Echo          ^<tr^>
>>"Report.html" <nul set /p "=             <td>%~1</td>"
>>"Report.html" <nul set /p "=             <td>%~2</td>"

REM === handling of "< and >" to display in HTML

if "%~3"=="<DIR>" (
>>"Report.html" <nul set /p "=             <td>&lt;DIR&gt;</td>"
) else (
>>"Report.html" <nul set /p "=             <td>%~3</td>"
)

REM === field 4 can be empty so special handling is needed

if "%~4"=="" (
>>"Report.html" <nul set /p "=             <td>&nbsp;</td>"
) else (
>>"Report.html" <nul set /p "=             <td>%~4</td>"
)

>>"Report.html" <nul set /p "=             <td>%~5</td>"
>>"Report.html" <nul set /p "=             <td>%~6</td>"
>>"Report.html" Echo.
>>"Report.html" Echo          ^</tr^>

GOTO :EOF

:HTML_P2
(
Echo       ^</table^>
Echo    ^</body^>
Echo ^</html^>
)>>"Report.html"
GOTO :EOF

:next
set "line="
set c=0
:next2
set /a c=c+1
set tokens="tokens=*"
if %c% EQU 3 set tokens=
for /f %tokens% %%a in ("%~1") do call set line=%%line%%,"%%a"
shift
if not "%~1"=="" goto :next2
>> myResults.csv echo %line:~1%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#34 Post by foxidrive » 13 Feb 2013 04:21

abc0502 wrote:This is the final unless something happens


It still breaks on (1) in the filename with this error:

.log</td> was unexpected at this time.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#35 Post by foxidrive » 13 Feb 2013 04:43

abc0502 wrote:The Parentheses explain every thing, while i was taking the <DIR> it was failling and when i removed the < and > every thing was fixed.
the code can't escape them.


I could parse the <DIR> fine, but the HTML doesn't render the < and > so I had to convert them to &lt; and &gt;

I simplified my code in the post above.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#36 Post by abc0502 » 13 Feb 2013 04:55

HTML doesn't render the < and > so I had to convert them to &lt; and &gt;
This is a nice trick :)

Does your Final Report get the time in right formation, I had to change "!field:~13,7!" to "!field:~12,8!" to get it right.
The HTML file looks better now after filling the missing cells :)

I will try again on testing files with names containing Parentheses and add the CSS style to your code ... as soon as i get some sleep :lol:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#37 Post by foxidrive » 13 Feb 2013 05:41

I think Windows 8 moved columns slightly because I also had to rejig a batch file that uses a DIR command to create a list and the columns had moved.

So in Windows 8 the time works ok. The HTML is great, looks good.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Reading Columns & Output

#38 Post by booga73 » 13 Feb 2013 10:11

:mrgreen: :mrgreen: :arrow: :mrgreen: :mrgreen:

I am floored, absolutely floored with the tremendous amount of work gone into the EXTRA coding. I initially posted my request for help for the sole purpose to simply get help with taking a text file, preserve the cell position of the data and empty cell positions. That is it. I got more involvement with a team effort from the DosTips forum then I could have imagined.

I do indeed express my thank you to your collaborated efforts; it is amazing.

best regards, Booga73
:idea: :arrow: :shock: . . . :D ---> :mrgreen:

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#39 Post by abc0502 » 13 Feb 2013 18:16

@Foxidrive, Did you fixed the parentheses problem in your last code ?
I created a file with the same name "WindowsUpdate (1).log" and added it to windows directory, and every thing runs ok, no problems or error messages, if you didn't fix it then the problem could happen because of the difference in windows system, i test on XP, and you Use 8.

BTW, I have no idea how your code works, in the Call :Row "%%A"
Row supposed to take 6 inputs but it take here only one and still output the right html format.
I was re-arrange the code so if i wanted to change a file name or destination i do that from one place instead changing in the whole code and i have no idea why it doesn't work with me and still work with your code.
:? :?:
Ok, I found the problem, i was adding double quotes arround %%A and that was missing every thing, but still don't understand how it take the whole 6 parameters as one
Last edited by abc0502 on 13 Feb 2013 19:03, edited 1 time in total.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Reading Columns & Output

#40 Post by booga73 » 13 Feb 2013 19:03

Well, don't need the solution to work on XP; I'm sure probably other folks use it but, the code is intended for either win7 or win8. r/ Booga73

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#41 Post by abc0502 » 13 Feb 2013 19:06

@booga73, it work with XP fine, just the time needs to be modified to "!field:~12,8!" instead of "!field:~13,8!" that's all :)

BTW, I have no idea how your code works, in the Call :Row "%%A"
Row supposed to take 6 inputs but it take here only one and still output the right html format.
I was re-arrange the code so if i wanted to change a file name or destination i do that from one place instead changing in the whole code and i have no idea why it doesn't work with me and still work with your code.
:? :?:
Ok, I found the problem, i was adding double quotes arround %%A and that was missing every thing, but still don't understand how it take the whole 6 parameters as one

Ok i get it now :oops: :oops:
The %%A variable hold the data in the form of 6 inputs like this "Param1" "Param2" "param3" "Param4" "Param5" "Param6"
so it reads it as if you inout 6 parameters instead one "as it look" :oops: :oops:

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#42 Post by abc0502 » 13 Feb 2013 19:10

This is the same code Foxidrive modified but with enhanced HTML look.
I only arranged code and added variables at the start so you can change any settings like the scanned directory or location or file names or max lines to read easily from the setting section at the start of the batch.

This is a sample of the output : Link
This is XP version, See line 33

Code: Select all

@ECHO OFF

REM =====[ Settings ]=================================
Rem Note that Main Variable Ends with "\"
SET "Main=%~dp0"
SET "Directory=C:\Windows"
SET "HTMLfile=%Main%Report.html"
SET "LOGfile=%Main%Report.csv"
SET "CollectedData=%Main%Data.txt"
SET "MaxLines=116"

REM =====[ Delete Previous HTML File ]================
DEL /F /Q "%HTMLfile%" 2>NUL
DEL /F /Q "%LOGfile%" 2>NUL
DEL /F /Q "%CollectedData%" 2>NUL

REM =====[ Scan Directory ]===========================
DIR "%Directory%" /T:CAW /X /Q /A /O:-G-DS |FIND "/" > "%CollectedData%"

SET LineNumber=0
FOR /F %%A IN ('FIND /C /V "" ^< "%CollectedData%"') DO SET /A LineNumber=%%A
ECHO Collected Data File Has : %LineNumber% Line^(s^)

IF "%MaxLines%" == "" SET "MaxLines=%LineNumber%"

REM =====[ Process Collected Data File ]==============
SETLOCAL EnableDelayedExpansion
SET LineCount=0
FOR /F "delims=" %%A IN ('TYPE "%CollectedData%"') DO (
   IF "!LineCount!" == "%MaxLines%" GOTO :ExitLoop
   SET "Field=%%A"
   SET "Date=!Field:~0,10!"
   REM IF using windows 8 set the Time to "!Field:~13,7!" The Below is for XP
   SET "Time=!Field:~12,8!"
   SET "SorT=!Field:~21,17!"
   SET "Short=!Field:~39,12!"
   SET "Owner=!Field:~52,23!"
   SET "Name=!Field:~75!"
   CALL :Next "!Date!" "!Time!" "!SorT!" "!Short!" "!Owner!" "!Name!"
   SET /A LineCount += 1
   )
:ExitLoop

REM =====[ Create HTML ]==============================
Rem Create First Part
CALL :HTML_1st_Part

Rem Add Table
FOR /F "delims=" %%A IN ('Type "%LOGfile%"') DO (
   Call :Table_Content %%A
   )

Rem Create Second Part
CALL :HTML_2nd_Part

REM =====[ Clean and Exit ]===========================
DEL /F /Q "%CollectedData%" 2>NUL
Ping LocalHost -n 3 >NUL
START "Report" "%HTMLfile%"
Exit /B

REM =====[ Functions ]================================
Rem HTML
:HTML_1st_Part
(
Echo ^<HTML^>
Echo    ^<head^>
Echo    ^<style media="screen" type="text/css"^>
Echo       html {
Echo             background-color: Black;
Echo             color: Orange;
Echo             }
Echo.
Echo       table {
Echo             border: 3px solid red;
Echo             padding: 2px;
Echo             text-align: center;
Echo             }
Echo.         
Echo       td {
Echo             width: 200px;
Echo             height: 20px;
Echo             border: 1px solid gray;
Echo             }
Echo.         
Echo       th {
Echo             badding-botton: 5px;
Echo             color: Red;
Echo             }
Echo.
Echo       h2,h3,h5 {
Echo             text-align: Center;
Echo             }
Echo.
Echo       h5 {
Echo             color: White;
Echo             }
Echo    ^</style^>
Echo    ^</head^>
Echo    ^<body^>
Echo       ^<h2^>Report^</h2^>
Echo       ^<h3^>%Date% - %time%^</h3^>
Echo       ^<table^>
Echo          ^<thead^>
Echo             ^<tr^>
Echo                ^<th^>Date^</th^>
Echo                ^<th^>Time^</th^>
Echo                ^<th^>Size^</th^>
Echo                ^<th^>Short Name^</th^>
Echo                ^<th^>File Owner^</th^>
Echo                ^<th^>File Name^</th^>
Echo             ^</tr^>
Echo          ^</thead^>
Echo          ^<tbody^>       
)>>"%HTMLfile%"
GOTO :EOF

:Table_Content
>>"%HTMLfile%" Echo          ^<tr^>
>>"%HTMLfile%" <nul set /p "=             <td>%~1</td>"
>>"%HTMLfile%" <nul set /p "=             <td>%~2</td>"

REM === handling of "< and >" to display in HTML

if "%~3"=="<DIR>" (
>>"%HTMLfile%" <nul set /p "=             <td>&lt;DIR&gt;</td>"
) else (
>>"%HTMLfile%" <nul set /p "=             <td>%~3</td>"
)

REM === field 4 can be empty so special handling is needed

if "%~4"=="" (
>>"%HTMLfile%" <nul set /p "=             <td>&nbsp;</td>"
) else (
>>"%HTMLfile%" <nul set /p "=             <td>%~4</td>"
)

>>"%HTMLfile%" <nul set /p "=             <td>%~5</td>"
>>"%HTMLfile%" <nul set /p "=             <td>%~6</td>"
>>"%HTMLfile%" Echo.
>>"%HTMLfile%" Echo          ^</tr^>

GOTO :EOF

:HTML_2nd_Part
(
Echo          ^</tbody^>
Echo       ^</table^>
Echo       ^<h5^>DosTips. com^</h5^>
Echo    ^</body^>
Echo ^</html^>
)>>"%HTMLfile%"
GOTO :EOF

REM Make Sure This label Always The Last One In The Batch
:Next
SET "line="
SET "c=0"
:Next2
SET /A C += 1
SET Tokens="tokens=*"
if %C% EQU 3 SET Tokens=
FOR /F %Tokens% %%a IN ("%~1") DO CALL SET line=%%line%%,"%%a"
SHIFT
IF NOT "%~1"=="" GoTO :Next2
>>"%LOGfile%" Echo %line:~1%


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#43 Post by foxidrive » 13 Feb 2013 21:45

It looks great abc0502 - and it handles the parentheses just fine.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Reading Columns & Output

#44 Post by abc0502 » 13 Feb 2013 23:09

:? Actually i didn't do any thing , it's your code i thought you fixed the parentheses problem :lol:

BTW, nice trick using the %%A in the call :Row in your code, it took me long time till i figured it out :lol:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Reading Columns & Output

#45 Post by foxidrive » 13 Feb 2013 23:53

hehe My version never had an issue with parentheses - I was just confirming that.

It was a lot of fun playing with this in collaboration. :)


The CALL :ROW thing was tricky as it had commas, and that could have taken a while to realise that they are treated as whitespace in a call statement.

Post Reply