CSV to HTML: to be improved

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

CSV to HTML: to be improved

#1 Post by budhax » 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.



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>

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

#2 Post by budhax » 04 Aug 2008 16:28

I found a solution.
Tested and works fine.

** TO CUSTOMIZE
- Set the column separator character: SET SEP=,

** USAGE
- Drag & drop a .CSV file on this script. It will build a HTML table.

** PROBLEMS
- Column's separator character in data are replaced by space " ". See cell A1 and A2.

Any comment or improvement will be appreciate.
Thanks


Code: Select all

::------------------------------------------------------ Conversion .CSV -> .HTM
::USAGE: Drag & Drop a .cvs file on this script

@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION

::************************************************* Customize Column's SEPARATOR
SET SEP=,



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"^>
   ::--------------------------------------------------------- FIRST LINE in <TH>
   ECHO. ^<THEAD^>
   ECHO.  ^<TR^>
   Call:GetL1 "%~DPnx1" "L1"
   SET LC=!L1:%SEP%= !
   FOR %%e in (!LC!) DO (ECHO.   ^<TH^>%%~e^</TH^>)
   ECHO.  ^</TR^>
   ECHO. ^</THEAD^>
   ::-------------------------------------------------------- OTHER LINES in <TD>
   ECHO. ^<TBODY^>
   FOR /f "SKIP=1 Delims=" %%f in ('TYPE "%~nx1"') DO (
      SET L=%%f
      SET LS=!L:%SEP%= !
      ECHO.  ^<TR^>
      FOR %%c in (!LS!) DO (ECHO.   ^<TD^>%%~c^</TD^>)
      ECHO.  ^</TR^>
   )
   ECHO. ^</TBODY^>
   ECHO.^</TABLE^>
   ECHO.
   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.Press To END ......&Pause>Nul&EXIT /b::___________________________


::*********************************************************** Get The First Line
:GetL1
SET P1=%~1
SET P2=%~2
FOR /f "Delims=" %%f in ('TYPE "%P1%"') DO (SET !P2!=%%f&GOTO:EOF)
::******************************************************************************




file.csv

Code: Select all

"SS,ID","MAC","RSSI","Mode","Encryption","Speed","PHY","Beacon","First Seen","Last Seen","Pairwise Ciphers","Group Ciphers","Key Management","SSID","MAC","RSSI","Mode","Encryption","Speed","PHY","Beacon","First Seen","Last Seen","Pairwise Ciphers","Group Ciphers","Key Management","Last Seen","Pairwise Ciphers","Group Ciphers","Key Management"
"d,link","00:19:5b:d8:7d:cc",-58,"BSS","WPA",54,"OFDM",100,1217778545,1217784946,"TKIP-CCMP","TKIP","PSK","dlink","00:19:5b:d8:7d:cc",-58,"BSS","WPA",54,"OFDM",100,1217778545,1217784946,"TKIP-CCMP","TKIP","PSK",1217784946,"TKIP-CCMP","TKIP","PSK"
"ZyXEL","00:13:49:a8:68:1d",-84,"BSS","WEP",54,"OFDM",100,1217778545,1217784946,"WEP","WEP","Not known","ZyXEL","00:13:49:a8:68:1d",-84,"BSS","WEP",54,"OFDM",100,1217778545,1217784946,"WEP","WEP","Not known",1217784946,"WEP","WEP","Not known"
"anMOTOS","00:18:4d:0c:ae:96",-84,"BSS","WEP",54,"OFDM",100,1217778566,1217784946,"WEP","WEP","Not known","anMOTOS","00:18:4d:0c:ae:96",-84,"BSS","WEP",54,"OFDM",100,1217778566,1217784946,"WEP","WEP","Not known",1217784946,"WEP","WEP","Not known"
"25cbfptuy8","00:0f:cc:39:7e:40",-84,"BSS","WEP",54,"OFDM",100,1217778606,1217784942,"WEP","WEP","Not known","25cbfptuy8","00:0f:cc:39:7e:40",-84,"BSS","WEP",54,"OFDM",100,1217778606,1217784942,"WEP","WEP","Not known",1217784942,"WEP","WEP","Not known"
"Macnet","00:18:4d:f6:a8:a8",-86,"BSS","WPA2",54,"OFDM",100,1217784807,1217784807,"CCMP","CCMP","PSK","Macnet","00:18:4d:f6:a8:a8",-86,"BSS","WPA2",54,"OFDM",100,1217784807,1217784807,"CCMP","CCMP","PSK",1217784807,"CCMP","CCMP","PSK"


Result: file.htm

Code: Select all

<HTML><HEAD>
<TITLE>file</TITLE>
</HEAD>
<BODY>

