Convert Text to VB Command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powersakthi
    New Member
    • Aug 2007
    • 5

    Convert Text to VB Command

    Hi All,
    There is a text file with content for example
    now()
    In vb i will read the content of this text file and then i want to execute this as a vb command. How to do that?

    Below is flow...

    Code:
      Dim handle As Integer 
      Dim sInhalt As String 
    
      handle = FreeFile 
    
      Open strFilename For Binary As #handle 
      sInhalt = Space$(LOF(handle)) 
      Get #handle, , sInhalt 
      Close #handle
    Now writing the read value to output in a text box.

    Code:
      Text1.Text = sInhalt

    Now if we execute this program, i'm getting only now() in the Text1
    control instead i want 10/3/2007 7:09:11 PM in the text1 control. How
    to do this is my question..... Similiary any command can be written in
    the file and if we execute then vb should read the content of the text file and find recognize as command and not text and execute it.... How to do this??? Any api or logic ....???

    OS: Windows
    Language: VB6
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Use "Microsoft Script Control"

    Regards
    Veena

    Comment

    • powersakthi
      New Member
      • Aug 2007
      • 5

      #3
      Hi Veena,
      Actually my requirement is some thing like this...

      I'm writing a parser which has standard xml format which will be written in a file like this
      "<TESTDATA><FAC TORY NAME=""SAKTHI"" TESTER=""" & Getcontent(97, ":", 1) & """ /></TESTDATA>"

      and this parser has to watch a folder constantly and then once file is dropped then the parser should read the contents and then execute this xml which means Getcontent(97, ":", 1) has to be translated as "Passed" . "Passed" is nothing but the string in the 97th line's first value of the tester log after splitting using : ...

      97th line is as below
      H UUT Result: Passed

      so whenever any file is dropped into that folder the parser reads the file and then reads the xml from the xmlinput.txt file and then translates the xml....

      This xml will vary incase if we are asked to read some more contents in 4th line and 8th line....

      So now if i read the content of the xmlinput.txt and then write it to a RichTextbox, vb just just writes as it is
      "<TESTDATA><FAC TORY NAME=""SAKTHI"" TESTER=""" & Getcontent(97, ":", 1) & """ /></TESTDATA>"

      but instead i want
      <TESTDATA><FACT ORY NAME=""SAKTHI"" TESTER="Passed" /></TESTDATA>

      How to do this????? Scripting has some limits since some of the vbcommands wont work and Memory Management is very important for parser....

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        OK, in that case, Script Control is not going to help you, you have to write a Parser of your own, Open the file, and check for the key word. And go to that line, and more over, not very sure, if Rich Text box is going to help you.
        Open the file using FSO and start parsing each line and save in some other file.

        REgards
        Veena
        Last edited by Killer42; Oct 5 '07, 06:16 AM.

        Comment

        • powersakthi
          New Member
          • Aug 2007
          • 5

          #5
          Hi All,

          Click here to download Parser Source Code

          Please check the attachment for my sample source code which tells what i need ultimately.... Microsoft scripting has some limitations and memory management issues... So Please suggest some method...

          Once you run the exe in the attachment or in vbp you can see two command buttons. If you click on the first button... the xml string is read from a text file and then displayed in the text box and if you click the command button 2 you can see a string of xml is displayed in the text box...

          My Requirement is command1's output should come like command2's output... How to do?? Any API or Idea or Tips???

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Hi,

            Add References "Microsoft Script RunTime" to ur project and a new Command button (cmdNew) and place this code in cmdNew_Click event :

            [code=vb]
            Private Sub cmdNew_Click()
            '
            Dim FSO As New FileSystemObjec t
            Dim TArr
            Dim fsStr As TextStream
            Dim NewString As String
            Dim FindStr As String
            Dim i As Integer
            Dim j As Integer
            Dim LineNo As Integer
            Dim TempStr As String
            '
            NewString = ""
            FindStr = ""
            LineNo = 0
            '
            Set fsStr = FSO.OpenTextFil e(App.Path & "\xmlinput.txt" , ForReading)
            TArr = Split(fsStr.Rea dAll, vbCrLf)
            NewString = TArr(0)
            'ASSUMING ONE LINE IN XML FILE
            'FOR MULTIPLE LINES IN XML, LOOP
            fsStr.Close
            Set TArr = Nothing
            '
            i = InStr(UCase(New String), "GETCONTENT ")
            If i > 0 Then
            FindStr = Mid(NewString, i)
            j = InStr(FindStr, "(")
            If j > 0 Then
            LineNo = Val(Mid(FindStr , (j + 1)))
            End If
            End If
            '
            Set fsStr = FSO.OpenTextFil e(App.Path & "\sample1.t xt", ForReading)
            TArr = Split(fsStr.Rea dAll, vbCrLf)
            fsStr.Close
            '
            LineNo = LineNo + 1
            If LineNo > 0 And LineNo <= UBound(TArr) Then
            TempStr = TArr(LineNo)
            j = InStr(TempStr, ":")
            If j > 0 Then
            TempStr = Trim(Mid(TempSt r, (j + 1)))
            TempStr = Replace(TempStr , Chr(9), "")
            End If
            End If
            '
            NewString = Replace(NewStri ng, """", "")
            i = InStr(UCase(New String), "GETCONTENT ")
            If i > 0 Then
            j = InStr(i, NewString, "&")
            NewString = Left(NewString, (i - 3)) & """" & TempStr & """" & Mid(NewString, (j + 1))
            RTB.Text = NewString
            End If
            '
            Set FSO = Nothing
            '
            End Sub

            [/code]

            It gives u basic idea, how u can parse the textfile..
            u can refine the code


            Regards
            Veena

            Comment

            • powersakthi
              New Member
              • Aug 2007
              • 5

              #7
              Hi Veena,
              Thanks for your reply! i also thought of same logic but then was waiting for some api stuffs which can directly execute the statement... Is there any other shortway with which like

              StrxmlOut = Execute(xmlfile content)

              RTB.Text = Strxmlout

              Something like this???
              Again, Thanks for your reply.... Also ||'ly one more clarification.. . FSO is better or OPEN ing file in Binary mode is better???

              Comment

              • QVeen72
                Recognized Expert Top Contributor
                • Oct 2006
                • 1445

                #8
                Hi,

                Always FSO is better. It is meant for better file operations. Comparatively it is faster also.
                In one go, "ReadAll" can transfer the contents from file to an array. Once you have all the contents in an array, you can do all the parsing/rearranging easily. With File IO you need to open and read line by line just to count number of lines.

                REgards
                Veena
                Last edited by Killer42; Oct 5 '07, 06:18 AM.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by QVeen72
                  Always FSO is better. It is meant for better file operations. Comparatively it is faster also.
                  In one go, "ReadAll" can transfer the contents from file to an array. Once you have all the contents in an array, you can do all the parsing/rearranging easily. With File IO you need to open and read line by line just to count number of lines.
                  That's just not true.

                  FSO does generally provide better functionality. But I doubt that it's any faster. If you open a file in Binary mode, you can also transfer the entire thing into a string or an array in one go.

                  Generally, I would recommend using FSO, as it provides lots of great functionality for dealing with drives, folders and files. But it should also be noted that it does have one or two small drawbacks. For instance...
                  • It requires additional of a reference to your project.
                  • It requires slightly more complex coding, compared to the built-in file processing.
                  • As I've recently learned the hard way, if you do use it to process a file line-by-line, it doesn't provide any way to track your progress your progress. In other words, you can't tell where you're up to.

                  Comment

                  Working...