Board logo

标题: [文本处理] 批处理如何把txt文本转换到htm网页格式? [打印本页]

作者: bbcwang123    时间: 2011-5-31 12:54     标题: 批处理如何把txt文本转换到htm网页格式?

我有很多txt的文本文件,想转成htm的网页文件,在文本的顶部加上
<html>
<head>
<title>txt文件的名字</title>
</head>
<body>
文本内容
</body>
</html>

大概就是这样的,都可以看懂的吧。    不过就在前面和后面加上文本修改格式后也不行,还有把换行符,回车都改成网页的标识符<br>  要求把批处理放到这个文件夹内,双击后可以把所有的文件全部转黄成网页形式,我是新手,有很多代码不懂请大家在代码后面简单的解释下。
先谢谢大家了!!  期待回复!!

作者: tmplinshi    时间: 2011-5-31 13:40

  1. @echo off
  2. for /f "delims=" %%i in ('dir /b *.txt') do (
  3.     findstr /n .* "%%i" >.tmp
  4.     (
  5.         echo ^<html^>
  6.         echo ^<head^>
  7.         echo ^<title^>%%i^</title^>
  8.         echo ^</head^>
  9.         echo ^<body^>
  10.         for /f "delims=" %%i in (.tmp) do (
  11.             set str=%%i
  12.             SetLocal EnableDelayedExpansion
  13.             echo !str:*:=!^<br^>
  14.             EndLocal
  15.         )
  16.         echo ^</body^>
  17.         echo ^</html^>
  18.     ) >"%%i"
  19. )
  20. del .tmp
复制代码

作者: batman    时间: 2011-5-31 16:11

vbs版本:
  1. Dim vbstr, fso, file
  2. Set fso = CreateObject("scripting.filesystemobject")
  3. Set ws = CreateObject("wscript.shell")
  4. For Each file In fso.GetFolder(ws.CurrentDirectory).Files
  5.   If LCase(fso.GetExtensionName(file)) = "txt" Then
  6.     vbstr = fso.OpenTextFile(file, 1).ReadAll()
  7.     vbstr = Replace(vbstr, vbCrLf, "<br>")
  8.     vbstr = "<html>" & vbCrLf & "<head>" & vbCrLf & "<title>" & file.Name & "</title>" & vbCrLf & "</head>" & vbCrLf & "<body>" & vbCrLf & vbstr & vbCrLf & "</body>" & vbCrLf & "</html>"
  9.     fso.OpenTextFile(file, 2, 1).Write vbstr
  10.     fso.MoveFile file, Replace(file.Name, ".txt", ".html")
  11.   End If
  12. Next
  13. Set ws = Nothing
  14. Set fso = Nothing
复制代码

作者: bbcwang123    时间: 2011-5-31 21:48

谢谢你们!!
作者: bbcwang123    时间: 2011-5-31 22:07

2# tmplinshi


我测试了一下,发现不怎么好用啊!
可能是我没注意一些字的编码问题,有些都变成乱码了, 貌似是少了一个 <meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
还有扩展名也没有批量改成.htm
作者: tmplinshi    时间: 2011-5-31 22:46

需要下载 concmd:

concmd - 编码转换工具
http://bbs.bathome.net/viewthread.php?tid=12595
  1. @echo off
  2. for /f "delims=" %%i in ('dir /b *.txt') do (
  3.     concmd /o:gbk "%%i" >nul
  4.     findstr /n .* "%%i" >.tmp
  5.     (
  6.         echo ^<html^>
  7.         echo ^<head^>
  8.         echo ^<title^>%%i^</title^>
  9.         echo ^</head^>
  10.         echo ^<body^>
  11.         for /f "delims=" %%i in (.tmp) do (
  12.             set str=%%i
  13.             SetLocal EnableDelayedExpansion
  14.             echo !str:*:=!^<br^>
  15.             EndLocal
  16.         )
  17.         echo ^</body^>
  18.         echo ^</html^>
  19.     ) >"%%i"
  20.     concmd /o:utf8 "%%i" "%%~ni.htm" >nul
  21.     del "%%i"
  22. )
  23. del .tmp
复制代码

作者: bbcwang123    时间: 2011-5-31 22:54

6# tmplinshi


恩! 我试试看 学习下!!
作者: batman    时间: 2011-5-31 23:26

vbs不行?
作者: bbcwang123    时间: 2011-6-1 11:15

8# batman


我小白不会用,学会试下!
作者: batman    时间: 2011-6-1 12:52

9# bbcwang123
保存为test.vbs在所在目录双击,不会?
作者: bbcwang123    时间: 2011-6-1 13:00

3# batman


经测试可用!    和批处理一样的效果, 个人感觉比批处理快一些.....  
主要还是有些编码的问题,  有些文件还是乱码.....    我再转换编码试一下......  
测试中............




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2