Code Red > AutoLISP (Vanilla / Visual)

VLisp Question

<< < (5/5)

Craig:
Aiight guys, here seems to be what I'll be going with. Now all I need is the (vl-copy-file fn nn) to automatically overwrite, NOT APPEND. Any ideas on what I can use.


--- Code: ---(defun cpCurdwg (/ doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn   (vlax-get-property doc 'FullName)
    dn   (vlax-get-property doc 'Name)
    nn   (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
  )
  )
  (if (findfile nn)
    (alert "File exists.....")
    (if (vla-save fn nn)
      (alert "File copied...")
      (alert "No file copied...")
    )
  )
  (princ)
)
--- End code ---

Craig:
Aiight Mark, I'll give it a try.

Craig:
Thanks Mark, Daron & Columbia for your help. Thanks again Mark. Here is the final

--- Code: ---(defun c:filestore (/ doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn   (vlax-get-property doc 'FullName)
    dn   (vlax-get-property doc 'Name)
    nn   (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
  )
  )
  (vl-file-delete nn)
  (vl-file-copy fn nn)
  (prompt "File Stored... ")
  (princ)
)
--- End code ---


As simple as that

Columbia:
There's one other thing that you may have to watch out for, depending on the speed of your network and PC.

Sometimes, the interfacing with the Windows File System that vl-file-delete does takes longer to occur than VLISP allows for on its own.  Another way to put that is that your code is running too fast.  It goes onto the next step before the file deletion actually takes place.  Therefore you may get an error, or worse, unexpected results.

My suggestion is to wrap the vl-file-delete in a while loop that will keep testing for the deletion (non-existence) of the file before continuing on.  Much like the following:

--- Code: ---
(defun c:filestore (/ doc fn dn nn count)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn      (vlax-get-property doc 'FullName)
    dn      (vlax-get-property doc 'Name)
    nn      (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
    count 0
      )
  )
  (if (findfile nn) ;; if the file already exists  <-- notice the if statement
    (progn
      (vl-file-delete nn)
      (while (and (findfile nn) (< count 100)) ;; <-- here's the loop
        ;; if the file is still to be found, hold on and try again...
        (setq count (1+ count))
      )
    )
  )
  (vl-file-copy fn nn)
  (prompt "File Stored... ")
  (princ)
)
--- End code ---


But that's just my final 2 cents worth...

Navigation

[0] Message Index

[*] Previous page

Go to full version