<TABLE border="1">
 <THEAD>
  <TR>
   <TH>SS ID</TH>
   <TH>MAC</TH>
   <TH>RSSI</TH>
   <TH>Mode</TH>
   <TH>Encryption</TH>
   <TH>Speed</TH>
   <TH>PHY</TH>
   <TH>Beacon</TH>
   <TH>First Seen</TH>
   <TH>Last Seen</TH>
   <TH>Pairwise Ciphers</TH>
   <TH>Group Ciphers</TH>
   <TH>Key Management</TH>
   <TH>SSID</TH>
   <TH>MAC</TH>
   <TH>RSSI</TH>
   <TH>Mode</TH>
   <TH>Encryption</TH>
   <TH>Speed</TH>
   <TH>PHY</TH>
   <TH>Beacon</TH>
   <TH>First Seen</TH>
   <TH>Last Seen</TH>
   <TH>Pairwise Ciphers</TH>
   <TH>Group Ciphers</TH>
   <TH>Key Management</TH>
   <TH>Last Seen</TH>
   <TH>Pairwise Ciphers</TH>
   <TH>Group Ciphers</TH>
   <TH>Key Management</TH>
  </TR>
 </THEAD>
 <TBODY>
  <TR>
   <TD>d link</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>
   <TD>1217784946</TD>
   <TD>TKIP-CCMP</TD>
   <TD>TKIP</TD>
   <TD>PSK</TD>
   <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>
   <TD>1217784946</TD>
   <TD>TKIP-CCMP</TD>
   <TD>TKIP</TD>
   <TD>PSK</TD>
   <TD>1217784946</TD>
   <TD>TKIP-CCMP</TD>
   <TD>TKIP</TD>
   <TD>PSK</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>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <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>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</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>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <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>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <TD>1217784946</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</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>
   <TD>1217784942</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <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>
   <TD>1217784942</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</TD>
   <TD>1217784942</TD>
   <TD>WEP</TD>
   <TD>WEP</TD>
   <TD>Not known</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>
   <TD>1217784807</TD>
   <TD>CCMP</TD>
   <TD>CCMP</TD>
   <TD>PSK</TD>
   <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>
   <TD>1217784807</TD>
   <TD>CCMP</TD>
   <TD>CCMP</TD>
   <TD>PSK</TD>
   <TD>1217784807</TD>
   <TD>CCMP</TD>
   <TD>CCMP</TD>
   <TD>PSK</TD>
  </TR>
 </TBODY>
</TABLE>

 </BODY>
</HTML>

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

#3 Post by budhax » 05 Aug 2008 15:36

Here is my final version.
WARNING: Values in the CSV shouldn't contain double quote (").

Code: Select all

::------------------------------------------------- Conversion D'un .CSV en .HTM
::*** UTILISATION: DRAG & DROP a .CSV
::Version 1.1

@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION

::************************************************* Customize Column's SEPARATOR
SET SEP=,



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^>
   ECHO.^<HEAD^>^<TITLE^>%~n1^</TITLE^>^</HEAD^>
   ECHO.^<BODY^>
   ECHO.
   ECHO.^<TABLE border="1"^>
   ::--------------------------------------------------------- FIRST LINE in <TH>
   ECHO. ^<THEAD^>
   ECHO.  ^<TR^>
   Call:GetL1 "%~DPnx1" "L1"
   SET L1=!L1:"=!
   FOR %%e in ("!L1:%SEP%=" "!") DO (ECHO.   ^<TH^>%%~e^</TH^>)
   ECHO.  ^</TR^>
   ECHO. ^</THEAD^>
   ::-------------------------------------------------------- OTHER LINES in <TD>
   ECHO. ^<TBODY^>
   FOR /f "SKIP=1 Delims=" %%f in ('TYPE "%~nx1"') DO (
      SET Li=%%f
      SET Li=!Li:"=!
      ECHO.  ^<TR onClick="ColTR(this)"^>
      FOR %%c in ("!Li:%SEP%=" "!") DO (ECHO.   ^<TD^>%%~c^</TD^>)
      ECHO.  ^</TR^>
   )
   ECHO. ^</TBODY^>
   ECHO.^</TABLE^>
   ECHO.
   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.END. BYE ......&PING -n 3 LocalHost>Nul&EXIT /b::_________________



::******************************************************** Renvoie la 1ère LiGNE
:GetL1
SET P1=%~1
SET P2=%~2
FOR /f "Delims=" %%f in ('TYPE "%P1%"') DO (SET !P2!=%%f&GOTO:EOF)
::******************************************************************************

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

#4 Post by budhax » 15 Aug 2008 17:43

Release 1.2
*** ADDED: function trying to find the column' separator.
*** CSS is set as: (tab)(space),(space);


