|
-
May 8th, 2019, 05:53 AM
#1
Thread Starter
New Member
Combining multiple .TXT files into one if a string matches in filename
I am completely newbie to VB.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 Mango content files will merger into one file with filename as AAAl_555A_MANGO.txt
... etc
Note : While merging the first three lines and last three lines should ignore
Code i tried
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
Last edited by shank455; May 9th, 2019 at 06:55 AM.
Reason: posted the code used
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|