Jump to content

Possible to use wildcards or string functions to identify Read-Line source file?


ynotrobits

Recommended Posts

I use Crystal Reports to export a text file which is then read by an AutoCad lisp using read-line.

 

As of now, I've had to manually export the text files which I always export to the same file name.

 

I've recently found a utility that automates the Crystal Report .txt export (which is great) but the problem is that the file names change on each export instance.

 

Ex:

 

1st export:

 

report1-20120316124908.txt

 

2nd export:

 

report1-20120316125527.txt

 

3rd export:

report1-20120316125623.txt

 

 

There will only be one single text file whenever the lisp is run and that file will always begin with the same name but the information after the "-" will be different.

 

So, for the example above, all I would need was to be able to identify a text file that began with "report1"

 

 

Is there any way within AutoCad to identify .txt files by using wildcards or string searches?

 

 

Thanks for looking

 

 

 

AJS

Edited by ynotrobits
Unclear descriptions
Link to comment
Share on other sites

Thanks for the response

 

I feel foolish for not looking at the VL options first. I learned Lisp originally on Intellicad which is Common Lisp and so I don't have a background in using VL.

 

However, that being said, the VL-FileName-mkTemp function works great:

 

_$ (setq Var1 (VL-FileName-mkTemp "report1" "C:\\CREXPORT" ".txt"))

 

Var1 yields: "C:\\CREXPORT\\repor00q.txt"

 

I still have a bit of tinkering to do with this but I'm pretty close to having a great little routine that's going to save a lot of time.

 

Thanks again

Link to comment
Share on other sites

I spoke too soon.

 

I was under the impression that my existing file was copied to the temp file but whenever I try to open it, I get a nil.

 

So what I then tried was using the VL-Directory-Files. When I do, I do get the full file name:

 

_$ (setq Var1 (vl-directory-files "C:\\CREXPORT" "Repor*.txt" 1))

 

Var1 yields:

 

("report1-20120316124908.txt")

 

However, when I try to open the Var1 text file, I get an error:

 

(setq Var2 (open Var1 "r")) ; error: bad argument type: stringp

 

I'm going to keep looking into this and if I get it figured out, I'll post my results

Link to comment
Share on other sites

I can't believe how many mistakes I made on this including forgetting about the directory path when was troubleshooting my text file access problem. However, I did finally get everything to work (The following isn't elegant, I'm sure I can condense it later):

 

_$ (setq Var1 (vl-directory-files "C:\\CREXPORT" "Repor*.txt" 1))

 

_$ (setq Var2 (vl-princ-to-string 'Var1))

 

(setq Var3 (-(strlen Var2)2))

 

(setq Var4(substr Var2 2 Var3))

 

(setq Var5 (Strcat "C:\\CREXPORT\\" Var4))

 

 

Var5 yields: "C:\\CREXPORT\\report1-20120316124908.txt"

 

...which I can successfully open---yeah!

 

 

I'm assuming that one of my problems was that the vl-directory-files function was generating a list which was fixed using princ-to-string.

 

Thanks again, hopefully my floundering will be of use to someone.

Link to comment
Share on other sites

Hint: Read the Documentation.

 

I was under the impression that my existing file was copied to the temp file but whenever I try to open it, I get a nil.

 

vl-filename-mktemp

 

Calculates a unique file name to be used for a temporary file

 

The file name returned by vl-filename-mktemp does not exist; as stated by the documentation, this function merely calculates a file name that is unique for the given parameters.

 

So what I then tried was using the VL-Directory-Files. When I do, I do get the full file name:

 

_$ (setq Var1 (vl-directory-files "C:\\CREXPORT" "Repor*.txt" 1))

 

Var1 yields:

 

("report1-20120316124908.txt")

 

However, when I try to open the Var1 text file, I get an error:

 

(setq Var2 (open Var1 "r")) ; error: bad argument type: stringp

 

Again, read the documentation. The open function requires a file name (string) to open and vl-directory-files returns a list of strings; hence you will need to use one of LISP's list functions to retrieve the string from the list.

 

However, I did finally get everything to work (The following isn't elegant, I'm sure I can condense it later):

 

_$ (setq Var1 (vl-directory-files "C:\\CREXPORT" "Repor*.txt" 1))

 

_$ (setq Var2 (vl-princ-to-string 'Var1))

 

(setq Var3 (-(strlen Var2)2))

 

(setq Var4(substr Var2 2 Var3))

 

(setq Var5 (Strcat "C:\\CREXPORT\\" Var4))

 

Var5 yields: "C:\\CREXPORT\\report1-20120316124908.txt"

 

The name LISP is derived from LISt Processing; and handling lists is what LISP does best.

 

Given that vl-directory-files returns a list, LISP offers a multitude of functions to allow you manipulate that list to your requirements... so you straightaway converted the list to a string...

 

Consider this:

 

(if
   (and
       (setq lst (vl-directory-files "C:\\CREXPORT" "Repor*.txt" 1))
       (setq fds (open (strcat "C:\\CREXPORT" (car lst)) "r"))
   )
   ... read the file ...
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...