Computer Hope

Microsoft => Microsoft DOS => Topic started by: mmonut on April 03, 2017, 04:51:30 AM

Title: need to create a batch file to Convert .csv to html
Post by: mmonut on April 03, 2017, 04:51:30 AM
Hi team,

I am try to create a batch file which convert .csv file to html
but the html file is not showing in proper format .

please help to create this script .


also Can be browser a file in batch script .

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "tokens=1,* delims=: " %%a in (csv2.csv) do (
set "var=!var!,%%a"
set "var2=!var2!,%%b"
)
>file.html echo %var:~1%
>>file.html echo %var2:~1%
pause

thank you
Title: Re: need to create a batch file to Convert .csv to html
Post by: DaveLembke on April 15, 2017, 12:36:43 PM
If you want to see it within a browser window you could flip the .csv into a .txt and view it that way. If your going to convert this to a html I would start with prepending HTML tags to the document, then append the .csv contents to that html file and then append the proper HTML closing tags. The .csv content can all be within the
Code: [Select]
<BODY>  </BODY> tags.
Title: Re: need to create a batch file to Convert .csv to html
Post by: Hackoo on April 15, 2017, 04:46:32 PM
Hi  ;)
you should provide a csv file to test it, but anyway,
you can try something like that as example with delimiters ","
CSV2HTML.bat
Code: [Select]
@echo off
Title Convert csv file to HTML file
if exist data.html del /f /q data.html
call :CreateHTMLtable data.csv data.html
start "" data.html
exit /b
::******************************************************************************************************
:CreateHTMLTable <inputfile> <outputfile>
setlocal
>%2 (
echo ^<!DOCTYPE HTML PUBLIC
echo "-//W3C//DTD HTML 4.01 Transitional//EN"
echo  "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
echo ^<HTML^>
echo ^<HEAD^>
echo ^<META HTTP-EQUIV="Content-Type"
echo CONTENT="text/html; charset=utf-8"^>
echo ^</HEAD^>
echo ^<BODY^>
echo ^<style type="text/css"^>
echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
echo .tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:left;}
echo .tftable tr {background-color:#e9dbbb;}
echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;}
echo .tftable tr:hover {background-color:#ffffff;}
echo ^</style^>
echo ^<table class="tftable" border="1"^>
)

for /f "tokens=1,2 delims=," %%a in (%1) do (
>>%2 echo ^<tr^>^<td^>%%a^</td^>^<td^>%%b^</td^>^</tr^>
)

>>%2 (
echo ^</table^>
echo ^</BODY^>
echo ^</HTML^>
)
::******************************************************************************************************

Here is another way to set what is your delimiters in your csv file :
Code: [Select]
@echo off
Title Convert csv file to HTML file
Rem you set what is your delimters in your csv file
set "delims=,"
if exist data.html del /f /q data.html
call :CreateHTMLtable data.csv data.html
start "" data.html
exit /b
::******************************************************************************************************
:CreateHTMLTable <inputfile> <outputfile>
setlocal
(
echo ^<!DOCTYPE HTML PUBLIC
echo "-//W3C//DTD HTML 4.01 Transitional//EN"
echo  "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
echo ^<HTML^>
echo ^<HEAD^>
echo ^<META HTTP-EQUIV="Content-Type"
echo CONTENT="text/html; charset=utf-8"^>
echo ^</HEAD^>
echo ^<BODY^>
echo ^<style type="text/css"^>
echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
echo .tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:left;}
echo .tftable tr {background-color:#e9dbbb;}
echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;}
echo .tftable tr:hover {background-color:#ffffff;}
echo ^</style^>
echo ^<table class="tftable" border="1"^>
)>%2

for /f "tokens=1,2 delims=%delims%" %%a in (%1) do (
echo ^<tr^>^<td^>%%a^</td^>^<td^>%%b^</td^>^</tr^>
)>>%2

