ynotrobits Posted March 16, 2012 Share Posted March 16, 2012 (edited) 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 March 16, 2012 by ynotrobits Unclear descriptions Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 16, 2012 Share Posted March 16, 2012 Study the VLIDE Help Documentation of the vl-directory-files function, specifically the 'pattern' argument. Quote Link to comment Share on other sites More sharing options...
ynotrobits Posted March 17, 2012 Author Share Posted March 17, 2012 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 Quote Link to comment Share on other sites More sharing options...
ynotrobits Posted March 17, 2012 Author Share Posted March 17, 2012 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 Quote Link to comment Share on other sites More sharing options...
ynotrobits Posted March 17, 2012 Author Share Posted March 17, 2012 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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 17, 2012 Share Posted March 17, 2012 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 ... ) Quote Link to comment Share on other sites More sharing options...
ynotrobits Posted March 17, 2012 Author Share Posted March 17, 2012 Thanks for the follow up and advice..... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.