Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

shankar455

Programmer
May 2, 2019
2
0
0
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top