readfile and writefile via VLISP ActiveX

Discussion in 'AutoCAD' started by kozmos, Jan 7, 2004.

  1. kozmos

    kozmos Guest

    By reference to VxReadTextFile from MENZI ENGINEERING GmbH. I create new codes to that function
    by using "READALL" method instead of "readline". By using "READALL", the list build will be processed
    in AutoLISP speed, I have not made any test about the speed difference between using "READLL" and "READLINE"
    method for big ASCII text file (size > 50kb, line number > 5000). Maybe somebody there can have the result.

    2) As an opposite function, the new vldos-writefile function can not run as well as vldos-readfile
    function do. The automation returned error. Is there any one know the trap?

    Following are both of the codes

    ;| Read all content of a text file into a list (more fast tha AutoLISP read-line)
    Syntax: (vldos-readfile FilenameToRead[STRING])
    ARG1: The full path file name to be read. (contain extension)
    Only text file can return good result.
    Description:
    Read all content of a text file and convert them into a list
    Return Value:
    [Success]: List contain text contents line by line
    [F a i l]: NIL
    |;
    (Defun vldos-readfile
    (Fil / string->list FilObj FilPth FilSys OpnFil All)
    (Defun string->list (String / ID Rtn) ;;; Convert string into list by "\r\n"
    (if (null (setq ID (vl-string-search "\r\n" String)))
    (setq Rtn (list String))
    (while ID
    (setq Rtn (cons (substr String 1 ID) Rtn)
    String (substr String (+ 3 ID))
    ID (vl-string-search "\r\n" String)
    )
    )
    )
    (setq Rtn (reverse (cons String Rtn)))
    Rtn
    )
    (if (and (setq FilPth (findfile Fil))
    (setq FilSys (vlax-create-object "Scripting.FileSystemObject"))
    )
    (progn
    (setq FilObj (vlax-invoke FilSys "GetFile" FilPth)
    OpnFil (vlax-invoke FilObj "OpenAsTextStream" 1 0)
    All (string->list (vlax-invoke OpnFil "readall"))
    )
    (vlax-invoke OpnFil "Close")
    (vlax-release-object OpnFil)
    (vlax-release-object FilObj)
    (vlax-release-object FilSys)
    )
    )
    All
    )
    ;| Write a string list into a text file (more fast tha AutoLISP write-line)
    Syntax: (vldos-writefile FileNameString[STRING] ContentStringList
    • ModeFlag[BOOLEAN])
      (vldos-writefile FileNameString[STRING] ContentString[STRING] ModeFlag[BOOLEAN])
      ARG1: The filename string
      ARG2: The string or string list to write
      ARG3: The append or overwrite mode flag. nil for append, T for overwrite
      Description:
      write a string list into a file
      Return Value:
      [Success]: The file name string.
      [F a i l]: NIL
      |;
      (Defun vldos-writefile
      (Fil TXT Mode / list->string FilObj FilPth FilSys OpnFil Line)
      (Defun list->string (slist / line rtn)
      (if (= (type slist) 'str)
      (setq rtn slist)
      (progn
      (setq rtn "")
      (foreach line slist
      (if (null rtn)
      (setq rtn line)
      (setq rtn (strcat rtn "\r\n" line))
      )
      )
      )
      )
      rtn
      )
      (if (and Mode (findfile Fil))
      (vl-file-delete Fil)
      )
      (if (setq FilSys (vlax-create-object "Scripting.FileSystemObject"))
      (progn
      (if (null (findfile Fil))
      (vlax-invoke-method FilSys "CreateTextFile" Fil 1 1)
      )
      (if (setq FilPth (findfile Fil))
      (progn
      (setq FilObj (vlax-invoke FilSys "GetFile" FilPth)
      OpnFil (vlax-invoke FilObj "OpenAsTextStream" 1 0)
      )
      ;;; ERROR BEGIN HERE!!!
      (vlax-invoke OpnFil "readall") ;;; read to end, new file the end
      (vlax-invoke OpnFil "Write" (list->string TXT))
      (vlax-invoke OpnFil "Close")
      (vlax-release-object OpnFil)
      (vlax-release-object FilObj)
      (vlax-release-object FilSys)
      )
      )
      )
      )
      filpth
      )
     
    kozmos, Jan 7, 2004
    #1
  2. I took a look at that code, and thought it might be easier to just post what I use:

    function to take a text file to a list

    (defun FILE-TO-LST-ALL (Fil
    / FilObj FilPth FilSys OpnFil RetVal
    FileH TextString)
    (if (setq FilPth (findfile Fil))
    (progn
    (setq FilSys (vlax-create-object "Scripting.FileSystemObject")
    FilObj (vlax-invoke FilSys "GetFile" FilPth)
    OpnFil (vlax-invoke FilObj "OpenAsTextStream" 1 0)
    )
    (while (= (vlax-get OpnFil "AtEndOfStream") 0)
    (setq RetVal (cons (vlax-invoke OpnFil "ReadLine") RetVal))
    )
    (vlax-invoke OpnFil "Close")
    (vlax-release-object OpnFil)
    (vlax-release-object FilObj)
    (vlax-release-object FilSys)
    (reverse RetVal)
    )
    nil
    )
    )

    function to send list to a text file

    (defun LST-TO-FILE-ALL (LST Fil / FilObj FilPth FilSys OpnFil RetVal)
    (if (setq FilPth (findfile Fil))
    (progn
    (setq FilSys (vlax-create-object "Scripting.FileSystemObject")
    FilObj (vlax-invoke FilSys "GetFile" FilPth)
    OpnFil (vlax-invoke FilObj "OpenAsTextStream" 2 0)
    )
    (FOREACH LINE LST
    (vlax-invoke OpnFil "WriteLine" LINE)
    )
    (vlax-invoke OpnFil "Close")
    (vlax-release-object OpnFil)
    (vlax-release-object FilObj)
    (vlax-release-object FilSys)
    T
    )
    nil
    )
    )


    Hope this helps some, the code is a lot clearer.
    Note that I too learned a lot from the MENZI ENGINEERING site.



    James Maeding
    Civil Engineer/Programmer
     
    James Maeding, Jan 7, 2004
    #2
  3. kozmos

    kozmos Guest

    Thanx for your reply. By little digging on WSH, i have fixed my problem.

    As for the Opnfil object, there is methods called "WRITE" and "WRITELINE", "READALL" and "READLINE", is there some speed difference between the couple of commands?
     
    kozmos, Jan 8, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.