ASCII-UNICODE Text File Reading Issue

ASCII-UNICODE Text File Reading Issue

serag.hassouna
Advocate Advocate
2,845 Views
9 Replies
Message 1 of 10

ASCII-UNICODE Text File Reading Issue

serag.hassouna
Advocate
Advocate

I'm trying to read a file that's written in normal ASCII characters and saved in Unicode format using the FileSystemObject leaning on the following methods

  1. OpenTextFile Method
  2. ReadAll Method

The code is simply like this

(defun read-unicode->ascii ()
  (setq path "C:\\UserProfile\\somefolder\\test.txt") ;supply a real path instead 
  ;Create the FileSystemObject
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq fobj (vlax-invoke-method 'OpenTextFile path 1 :vlax-true 0))
  (setq txt (vlax-invoke-method fobj "ReadAll"))
(vlax-invoke-method fobj "Close") (mapcar 'vlax-object-release (list fso fobj)) (setq fso nil fobj nil) txt ;what to return );end-defun [read-unicode->ascii]

The issue is that 

(vlax-invoke-method fobj "ReadAll")

Throws the error 

; error: Automation Error. Description was not provided.

______________________
I've tried the methods that're responsible for writing text files, and they work fine.

______________________

P.S. The Autolisp function read-line returns the unicode equivalent result, but not the actual text that's written in normal ASCII, That's why I've decided to give the "FileSystemObject" a try.
______________
Any help and insightful responses are appreciated.

0 Likes
Accepted solutions (2)
2,846 Views
9 Replies
Replies (9)
Message 2 of 10

dlanorh
Advisor
Advisor

At a quick glance, you are not using the fso.

 

This invoke method has no object

(setq fobj (vlax-invoke-method 'OpenTextFile path 1 :vlax-true 0))

 

perhaps it should be

(setq fobj (vlax-invoke-method fso 'OpenTextFile path 1 :vlax-true 0))

I am not one of the robots you're looking for

0 Likes
Message 3 of 10

dlanorh
Advisor
Advisor

This is also incorrect -

(mapcar 'vlax-object-release (list fso fobj))

 

It should be

(mapcar 'vlax-release-object (list fso fobj))

I am not one of the robots you're looking for

0 Likes
Message 4 of 10

serag.hassouna
Advocate
Advocate

You're right @dlanorh, To be honest, this code is meant to be an illustrative sample, but anyway here's a modified one

(defun read-unicode->ascii ()
  (setq path "C:\\UserProfile\\somefolder\\test.txt") ;supply a real path instead 
  ;Create the FileSystemObject
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq fobj (vlax-invoke-method fso 'OpenTextFile path 1 :vlax-true 0))
  (setq txt (vlax-invoke-method fobj "ReadAll"))
  (vlax-invoke-method fobj "Close")
  (mapcar 'vlax-release-object (list fso fobj))
  (setq fso nil fobj nil)
  txt ;what to return
);end-defun [read-unicode->ascii]

What about the reading problem with these methods {ReadAll ,ReadLine, Read, Skip, SkipLine} ?

0 Likes
Message 5 of 10

dlanorh
Advisor
Advisor

It's years since i used a file scripting object to read/write a file, and i'm not in a position to test code at the moment. I vaguely remember something about opening files for reading using the "OpenAsTextStream" Method if you want to use "ReadAll" https://docs.microsoft.com/en-us/previous-versions//hwfw5c59%28v%3dvs.85%29

I am not one of the robots you're looking for

0 Likes
Message 6 of 10

serag.hassouna
Advocate
Advocate
(setq txtfile (vlax-invoke-method fso 'GetFile "C:\\UserProfile\\somefolder\\test.txt"))
(setq fobj (vlax-invoke-method txtfile 'OpenAsTextStream 1 0))

The output type is the same, a "TextStream Object" like this

 #<VLA-OBJECT ITextStream 000002a1e5376490>

However, the same error arises when invoking any reading method on the "TextStream Object"

; error: Automation Error. Description was not provided. 

 

0 Likes
Message 7 of 10

dlanorh
Advisor
Advisor
Accepted solution

This works for me in Win7 using AutoCAD2012

 

(defun c:read ( / path fso txtfile fobj txt)
  (setq path "C:\\temp\\test.txt") ;a small text file on my system
  ;Create the FileSystemObject
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  ;Get the File Object
  (setq txtfile (vlax-invoke-method fso 'GetFile path))
  ;Open File Object as TextStream
  (setq fobj (vlax-invoke-method txtfile 'OpenAsTextStream 1 0));0 = read as ascii (use -1 for unicode)
  ;Read from the textstream
  (setq txt (vlax-invoke-method fobj "ReadAll"))
  (vlax-invoke-method fobj "Close")
  (mapcar 'vlax-release-object (list fso fobj txtfile))
  (setq fso nil fobj nil txtfile nil)
  txt ;what to return
);end-defun [read-unicode->ascii]

 

Run in the VLIDE value of txt is contents of  *.txt file

I am not one of the robots you're looking for

Message 8 of 10

serag.hassouna
Advocate
Advocate

I'm working on Windows 10 with AutoCAD 2016.
The Good news, it works fine if the file exists before running the lisp file.

However, with some added lines to echo a text string to a new file, "FileSystemObject" doesn't recognize its creation!, returns :vlax-false when invoking "FileExist" Method, and returns the same error message.

(defun c:read ( / path cmd fso txtfile fobj txt)
  (setq path "C:\\temp\\test.txt") ;a file to be created, or an already existing file
  (setq cmd (strcat "echo Hello World > " path))
  (command "Shell" cmd); echos "Hello World" to the text file
  ;Create the FileSystemObject
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  ;Get the File Object
(princ "File Existence State: ") (princ (vlax-invoke-method fso "FileExists" path)) (princ "\n") (setq txtfile (vlax-invoke-method fso 'GetFile path)) ;Open File Object as TextStream (setq fobj (vlax-invoke-method txtfile 'OpenAsTextStream 1 0));0 = read as ascii (use -1 for unicode) ;Read from the textstream (setq txt (vlax-invoke-method fobj "ReadAll")) (vlax-invoke-method fobj "Close") (mapcar 'vlax-release-object (list fso fobj txtfile)) (setq fso nil fobj nil txtfile nil) txt ;what to return );end-defun [c:read]
0 Likes
Message 9 of 10

dlanorh
Advisor
Advisor
Accepted solution
If you cut and paste these one by one onto the command line in Autocad, it returns :vlax-true.

When I pasted (command "Shell" cmd) onto the command line and pressed return there was a short delay and a brief "command shell" window flicker

This makes me think that this is an automation problem where you are creating the fso and checking if the files exists before the system has created the file. Try inserting a (vl-cmdf "_delay" 1000) [1 second wait] between the (command "shell" cmd) and creating the fso. You may have to play around with the length of the delay (integer in milliseconds) until you find an exceptable balance.

I am not one of the robots you're looking for

0 Likes
Message 10 of 10

serag.hassouna
Advocate
Advocate

Thanks a lot, this really did much help, it works with me from about 200 milliseconds.
The same "command shell" window flicker happens also with me when (command "Shell" cmd) is invoked.