Code: Select all

::------------------------------------------------------------------ CSV --> HTM
::------------------------------------------------------------------ Version 1.2
::*** UTILISATION    DRAG & DROP a .CSV
::*** LIMITATIONS    Value's Data Shouldn't Contain: Tab " , ;

@ECHO OFF
@SETLOCAL ENABLEDELAYEDEXPANSION
CD /D "%~DP1"
IF "%~1"==""             (SET ErrU=1&GOTO:FinE)
IF /i NOT "%~x1"==".csv" (SET ErrX=1&GOTO:FinE)


::****************************************** CCS: Column's Candidates SEPARATORS
SET L1=
SET CCS=    , ;
Call:GetL1 "%~DPnx1" "L1"
::------------------- Find The Column's SEPARATOR
FOR %%s in ("%CCS: =" "%") DO (
   SET SEP=%%~s
   SET /a Col=0
   FOR %%e in ("!L1:%%~s=" "!") DO (SET /a Col+=1)
   IF 1 LSS !Col! (GOTO:BREAK)
)
:BREAK
::ECHO.Found SEPARATOR: [!SEP!]  Columns: [!Col!]


:CHOI
COLOR 07&ECHO.&ECHO.------------------------------------------------------------
ECHO.Conversion: .CSV -^> .HTM
ECHO.Patientez ...
ECHO.

SET z=%~n1.hTM
TYPE NUL>"%z%"
(
   ECHO.^<HTML^>
   ECHO.^<HEAD^>^<TITLE^>%~n1^</TITLE^>^</HEAD^>
   ECHO.^<BODY^>
   ECHO.
   ECHO.^<TABLE class="sortable" border="1"^>
   ::--------------------------------------------------------- FIRST LINE in <TH>
   ECHO. ^<THEAD^>
   ECHO.  ^<TR^>
   Call:GetL1 "%~DPnx1" "L1"
   SET L1=!L1:"=!
   FOR %%e in ("!L1:%SEP%=" "!") DO (ECHO.   ^<TH^>%%~e^</TH^>)
   ECHO.  ^</TR^>
   ECHO. ^</THEAD^>
   ::-------------------------------------------------------- OTHER LINES in <TD>
   ECHO. ^<TBODY^>
   FOR /f "SKIP=1 Delims=" %%f in ('TYPE "%~nx1"') DO (
      SET Li=%%f
      SET Li=!Li:"=!
      ECHO.  ^<TR onClick="ColTR(this)"^>
      FOR %%c in ("!Li:%SEP%=" "!") DO (ECHO.   ^<TD^>%%~c^</TD^>)
      ECHO.  ^</TR^>
   )
   ECHO. ^</TBODY^>
   ECHO.^</TABLE^>
   ECHO.
   IF EXIST "%~DP0S-TAB.hTM" (TYPE "%~DP0S-TAB.hTM"&ECHO.)
   ECHO.^</BODY^>
   ECHO.^</HTML^>
)>> "%z%"
GOTO:FinN

:FinE
ECHO.&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.END. BYE ......&PING -n 3 LocalHost>Nul&EXIT /b::_________________


::******************************************************** Renvoie la 1ère LiGNE
:GetL1
SET P1=%~1
SET P2=%~2
FOR /f "Delims=" %%f in ('TYPE "%P1%"') DO (SET !P2!=%%f&GOTO:EOF)
::******************************************************************************

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 28 Aug 2008 21:38

budhax,

In an attempt to avoid all the escaping business I found the for command pretty useful:

Code: Select all

@echo off
set "csvFile=%~1"

for %%Z in ("<HTML>")                     do echo.%%~Z
for %%Z in (" <HEAD>")                    do echo.%%~Z
for %%Z in (" <TITLE>%~nx0</TITLE>")      do echo.%%~Z
for %%Z in (" </HEAD>")                   do echo.%%~Z
for %%Z in (" <BODY>")                    do echo.%%~Z
for %%Z in ("  <TABLE border='1'>")       do echo.%%~Z
for %%Z in ("   <TBODY>")                 do echo.%%~Z
for /f "usebackq tokens=* delims=" %%A in ("%csvFile%") do (
    for %%Z in ("    <TR>")               do echo.%%~Z
    for %%L in (%%A) do (
        for %%Z in ("     <TD>%%~L</TD>") do echo.%%~Z
    )
    for %%Z in ("    </TR>")              do echo.%%~Z
)                                       
for %%Z in ("   </TBODY>")                do echo.%%~Z
for %%Z in ("  </TABLE")                  do echo.%%~Z
for %%Z in (" </BODY>")                   do echo.%%~Z
for %%Z in ("</HTML>")                    do echo.%%~Z

What do you think?

Post Reply