TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on October 18, 2012, 04:57:11 PM
-
Is there a way to append a string to a file that's not specified as an absolute path?
My support paths are pointing to: c:/cad/lisp/
The code without absolute path:
(setq logfile (open "cadlog.log" "a"))
(princ "new log" logfile)
(close logfile)
(setq logfile (open "c:/cad/lisp/cadlog.log" "a"))
(princ "new log" logfile)
(close logfile)
Thanks
-
If the file resides in a Support Path or the Working Directory, you can use the findfile function to retrieve the full filepath, given the filename.
-
Hi Mac,
(setq logfile (findfile cadlog.log))
(open logfile "a")
(close logfile)
Like this?
error: bad argument type: streamp "C:\\Users\\SNomichith\\Documents\\cadlog.log"
By The Way, your website was very helpful. You are a Guru with Lisp.
-
Hi Mac,
(setq logfile (findfile cadlog.log))
(open logfile "a")
(close logfile)
Like this?
error: bad argument type: streamp "C:\\Users\\SNomichith\\Documents\\cadlog.log"
Not quite - consider instead:
(setq logfile
(open logfile
"a")) ;; Open it for appending )
(write-line "AutoLISP is cool!" logfile
) ;; Write something to it (close logfile
) ;; Close it )
)
By The Way, your website was very helpful. You are a Guru with Lisp.
Thank you dubb, that's very kind of you :-)
-
Thanks! this worked. So there needs to be a conditional statement. One last question, since my lisp file is located in a different directory, why does it default to c:/users/[username]/My Documents/..?
I assumed t it would write to the file that's specified in the support directory.
-
So there needs to be a conditional statement.
The conditional statement is used to verify that the file exists and can be opened for appending before attempting to write to the file. Note that the absence of the conditional expression wasn't the reason that your code was failing - this was because in your code, you are attempting to use the close function on a filepath string, not a file descriptor (as returned by the open function); you were also missing quotation marks around the filename in the findfile expression.
One last question, since my lisp file is located in a different directory, why does it default to c:/users/[username]/My Documents/..?
I assumed it would write to the file that's specified in the support directory.
findfile will always check the working directory before the support paths.
-
Okay I understand now. I greatly appreciate your help Sir. I am now able to make a simple log script thank you again.
-
Okay I understand now. I greatly appreciate your help Sir. I am now able to make a simple log script thank you again.
Excellent - you're welcome dubb :-)
-
FWIW -
Going from memory... This should also work, provided that "C:\\Users\\SNomichith\\" is included in your Support File Search Path (SFSP):
I use this method to simplify our user Profile(s), yet allow access to numerous nested applications, plug-ins, and routines.
HTH
-
You can of course make use OS environment variables too
I don't know whether this is common knowledge, but you can also use "." & ".." to find folders related to your start in folder.
Works slightly differently in BricsCAD to AutoCAD. Wrote about it here http://www.cadconcepts.co.nz/finding-your-start-in-folder-using-lisp/ (http://www.cadconcepts.co.nz/finding-your-start-in-folder-using-lisp/)