VBS实现文本文件按行数拆分的脚本
今天写了一个VBS脚本,用于将指定的文本文件按行数拆成多个文件,如源文件为20万行,按5万行进行拆分,则通过该脚本可将其拆成4个文件。
其中实现了分级日志的功能,即可以设置不同的日志级别,在运行过程中向用户展现不同的提示信息。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 'Author: zhangbo2012@outlook.com '------------------------------------------------------------------------ '修订记录 '20140801 创建 '------------------------------------------------------------------------ Dim fso Set fso = CreateObject( "Scripting.Filesystemobject" ) Dim wsh Set wsh = createobject( "wscript.shell" ) Dim isdebug isdebug = 1 Const error = "[error]" Const info = "[info]" Const alert = "[alert]" log "START" ,alert cutfile "清理数据汇总1.txt" ,50000, ".csv" ,1 log "OVER" ,alert '------------------------------------------- 'cutfile(file,cutline,filetype,hastitle) 'file 需要处理的文件名 'cutline 拆分行数,如50000 'filetype 输出文件类型,如.csv 'hastitle 源文件中是否包含标题,如果包含 ' 则新创建的文件也会带有标题行 '------------------------------------------- Function cutfile(file,cutline,filetype,hastitle) If not fso.FileExists(file) then log file & " is not exists!" ,error exit function End If Set rf=fso.opentextfile(file,1) If hastitle then title = rf.readline line_cursor=1 file_cursor=0 Set wf=fso.createtextfile(file & "_" & file_cursor & filetype) wf.writeline title Do While rf.atendofstream=0 wf.writeline rf.readline line_cursor = line_cursor + 1 If line_cursor mod cutline = 0 then log "resolved " & line_cursor,info wf.close file_cursor = file_cursor + 1 Set wf=fso.createtextfile(file & "_" & file_cursor & filetype) wf.writeline title log "building " & file & "_" & file_cursor & filetype,info End If Loop End Function Function log(str,level) If not(level = info and isdebug=0) then wscript.echo level & "|" & Formattime & "|" & str End if End Function Function Formattime() Formattime = year(date) & string(2-len(month(date)), "0" ) & month(date) & string(2-len(day(date)), "0" ) & day(date) & string(2-len(hour(time)), "0" ) & hour(time) & string(2-len(Minute(time)), "0" ) & Minute(time) & string(2-len(Second(time)), "0" ) & Second(time) End Function |
分级日志执行效果
isdebug = 1
![](http://images0.cnblogs.com/blog/611477/201408/011041065246944.png)
isdebug = 0
![](http://images0.cnblogs.com/blog/611477/201408/011041078058985.png)
error级别日志
![](http://images0.cnblogs.com/blog/611477/201408/011041080402214.png)