最后一行加粗-使用批处理文件

时间:2019-02-22 08:50:35

标签: batch-file

我正在运行一个批处理脚本,以将CSV转换为HTML文件,但是我需要使最后一行加粗,但是我尝试了其他方法使最后一行加粗,但是我无法将脚本转换为确定它是否是最后一行。

有什么想法吗?

@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=FinalReportFormatted.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%"

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:#fff;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:center;}
 echo .tftable tr {background-color:#ffffff;}
 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 ^<p^>
 echo ^<Dear All,^>
 echo ^</p^>
 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
::******************************************************************************************************

1 个答案:

答案 0 :(得分:1)

我同意Compo的观点,即使用PowerShell是更具前瞻性的方法,
但由于该批处理似乎效果很好,请实施monotero的建议:

  • 使用find /V /C "" <"%~1"来计数.csv文件的行
  • 解析该数字并将其存储在变量Lines
  • 使用递减计数行修改子:CreateHTMLTable的中间部分
  • 如果(剩余)行数大于0,则像以前一样进行
  • 如果未插入<B></B>标签。

for /f "delims=" %%A in ('find.exe /V /C "" ^<"%~1"') Do Set Lines=%%A
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 /A Lines-=1
 if !Lines! gtr 0 (
   set "var=!var:%delims%=</td><td>!"
   echo ^<tr^>^<td^>!var!^</td^>^</tr^>
 ) else (
   set "var=!var:%delims%=</B></td><td><B>!"
   echo ^<tr^>^<td^>^<B^>!var!^</B^>^</td^>^</tr^>
 )
)>>%2

enter image description here