每行文字转换为15字符长度

' vbs1: 批处理文件.vbs

'注意: 运行脚本前必须手动创建文件夹a, b, c, 并将要处理的文件放在a中,b,c为空文件夹
'处理后的txt文件在b中, 在将b中的txt文件复制到c中的doc文档中
Dim ws
Set ws = CreateObject("WScript.Shell")

Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(".\a")
Set fc = f.Files
For Each f1 in fc
 If (f1.Name <> "批处理文件.vbs" Or f1.Name <> "转换为15字符长度.vbs") Then
   ws.Run "Wscript.exe 转换为15字符长度.vbs " & f1.Name
 End If
Next

MsgBox "End"

Sub m(s)
 MsgBox s
End Sub

'  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

' 将每行文字转化为15字符长
Option Explicit
Const ForReading=1
Const ForWriting=2
Const LineCharNum = 15
Dim fso, fr, fw, fPath, f, allText
Dim sFile, srLine, swlineLength, nxLine, i, swLine
Set fso=CreateObject("scripting.filesystemobject")
'sFile = "abc.txt" '处理单个文件
sFile = WScript.Arguments(0) '被"批处理文件.vbs"调用, 批量处理文件
Set fr = fso.OpenTextFile(".\a\" & sFile, ForReading, True)
Set fw = fso.OpenTextFile(".\b\" & sFile, ForWriting, True)
Do While (NOT fr.AtEndOfStream)
 srLine = fr.ReadLine
 swLineLength = Len(srLine)
 nxLine = CInt(swLineLength / LineCharNum)

 For i = 1 To nxLine
  swLine = Mid(srLine, (i-1)*LineCharNum+1, LineCharNum)
  fw.WriteLine swLine
 Next

 If nxLine = 0 Then
  fw.WriteLine srLine
 ElseIf (swLineLength Mod 15) <> 0 Then
  swLine = Mid(srLine, nxLine * LineCharNum+1)
  fw.WriteLine swLine
 End If
Loop

fr.Close
fw.Close
Set fr = fso.OpenTextFile(".\b\" & sFile, ForReading, True)
allText = fr.ReadAll
fPath = ".\c\" & sFile & ".doc"
Set fr = fso.OpenTextFile(fPath, ForReading, True)
fr.Close
Set f = fso.GetFile(fPath)
fPath = f.Path
toWord fPath, allText

Set fso=Nothing
'MsgBox "End"

Sub m(s)
 MsgBox s
End Sub

'''向DOC文档的末尾追加文本:::
Sub toWord(fileName, text)

 Const wdStory = 6
 Const wdMove = 0
 Dim objWord, objDoc, objSelection
 Set objWord = CreateObject("Word.Application")
 objWord.Visible = False

 Set objDoc = objWord.Documents.Open(fileName)

 Set objSelection = objWord.Selection
 'objSelection.EndKey wdStory, wdMove
 'objSelection.HomeKey wdStory, wdMove
 'objSelection.TypeParagraph()
 objSelection.TypeText text
 objWord.Documents.Save
 objWord.Quit
End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值