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
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 )