Sponsored Links

Login Form






Lost Password?

Syndicate

Home
Combine multiple text files PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Cheyenne Harden   
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.

Comments
Add NewSearch
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
 
Security Image
Please input the anti-spam code that you can read in the image.

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.

 
Tag it:
Delicious
Furl it!
Spurl
digg
YahooMyWeb
< Prev   Next >
 

Google Search

Google