shankar455
Programmer
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
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