trospe
trospe

Reputation: 13

VBscript: Loop through all text files in a folder, delete the first and last line of each file and merge into one file

I am a newbie and looking for a way in vb script to do the following: I have several text files in a specific folder (C:\data), I want to loop through all the text files, delete the first and last line of each text file and then combine/merge all the files into one text file. Your help is much appreciated. Thanks in advance.

Here is what I have written so far after hours of reading/searching the web (but it ends with an endless loop):

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("C:\data\")
Set outFile = objFSO.CreateTextFile("C:\data\out\testout.txt")

for each file in folder.Files

if lcase(objFSO.getExtensionName(file.path))="txt" then

Set testfile = objFSO.OpenTextFile(file.path, ForReading)


Do Until testfile.AtEndOfStream
  If testfile.Line <= 1 Then
    testfile.SkipLine
  Else
    if Not IsEmpty(line) Then outFile.WriteLine(line)
    line = testfile.ReadLine
  End If
Loop

testfile.Close

End If

next

outFile.Close

Upvotes: 1

Views: 4280

Answers (2)

JosefZ
JosefZ

Reputation: 30113

Always use option explicit statement. Doing that helps to reveal errors in syntax and in logic as well.

Here are some code improvement hints:

  • Involve a line counter ( see linecount).
  • IsEmpty function returns a Boolean value indicating whether a variable has been initialized. Did you mean empty string?
  • Note and think about line = "" statements.

However, next code snippet could work. But could work as expected?

Set testfile = objFSO.OpenTextFile(file.path, ForReading)
line = ""
linecount = 1           ' or 0 ?
Do Until testfile.AtEndOfStream
  If linecount <= 1 Then
    testfile.SkipLine
    line = ""
  Else
    if Not Trim(line) = "" Then outFile.WriteLine(line)
    line = testfile.ReadLine
  End If
  linecount = linecount +1
Loop
testfile.Close

Edit according to Ekkehard.Horner's comment (thank you):

  • he is right with 5th wheel:),
  • he is right with 1st line skipping,
  • the last line will not be written because .ReadLine succeeds output writing.

So the code snippet could be as follows:

Set testfile = objFSO.OpenTextFile(file.path, ForReading)
line = ""
if not testfile.AtEndOfStream then testfile.SkipLine
Do Until testfile.AtEndOfStream
    if Not Trim(line) = "" Then outFile.WriteLine(line)
    line = testfile.ReadLine
Loop
testfile.Close

Edit2: to retain empty lines

Set testfile = objFSO.OpenTextFile(file.path, ForReading)

set line=Nothing

if not testfile.AtEndOfStream then testfile.SkipLine
Do Until testfile.AtEndOfStream
    if Not IsObject(line) Then outFile.WriteLine(line)
    line = testfile.ReadLine
Loop
testfile.Close

Edited according to this Ekkehard.Horner's comment

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

What I couldn't put into the comment:

type 28507073.vbs
Option Explicit

Dim tsIn : Set tsIn = CreateObject("Scripting.FileSystemObject").OpenTextFile(".\28507073.txt")
If tsIn.AtEndOfStream Then
   WScript.Echo "Empty file"
Else
   tsIn.SkipLine
   If Not tsIn.AtEndOfStream Then
      Dim s : s = tsIn.ReadLine()
      Do Until tsIn.AtEndOfStream
         WScript.Echo s
         s = tsIn.ReadLine()
      Loop
   End If
End If
tsIn.Close

type 28507073.txt
1
2
3
4

cscript 28507073.vbs
2
3

Upvotes: 0

Related Questions