×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VB script to append multiple text files into single individual files without including Headers

VB script to append multiple text files into single individual files without including Headers

VB script to append multiple text files into single individual files without including Headers

(OP)
Hi All- I need to write a VB script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames?
AAAL_555A_ORANGE1_F190404.TXT
AAAL_555A_ORANGE2_F190404.TXT
AAAL_555A_ORANGE3_F190404.TXT
AAAL_555A_ORANGE4_F190404.TXT
AAAL_555A_MANGO_F190404.TXT
AAAL_555A_MANGO2_F190404.TXT
AAAL_555B_APPLE_F190404.TXT
AAAL_555B_ORANGE_F190404.TXT
AAAL_555B_Orange_F190404.TXT


If second part of filename='555A' and third part consists of ORANGE then all Oranges content files will merger into one file with filename as AAAl_555A_ORANGE.txt.

If second part of filename='555B' and third part consists of ORANGE then all Oranges content files will merger into one file with filename as AAAl_555B_ORANGE.txt.

If second part of filename='555A' and third part consists of MANGO then all Oranges content files will merger into one file with filename as AAAl_555A_MANGO.txt, etc.

I have tried the below code its not working.Kindly help

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\test"
Set objfolder = ObjFSO.GetFolder(objStartFolder)
Set colfiles = objfolder.Files
Set re = New RegExp
re.Pattern = "\d+$"

For Each objFile In colfiles
a = Split(objFile.Name, "_")

'Construct the basename of the output file from the elements of the split
'input filename. Use a regular expression replacement to remove trailing
'digits from the third element.
BaseName = a(0) & "_" & a(1) & "_" & re.Replace(a(2), "")
Filename = BaseName & ".txt"
MsgBox Len(BaseName)
MsgBox Left(objFile.Name, Len(BaseName))
MsgBox BaseName
MsgBox objFile.Name

If Left(objFile.Name, Len(BaseName)) = BaseName Then
Set outFile = ObjFSO.OpenTextFile(Filename, 8, True)
Set inFile = ObjFSO.OpenTextFile(objFile.Path, ForReading)
Do Until inFile.AtEndOfStream
outFile.WriteLine inFile.ReadLine
Loop
inFile.Close
outFile.Close
End If
Next
End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close