Want to read all characters before space from each line of a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaagarw
    New Member
    • Oct 2012
    • 1

    Want to read all characters before space from each line of a text file

    My text file is :
    267326sgdhs2378 hdfksfldsjl
    3276jfdjksh8427 s287328hfd23 jdhfhw

    Now I want to read "267326sgdhs237 8" and "3276jfdjksh842 7s287328hfd23" from that text file and store them in two variables.
    Later on I want to pass these two variables as a command line argument to the batch file.

    Please help in doing both.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What do you have so far?

    Comment

    • twinnyfo
      Recognized Expert Moderator Specialist
      • Nov 2011
      • 3653

      #3
      I would start by finding the length of the string, then, using a pointer, go character by character until I found a space. Save partial part of the string, based on the pointer, and repeat for the next segment of the string.

      The rest is simply creating a text file and saving as an executable extension....

      But, Rabbit is correct, we would need to know your efforts so far in order to assist in greater detail....

      Comment

      • smartchap
        New Member
        • Dec 2007
        • 236

        #4
        Read first line in a variable, use INSTR$() to fine characters before a space, now use LEFT$() to put these left characters (before a space) in the desired variable. Go to next line and so on.
        In place of this can use SPLIT function to read characters before first space.

        Comment

        • IraComm
          New Member
          • Oct 2012
          • 14

          #5
          Here is some code called "GetString" which alows the user to define where in a string they want to retrive some portion of that string.

          As in the example above, I would use "Line Input #ff,a$" to get the line from the file. Next invoke GetString

          Ret = GetString("",,, " ")
          This will get whatever is at the beginning of the line up to the first space. (non-inclusive)

          Code:
          Public Function GetString(vString, StartChrs As String, _
          Optional StartPos As Integer = 0, _
          Optional LenOfFind As Integer = 0, _
          Optional EndChrs As String = "") As String
          
          
          Dim L As Integer
          Dim E As Integer
          Dim S As Integer
          'Wild cards _
          
          If vString = "" Then
              Exit Function
          End If
          If StartPos > 0 Then
              If StartChrs <> "" Then
                  S = InStr(StartPos, vString, StartChrs)
                  If S = 0 Then
                      Exit Function
                  End If
                  S = S + Len(StartChrs)
              Else
                  S = StartPos
              End If
          Else
              If StartChrs <> "" Then
                  S = InStr(vString, StartChrs)
                  S = S + Len(StartChrs)
              Else
                  S = 1
              End If
          End If
          If LenOfFind > 0 Then
              GetString = Mid$(vString, S, LenOfFind)
              Exit Function
          End If
          If EndChrs <> "" Then
              L = InStr(vString, EndChrs) - S
              If L = 0 Then
                  GetString = Mid$(vString, S)
              Else
                  GetString = Mid$(vString, S, L)
              End If
          Else
              GetString = Mid$(vString, S)
          End If
          
          
          End Function

          Comment

          Working...