Follow us on Twitter

Have a question on something? Visit the forum for help.

NEW!! LazyNetworkAdmin Forums! Click -> here

 

Combine multiple text files PDF Print E-mail

An engineer asked me to create a script that would take ASCII files and combine them for use with a piece of software. The names of the files were all different, and they needed to sort the data by creation/ modification date. So, I created the script below with a little help from the Windows Script Repository.

----------COPY EVERYTHING BELOW THIS LINE for the Script----------
'' This script will combine text files into one big file
'Created by Cheyenne Harden 7.13.07 with some help from the Windows scripting Guys.

Const ForReading = 1
strComputer = "."
RenameFiles ()
WriteFiles ()

 

Function RenameFiles ()
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Documents and Settings\test.user\Desktop\text'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In FileList
    strDate = Left(objFile.CreationDate, 24)
    strNewName = objFile.Drive & objFile.Path & _
       strDate & "." & "txt"
    strNameCheck = Replace(strNewName, "\", "\\")

    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
        Else
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                strDate & "_" & i & "." & "txt"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
    Loop
Next
End Function


Function WriteFiles ()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")


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

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Documents and Settings\test.user\Desktop\text'} Where " _
        & "ResultClass = CIM_DataFile")

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

objOutputFile.Close
End Function

----------COPY EVERYTHING ABOVE THIS LINE for the Script----------

 

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need the items below!


1. Make sure to use a copy of the files you want to combine
2. Change the two lines that look like the item below to represent your file structure.
 Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Documents and Settings\test.user\Desktop\text'} Where " & "ResultClass = CIM_DataFile")
3. If you want you can change the name of the output file, see the line below.
 Set objOutputFile = objFSO.CreateTextFile("output.txt")
 
This information is provided "AS IS" with no warranties expressed or implied.




Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! TwitThis Joomla Free PHP
 
 

Sponsored Links