VBS脚本,在多个txt

时间:2016-02-17 14:50:36

标签: text vbscript numbers line

我在文件夹中有一堆txt文件,我需要在每个文件夹中添加一行到指定的行号。

input box 1 : New lineofText as string.
input box 2 : Line where I want this lineoftext to be inserted, as an integer.

我发现只是在顶部或底部添加文字,如下所示:

Dim FSO, txs, fld, fil, content
newline = inputbox("New line :")
line = inputbox("Which line :")
Set FSO = CreateObject("Scripting.FileSystemObject")

Set fld = FSO.GetFolder("\folderpath")
For Each fil In fld.Files
If Right(fil.Name, 3) = "txt" Then

    Set txs = fil.OpenAsTextStream(1) ' 1 = for reading
    content = txs.ReadAll
    txs.Close

    Set txs = fil.OpenAsTextStream(2) ' 2 = for writing
    txs.Write newline & vbCrLf & content
    txs.Close

End If    
Next

写到最后我只是切换它:

txs.Write newline & vbCrLf & content

到此:

txs.Write content & vbCrLf & newline

但是如何让它将新行写入指定的行号?

编辑:用户duDE告诉我,我需要用这个计算行数:

Dim nLineToAddTheNewLine : nLineToAddTheNewLine = 5 ' your desired line where you need the new line
line = 5
newline = "blablabla"
filename = "C:\myfile.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)

Dim nCount : nCount = 0
Dim sContent : sContent = ""

Do Until f.AtEndOfStream
WScript.Echo f.ReadLine
sContent = sContent & f.ReadLine
nCount = nCount + 1
If nCount = line Then sContent = sContent & vbCrLf & newline
Loop
f.Close

此代码适用于具有直接名称的一个文件。 所以在这一点上,我在使脚本工作时遇到了很多麻烦。 这就是我被困的地方:

Const ForReading = 1
Const ForWriting = 2
'newline = inputbox("New line :")
'line = inputbox("Which line :")
newline = "blablabla"
line = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Agent-API Blocking Queue Size: [1-9]+[0-9]*"
objStartFolder = "C:\MyfolderWithLotOfFiles\"

Dim lineCount : lineCount = 0
Dim firstContent : firstContent = ""

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
  lineCount = 0
  firstContent = ""
  FileName = objStartFolder & objFile.Name
  WScript.echo FileName
  Set objStream = objFSO.OpenTextFile(FileName, ForReading)
  Do Until objStream.AtEndOfStream
    lineCount = lineCount + 1
    firstContent = firstContent & objStream.ReadLine
    if lineCount = line Then 
        Set objStream = objFSO.OpenTextFile(FileName, ForWriting)
        objStream.Write firstContent & vbCrLf & newline & vbCrLf
    End if
  Loop  
  objStream.Close 
Next

脚本正在读取每个txt文件的每一行,但是当我想写时出错,请帮助!

if lineCount = line Then 
    Set objStream = objFSO.OpenTextFile(FileName, ForWriting)
    objStream.Write firstContent & vbCrLf & newline & vbCrLf
End if

1 个答案:

答案 0 :(得分:2)

试试这段代码:

Const ForReading = 1
Const ForWriting = 2
'newline = inputbox("New line :")
'line = inputbox("Which line :")
newline = "blablabla"
line = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\MyfolderWithLotOfFiles\"

Dim lineCount : lineCount = 0
Dim firstContent : firstContent = ""

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If LCase(objFSO.GetExtensionName(objFile)) = "txt" Then
        lineCount = 0
        firstContent = ""
        FileName = objStartFolder & objFile.Name
        WScript.echo FileName
        Set objStream = objFSO.OpenTextFile(FileName, ForReading)
        Do Until objStream.AtEndOfStream
            lineCount = lineCount + 1
            firstContent = firstContent & objStream.ReadLine & vbCrLf
            if lineCount = line Then 
                firstContent = firstContent & newline & vbCrLf
            End if
        Loop  
        Set objStream = objFSO.OpenTextFile(FileName, ForWriting)
        objStream.WriteLine firstContent
        objStream.Close 
    End If  
Next