CSV to HTML: to be improved
Posted: 03 Aug 2008 14:52
Hello,
This script convert a .CSV file to a HTML table. Could you HELP me to improve it?
I would:
1. Put the first row in a TH tag, then the other line in TR tag.
2. Improve it to work fine for any number of column. This one works fine for 9 columns, but my CVS file has 13 columns.
3. Make the script independent of separator. It should work for "," or ";" or the tab character.
Thanks in advance.
file.csv
"SSID","MAC","RSSI","Mode","Encryption","Speed","PHY","Beacon","First Seen","Last Seen","Pairwise Ciphers","Group Ciphers","Key Management"
"dlink","00:19:5b:d8:7d:cc",-58,"BSS","WPA",54,"OFDM",100,1217778545,1217784946,"TKIP-CCMP","TKIP","PSK"
"ZyXEL","00:13:49:a8:68:1d",-84,"BSS","WEP",54,"OFDM",100,1217778545,1217784946,"WEP","WEP","Not known"
"anMOTOS","00:18:4d:0c:ae:96",-84,"BSS","WEP",54,"OFDM",100,1217778566,1217784946,"WEP","WEP","Not known"
"25cbfptuy8","00:0f:cc:39:7e:40",-84,"BSS","WEP",54,"OFDM",100,1217778606,1217784942,"WEP","WEP","Not known"
"Macnet","00:18:4d:f6:a8:a8",-86,"BSS","WPA2",54,"OFDM",100,1217784807,1217784807,"CCMP","CCMP","PSK"
file.html
This script convert a .CSV file to a HTML table. Could you HELP me to improve it?
I would:
1. Put the first row in a TH tag, then the other line in TR tag.
2. Improve it to work fine for any number of column. This one works fine for 9 columns, but my CVS file has 13 columns.
3. Make the script independent of separator. It should work for "," or ";" or the tab character.
Thanks in advance.
Code: Select all
::------------------------------------------------- Conversion D'un .CSV en .HTM
@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION
CD /D "%~DP1"
IF "%~1"=="" (SET ErrU=1&GOTO:FinE)
IF /i NOT "%~x1"==".csv" (SET ErrX=1&GOTO:FinE)
:CHOI
COLOR 07&ECHO.&ECHO.------------------------------------------------------------
ECHO.Conversion: .CSV -^> .HTM
ECHO.Patientez ...
ECHO.
SET z=%~n1.hTM
TYPE NUL>"%z%"
(
ECHO.^<HTML^>^<HEAD^>
ECHO.^<TITLE^>%~n1^</TITLE^>
ECHO.^</HEAD^>
ECHO.^<BODY^>
ECHO.
ECHO.^<TABLE border="1"^>
FOR /f "Tokens=* Delims=" %%f in ('TYPE "%~nx1"') DO (
ECHO. ^<TR^>
FOR /f "Tokens=1-9 Delims=," %%r in ("%%f") DO (
ECHO. ^<TD^>%%~r^</TD^>
ECHO. ^<TD^>%%~s^</TD^>
ECHO. ^<TD^>%%~t^</TD^>
ECHO. ^<TD^>%%~u^</TD^>
ECHO. ^<TD^>%%~v^</TD^>
ECHO. ^<TD^>%%~w^</TD^>
ECHO. ^<TD^>%%~x^</TD^>
ECHO. ^<TD^>%%~y^</TD^>
ECHO. ^<TD^>%%~z^</TD^>
)
ECHO. ^</TR^>
)
ECHO.^</TABLE^>
ECHO. ^</BODY^>
ECHO.^</HTML^>
)>> "%z%"
GOTO:FinN
:FinE
COLOR 0C
IF DEFINED ErrF (ECHO.*** ERREUR: Fichier Absent!)
IF DEFINED ErrD (ECHO.*** ERREUR: Dossier Absent!)
IF DEFINED ErrX (ECHO.*** ERREUR: De L'Extention!)
IF DEFINED ErrU (ECHO.*** ERREUR: Glisser un csv!)
:FinN
ENDLOCAL&ECHO.&ECHO.END. BYE ......&PING -n 2 LocalHost>Nul&EXIT /b::___________
file.csv
"SSID","MAC","RSSI","Mode","Encryption","Speed","PHY","Beacon","First Seen","Last Seen","Pairwise Ciphers","Group Ciphers","Key Management"
"dlink","00:19:5b:d8:7d:cc",-58,"BSS","WPA",54,"OFDM",100,1217778545,1217784946,"TKIP-CCMP","TKIP","PSK"
"ZyXEL","00:13:49:a8:68:1d",-84,"BSS","WEP",54,"OFDM",100,1217778545,1217784946,"WEP","WEP","Not known"
"anMOTOS","00:18:4d:0c:ae:96",-84,"BSS","WEP",54,"OFDM",100,1217778566,1217784946,"WEP","WEP","Not known"
"25cbfptuy8","00:0f:cc:39:7e:40",-84,"BSS","WEP",54,"OFDM",100,1217778606,1217784942,"WEP","WEP","Not known"
"Macnet","00:18:4d:f6:a8:a8",-86,"BSS","WPA2",54,"OFDM",100,1217784807,1217784807,"CCMP","CCMP","PSK"
file.html
Code: Select all
<HTML><HEAD>
<TITLE>file</TITLE>
</HEAD>
<BODY>
<TABLE border="1">
<TR>
<TD>SSID</TD>
<TD>MAC</TD>
<TD>RSSI</TD>
<TD>Mode</TD>
<TD>Encryption</TD>
<TD>Speed</TD>
<TD>PHY</TD>
<TD>Beacon</TD>
<TD>First Seen</TD>
</TR>
<TR>
<TD>dlink</TD>
<TD>00:19:5b:d8:7d:cc</TD>
<TD>-58</TD>
<TD>BSS</TD>
<TD>WPA</TD>
<TD>54</TD>
<TD>OFDM</TD>
<TD>100</TD>
<TD>1217778545</TD>
</TR>
<TR>
<TD>ZyXEL</TD>
<TD>00:13:49:a8:68:1d</TD>
<TD>-84</TD>
<TD>BSS</TD>
<TD>WEP</TD>
<TD>54</TD>
<TD>OFDM</TD>
<TD>100</TD>
<TD>1217778545</TD>
</TR>
<TR>
<TD>anMOTOS</TD>
<TD>00:18:4d:0c:ae:96</TD>
<TD>-84</TD>
<TD>BSS</TD>
<TD>WEP</TD>
<TD>54</TD>
<TD>OFDM</TD>
<TD>100</TD>
<TD>1217778566</TD>
</TR>
<TR>
<TD>25cbfptuy8</TD>
<TD>00:0f:cc:39:7e:40</TD>
<TD>-84</TD>
<TD>BSS</TD>
<TD>WEP</TD>
<TD>54</TD>
<TD>OFDM</TD>
<TD>100</TD>
<TD>1217778606</TD>
</TR>
<TR>
<TD>Macnet</TD>
<TD>00:18:4d:f6:a8:a8</TD>
<TD>-86</TD>
<TD>BSS</TD>
<TD>WPA2</TD>
<TD>54</TD>
<TD>OFDM</TD>
<TD>100</TD>
<TD>1217784807</TD>
</TR>
</TABLE>
</BODY>
</HTML>