(
echo ^</table^>
echo ^</BODY^>
echo ^</HTML^>
)>>%2
::******************************************************************************************************
Title: Re: need to create a batch file to Convert .csv to html
Post by: Hackoo on April 15, 2017, 09:33:04 PM
This an update version  ;)
CSV2HTML.bat
Code: [Select]
@echo off
Title Convert csv file to HTML file
Rem Set what is your delimiters in your csv file like [,] [;] [:] [|] or [tab]
set "delims=:"
Rem Set the name of the CSV file into a variable
set "CSV_File=data.csv"
Rem Set the name of the HTML output file from CSV file into variable
for %%a in ("%CSV_File%") do set "HTML_File=%%~na.html"
if exist "%HTML_File%" del /f /q "%HTML_File%"
Call :CreateHTMLtable "%CSV_File%" "%HTML_File%"
start "" "%HTML_File%"
exit /b
::******************************************************************************************************
:CreateHTMLTable <inputfile> <outputfile>
setlocal
(
echo ^<!DOCTYPE HTML PUBLIC
echo "-//W3C//DTD HTML 4.01 Transitional//EN"
echo  "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
echo ^<HTML^>
echo ^<HEAD^>
echo ^<META HTTP-EQUIV="Content-Type"
echo CONTENT="text/html; charset=utf-8"^>
echo ^</HEAD^>
echo ^<BODY^>
echo ^<style type="text/css"^>
echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
echo .tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:center;}
echo .tftable tr {background-color:#e9dbbb;}
echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91; text-align:center;}
echo .tftable tr:hover {background-color:#ffffff;}
echo ^</style^>
echo ^<center^>^<table class="tftable" border="1"^>
)>%2

setlocal enabledelayedexpansion
for /F "delims=" %%A in ('Type "%~1"') do (
set "var=%%A"
set "var=!var:&=&amp;!"
set "var=!var:<=&lt;!"
set "var=!var:>=&gt;!"
set "var=!var:%delims%=</td><td>!"
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
)>>%2

(
echo ^</table^>^</center^>
echo ^</BODY^>
echo ^</HTML^>
)>>%2
endlocal
::******************************************************************************************************
Title: Re: need to create a batch file to Convert .csv to html
Post by: Hackoo on April 18, 2017, 09:54:28 PM
Hi  ;)
This is a new version that can be used with a drag and drop a csv file over the batch file to be converted in HTML Table.
CSV2HTML.bat
Code: [Select]
@echo off
Mode con cols=70 lines=3 & color 0E
Title Convert csv file to HTML file
Rem Set what is your delimiters in your csv file like [,] [;] [|]
set delims="," ";" "|"
set "CSV_File=%~1"
IF ["%CSV_File%"] EQU [""] Goto:Error
Rem Set the name of the HTML output file from CSV file into variable
for /f "usebackq delims=" %%a in (`echo "%CSV_File%"`) do ( set "HTML_File=%%~na.html")
if exist "%HTML_File%" del /f /q "%HTML_File%"
echo(
echo         Converting to "%HTML_File%" is in progress ...
Timeout /T 2 /nobreak>nul
Call :CreateHTMLtable "%CSV_File%" "%HTML_File%"
start "" "%HTML_File%"
exit /b
::******************************************************************************************************
:CreateHTMLTable <inputfile> <outputfile>
setlocal
(
echo ^<!DOCTYPE HTML PUBLIC
echo "-//W3C//DTD HTML 4.01 Transitional//EN"
echo  "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
echo ^<HTML^>
echo ^<HEAD^>
echo ^<META HTTP-EQUIV="Content-Type"
echo CONTENT="text/html; charset=utf-8"^>
echo ^</HEAD^>
echo ^<BODY^>
echo ^<style type="text/css"^>
echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
echo .tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:center;}
echo .tftable tr {background-color:#e9dbbb;}
echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91; text-align:center;}
echo .tftable tr:hover {background-color:#ffffff;}
echo ^</style^>
echo ^<center^>^<table class="tftable" border="1"^>
)>%2

setlocal enabledelayedexpansion
for /F "delims=" %%A in ('Type "%~1"') do (
set "var=%%A"
set "var=!var:&=&amp;!"
set "var=!var:<=&lt;!"
set "var=!var:>=&gt;!"
for %%a in (%delims%) do (
set "var=!var:%%~a=</td><td>!"
)
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
)>>%2

(
echo ^</table^>^</center^>
echo ^</BODY^>
echo ^</HTML^>
)>>%2
endlocal
exit /b
::******************************************************************************************************
:Error
Color 0C
echo.
ECHO    You must drag and drop a .csv file over this batch program !
Timeout /T 4 /NoBreak >nul
Exit