Format a file to get desired output using VBScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 12061986
    New Member
    • Aug 2012
    • 5

    Format a file to get desired output using VBScript

    Hello ,

    I tried a code which reads the file InputFile.txt (attached) grep particular values and write it to a file in a desired output format is in file OutputFile.txt

    Below is the code partially I have done..

    Code:
    Dim re, targetString, colMatch, objMatch, nMT
    
    Set objFS = CreateObject("Scripting.FileSystemObject")
    
    Set rs = New RegExp
    
    strFileIN = "..\Inputfile.txt"
    strFileOUT = "..\Outputfile.txt"
    
    With rs
      .Pattern = "\w+\(\w\.+\)" & "\w+\(\w+\)" 
      .Global = True
      .IgnoreCase = True
    End With 
    
    Set objFile = objFS.OpenTextFile(strFileIN)
    strText = objFile.ReadAll
    
    
    
    Set colMatch = rs.Execute(strText)
    
    Set objOutFile = objFS.CreateTextFile(strFileOUT,True)    
    
    
    For nMT = 0 To colMatch.Count - 4
    
    objOutFile.WriteLine colMatch(nMT + 0) &" "& colMatch(nMT + 1) &" "& colMatch(nMT + 2) &" "& colMatch(nMT + 3)
    
    Next 
    
    
    
    objOutFile.Close
    Help me on correcting my code to get to my desired output as in file OutputFile.txt.
    Attached Files
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You haven't said what is wrong with the code. Not working is too general a statement.

    Also, since VBS is closer to VBA than VB6, I'm moving this to the Access forum where you may get more help.

    Comment

    • 12061986
      New Member
      • Aug 2012
      • 5

      #3
      QUEUE(TEST.QA.Q 1) TYPE(QLOCAL)
      CURDEPTH(0) MAXDEPTH(100000 )

      On my inputfile.txt , How do I form the regular expression pattern to match above pattern's. pls note word "TEST.QA.Q1 " may change and the words delimited by "." dots may vary in numbers.

      The pattern That i formed in my code didn't match QUEUE(TEST.QA.Q 1).

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Your regex pattern is malformed. It should be \w+\([\w\.]+\)
        Last edited by Rabbit; Aug 14 '12, 07:46 PM.

        Comment

        • 12061986
          New Member
          • Aug 2012
          • 5

          #5
          Thanks Rabbit . It worked. I will try my code.

          Comment

          • 12061986
            New Member
            • Aug 2012
            • 5

            #6
            Code:
            QUEUE(Q1.A1.C3.TEMP)         TYPE(QLOCAL)
               CURDEPTH(0)                             MAXDEPTH(1000)


            How to take values between ( and ) parenthesis and assign it to a variable ? I tried with mid function, but since my number of characters change it is not a solution. Do we have a special function ?



            Code:
            QUEUE=Q1.A1.C3.TEMP TYPE=QLOCAL CURDEPTH=0 MAXDEPTH=1000
            Last edited by 12061986; Aug 16 '12, 03:37 PM. Reason: corrected the question to specific.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              You already have the pattern, it's the one I posted earlier.

              Comment

              • 12061986
                New Member
                • Aug 2012
                • 5

                #8
                Corrected my question to be specific. Please check now.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  You can use the replace function.

                  Comment

                  • Mariostg
                    Contributor
                    • Sep 2010
                    • 332

                    #10
                    You are probably looking after placeholders ($1, $2) to use the expression you put between parenthesis in your regular expression. Here is an exemple I have in my codes. It takes a line of text, search for numbers having a negative sign at the end and move it at the beginning of the number.
                    Code:
                    Function BringForwardNegativeSign(LineToFix As String) As String
                    'This is to put negative sign in front 6.54- to -6.54 that exist in the DRMIS file.
                        Dim re As New RegExp
                        re.pattern = "\|( +)([0-9]*,?[0-9]*,?[0-9]*\.[0-9]{2})-"
                        re.Global = True
                        If (re.test(LineToFix) = True) Then
                            BringForwardNegativeSign = re.Replace(LineToFix, "|$1-$2")
                        Else
                            BringForwardNegativeSign = LineToFix
                        End If
                    End Function

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      I'd just like to mention here that Instr() may be the function you were looking for. It searches a string for another string and tells you where (or whether) it occurs.

                      Comment

                      Working...