Solved

Sort contents of text file with headers alphabetically using vbs

Posted on 2008-10-27
2
444 Views
Last Modified: 2010-04-21
Hi all,

I need to sort, using vb script, the contents of a text file that looks like this:

[WEBN]
Wam Es= 1
AIG Esk=1
Aviv RJF=1
[TABB]
Sch Ins=1
CMH Cap=1
AIG Esk=1
Stikem Ell=1

to have the information under the users name alphabetical like this:

[WEBN]
AIG Esk=1
Aviv RJF=1
Wam Es= 1
[TABB]
AIG Esk=1
CMH Cap=1
Sch Ins=1
Stikem Ell=1

As you can see, the only items to be sorted are those between the closing ] of a users name to the opening [ of another users name, etc.  Please note that I do not need the users sorted at all, just the data below their section.  In my example, [WEBN] can continue to come before [TABB].

I need this in vb script.  Any help would be appreciated.  Thanks.
0
Comment
Question by:smsstech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 22815004
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFile variable with the location of the .ini file.  Running the script will sort the lines under each heading.

This script requires the .NET Framework to be installed.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strFile = "sample.ini"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
strText = objFile.ReadAll
arrText = Split(strText, vbCrLf)
objFile.Close
 
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
 
For Each strINILine in arrText
    If Left(strINILine, 1) = "[" Then
        If IsArray(arrSection) Then
            For Each strLine in fSortArray(arrSection)
                objFile.WriteLine strLine
            Next		
			
            Set arrSection = nothing
        End If
        objFile.WriteLine strINILine
    ElseIf Not IsArray(arrSection) Then
        ReDim arrSection(0)
        arrSection(0) = strINILine
    Else
        i = UBound(arrSection) + 1
        ReDim Preserve arrSection(i)
        arrSection(i) = strINILine
    End If
Next
 
If IsArray(arrSection) Then
    For Each strLine in fSortArray(arrSection)
        objFile.WriteLine strLine
    Next
End If
 
objFile.Close
 
Function fSortArray(arrSortThisArray)
    Set objArrayList = CreateObject("System.Collections.ArrayList" )
    For i = 0 To UBound(arrSortThisArray)
        objArrayList.Add arrSortThisArray(i)
    Next
    objArrayList.Sort
    set fSortArray = objArrayList
End Function

Open in new window

0
 

Author Closing Comment

by:smsstech
ID: 31510324
Shift-3, thank you very much.  That worked like a charm.  I was originally getting a ubound error with my array and was starting to second guess myself, hence the support call as my issue was becoming urgent.  Perfect coding my man.  Cheers.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question