Author Topic: Absolute file paths  (Read 2366 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
Absolute file paths
« 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:
Code: [Select]
(setq logfile (open "cadlog.log" "a"))
(princ "new log" logfile)
      (close logfile)

Code: [Select]
(setq logfile (open "c:/cad/lisp/cadlog.log" "a"))
(princ "new log" logfile)
      (close logfile)

Thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Absolute file paths
« Reply #1 on: October 18, 2012, 05:03:59 PM »
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.

dubb

  • Swamp Rat
  • Posts: 1105
Re: Absolute file paths
« Reply #2 on: October 18, 2012, 05:45:54 PM »
Hi Mac,
Code: [Select]
(setq logfile (findfile cadlog.log))
(open logfile "a")
(close logfile)

Like this?
Quote
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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Absolute file paths
« Reply #3 on: October 18, 2012, 05:57:41 PM »
Hi Mac,
Code: [Select]
(setq logfile (findfile cadlog.log))
(open logfile "a")
(close logfile)

Like this?
Quote
error: bad argument type: streamp "C:\\Users\\SNomichith\\Documents\\cadlog.log"

Not quite - consider instead:
Code - Auto/Visual Lisp: [Select]
  1.     (and
  2.         (setq logfile (findfile "cadlog.log")) ;; Find the file
  3.         (setq logfile (open logfile "a")) ;; Open it for appending
  4.     )
  5.     (progn
  6.         (write-line "AutoLISP is cool!" logfile) ;; Write something to it
  7.         (close logfile) ;; Close it
  8.     )
  9. )

By The Way, your website was very helpful. You are a Guru with Lisp.

Thank you dubb, that's very kind of you  :-)

dubb

  • Swamp Rat
  • Posts: 1105
Re: Absolute file paths
« Reply #4 on: October 18, 2012, 06:14:16 PM »
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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Absolute file paths
« Reply #5 on: October 18, 2012, 06:34:40 PM »
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.

dubb

  • Swamp Rat
  • Posts: 1105
Re: Absolute file paths
« Reply #6 on: October 18, 2012, 06:46:52 PM »
Okay I understand now. I greatly appreciate your help Sir. I am now able to make a simple log script thank you again.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Absolute file paths
« Reply #7 on: October 18, 2012, 07:02:37 PM »
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  :-)

BlackBox

  • King Gator
  • Posts: 3770
Re: Absolute file paths
« Reply #8 on: October 18, 2012, 08:22:40 PM »
FWIW -

Going from memory... This should also work, provided that "C:\\Users\\SNomichith\\" is included in your Support File Search Path (SFSP):

Code - Auto/Visual Lisp: [Select]
  1. (findfile "Documents\\cadlog.log")
  2.  

I use this method to simplify our user Profile(s), yet allow access to numerous nested applications, plug-ins, and routines.

HTH
"How we think determines what we do, and what we do determines what we get."

PrinceLISPalot

  • Newt
  • Posts: 36
  • perfectionist trapped inside the mind of an idiot.
Re: Absolute file paths
« Reply #9 on: October 20, 2012, 09:36:32 PM »
You can of course make use OS environment variables too
Code - Auto/Visual Lisp: [Select]
  1. (findfile (strcat (getenv "USERPROFILE") "\\Documents\\cadlog.log"))

I don't know whether this is common knowledge, but you can also use "." & ".." to find folders related to your start in folder.
Code - Auto/Visual Lisp: [Select]
  1. (findfile ".")
Code - Auto/Visual Lisp: [Select]
  1. (findfile "..")

Works slightly differently in BricsCAD to AutoCAD. Wrote about it here http://www.cadconcepts.co.nz/finding-your-start-in-folder-using-lisp/