Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
The_Caddie
Posts: 208
Registered: ‎06-08-2010
Message 1 of 4 (751 Views)

Executing a command if findfile returns NIL

751 Views, 3 Replies
12-17-2012 05:55 AM

Hi,

 

Im having a little trouble of late understanding how to undertake an action/ command if nil

 

what i do know id the following:

 

(setq TESTvar(findfile "acad.lsp"))

(alert TESTvar)

  this gives me the path of the acad.lsp, that is provided it actualy exists, however because the acad.lsp is not compulsary  it may indeed not exist meneaning that the command prompt would return the NIL value instead of the pathname.

 

based upon that I would like to create a new acad.lsp file if not found by autocad.

 

however I cant figure this out?

Mentor
balisteor
Posts: 339
Registered: ‎02-23-2009
Message 2 of 4 (744 Views)

Re: Executing a command if findfile returns NIL

12-17-2012 06:38 AM in reply to: The_Caddie

If that fails then testvar will be nil, as in nothing. so then we can say.

 

 

(setq TESTvar (findfile "acad.lsp"))

(if testvar
(dothis) ; if testvar is anything other than nil (in this case the filepath) (dothis) ; if testvar is nothing. );end if

Like you mentioned you don't want to assume the file always exists, so we use "if"  so that when it does not exist then the error will be avoided. and you can have the program do something else instead of fail.

 

 

*Expert Elite*
Kent1Cooper
Posts: 5,795
Registered: ‎09-13-2004
Message 3 of 4 (721 Views)

Re: Executing a command if findfile returns NIL

12-17-2012 09:31 AM in reply to: The_Caddie

The_Caddie wrote:

... 

what i do know id the following:

 

(setq TESTvar(findfile "acad.lsp"))

(alert TESTvar)

  this gives me the path of the acad.lsp, that is provided it actualy exists, however ... it may indeed not exist meneaning that the command prompt would return the NIL value instead of the pathname.

 

based upon that I would like to create a new acad.lsp file if not found by autocad.

....


If you want to just notify the User one way or the other, and let them handle the creating of the acad.lsp file if needed, here's an arrangement that needs only one (alert) function coded, by nesting the test inside it:

 

(alert

  (if (setq TESTvar (findfile "acad.lsp"))

    TESTvar ; then

    "Did not find acad.lsp file." ; else

  ); if

); alert

 

Or if you don't need TESTvar saved as a variable for any other use, but are using it only for notification purposes, you can do without it:

 

(alert

  (cond

    ((findfile "acad.lsp"))

    ("Did not find acad.lsp file.")

  ); cond

); alert

 

But if you want the code itself to actually do the creating of an acad.lsp file if it doesn't exist, you can do something like this:

 

(if (setq TESTvar (findfile "acad.lsp"))

  (alert TESTvar); then

  (progn ; else

    ( ... do stuff with (open) and (write-line) and )

    (  (close) functions, etc., to create the file ... )

  ); progn

); if

Kent Cooper
*Expert Elite*
hmsilva
Posts: 3,191
Registered: ‎12-17-2004
Message 4 of 4 (693 Views)

Re: Executing a command if findfile returns NIL

12-17-2012 02:47 PM in reply to: The_Caddie

The_Caddie,
just one more approach, if you have a backup of the acad.lsp, or other customization files, in a network directory,
you can use "vl-file-copy" to copy, instead you create a new one.

 

 

(if (findfile "acad.lsp")
      (alert (strcat "acad.lsp exists in:\n" (findfile "acad.lsp")))
  (progn
    (setq mydir (findfile "acad.mnr"))
    (setq mydir (vl-string-right-trim "acad.mnr" mydir))
    (setq myfile "g:\\acad\\backup\\acad.lsp"); your network directory
    (vl-file-copy myfile (strcat dir (vl-string-left-trim "g:\\acad\\backup\\" myfile)))
    (alert (strcat "acad.lsp was copied to:\n" dir (vl-string-left-trim "g:\\acad\\backup" myfile)))
    )
    )

 

hope that helps

 

Henrique

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!