Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Executing a command if findfile returns NIL

3 REPLIES 3
Reply
Message 1 of 4
The_Caddie
1939 Views, 3 Replies

Executing a command if findfile returns NIL

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?

3 REPLIES 3
Message 2 of 4
balisteor
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.

 

 

Message 3 of 4
Kent1Cooper
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, AIA
Message 4 of 4
hmsilva
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

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost