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!

Combine text files with conditions

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
0
0
US
I have this script for combining text files which works fine, but I have hit a big road block. I have a directory that contains approx 500 text files that I need to combine with a condition. I have many "pairs" of files that are job files where the name is like "JOB-A-TOP" & "JOB-A-BTM". I need to combine all of the pairs with the "-TOP" & "-BTM". Can someone assist me to get me started on how to accomplish this?

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("Merged.txt")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Combine_These'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In FileList
    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading) 
    strText = objTextFile.ReadAll
    objTextFile.Close
    objOutputFile.WriteLine objFile.Name & ", " & strText
Next

objOutputFile.Close
 
One approach could be something like this pseudocode
Code:
For Each objFile In FileList
[COLOR=blue]   If file ends in the first suffix
      If a file exists with same name but ends with the second suffix
         Merge the two files
      End if
   End If[/color]
Next

Functions like Left(), Right(), Mid() etc can help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top