' 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