#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Need script that extracts text parts from .txt file


    I need a small script that uses regular expressions (egrep) or something similar to extract small pieces of information from .txt files, and put this info in a new comma separated .txt. Like this:

    1. User put into script the number of iterations to loop (into the code). From A to B. The iterations is the same as the number of files to process.

    2. In a set directory, the .txt files have prefixes [001_, 002_ etc]. Script loads the first file.

    3. Script checks the FIRST row for a match of [ N], and extract the part between the before N and the previous on that row. (Example: 00 4430.7486 N 06417.4951 W). If no match, then script instead check on that FIRST row a match of [,N] and extract the part between the , before N and the previous , on that row. (Example: 0,4430.7483,"N",6417.4969,"W")

    4. Script checks the FIRST row for a match of [ W], and extract the part between the before W and the previous on that row. (Example: 00 4430.7486 N 06417.4951 W). If no match, then script instead check on that FIRST row a match of [,W] and extract the part between the , before W and the previous , on that row. (Example: 0,4430.7483,"N",6417.4969,"W")

    5. Script does the same thing as on 3 above, but for the LAST row in the current .txt file.

    6. Script does the same thing as on 4 above, but for the LAST row in the current .txt file.

    7. Script creates a new .txt file with: loop iteration number, Extraction in 3, Exctraction in 4, Extraction in 5, Extraction in 6. (Example: 001, 4413.7896, 6413.7896, 4412.1256, 6415.2223)

    8. Script starts over with next iteration in loop, loads a new .txt file and write a new row in the created .txt file for each round.

    Could someone help me with this ? It for a hobby project.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    You want to write a stand-alone script to do this, like VBScript or JScript? Or do you want to do this from Visual Basic?

    If you want to use VBScript or JScript, check out Scripting.FileSystemObject. It allows you to do the things you mentioned, such as listing all of the files in a directory, opening text files to read their contents, and creating text files for output.

    If you'd like further help, please give an example directory listing of the files, an example of one of the files you need to process, and what the script needs to do with it.
    Last edited by Clone53421; February 4th, 2014 at 03:34 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    This is VB6 code, but to make it easier to convert to VBA, I have declared most variables as variant.

    J.A. Coutts
    Code:
    Private Sub Command1_Click()
        Dim FileName, PathName, FileNum
        Dim FileLine, LastLine, N, LineNum
        Dim FileArray() As String
        PathName = App.Path & "\"   ' Set the path.
        FileName = Dir(PathName)   ' Retrieve the first entry.
        Do While FileName <> ""   ' Start the loop.
            If Val(FileName) And Mid$(FileName, 4, 1) = "_" Then
                Debug.Print FileName
                FileNum = FreeFile
                Open PathName & FileName For Input As FileNum
                LineNum = 1
                While Not EOF(FileNum)
                    Line Input #FileNum, FileLine
                    If Len(FileLine) > 0 Then LastLine = FileLine
                    If LineNum = 1 Then
                        Debug.Print ProcessLine(FileLine)
                    End If
                    LineNum = LineNum + 1
                Wend
                Debug.Print ProcessLine(LastLine)
                Close FileNum
            End If
            FileName = Dir   ' Get next entry.
        Loop
    End Sub
    
    Function ProcessLine(Line)
        Dim FileArray() As String
        Dim N As Integer
        FileArray = Split(Line, " ", , 1)
        For N = 0 To UBound(FileArray)
            If FileArray(N) = "N" Or FileArray(N) = "W" Then
                ProcessLine = FileArray(N - 1)
                Exit Function
            End If
        Next N
        'Replace double quote with single
        Line = Replace(Line, Chr$(&H22), "'")
        FileArray = Split(Line, ",", , 1)
        For N = 0 To UBound(FileArray)
            If FileArray(N) = "'N'" Or FileArray(N) = "'W'" Then
                ProcessLine = FileArray(N - 1)
                Exit Function
            End If
        Next N
        ProcessLine = ""
    End Function

IMN logo majestic logo threadwatch logo seochat tools logo