Jump to content

Overwriting - Replacing a File if it exists


rcb007

Recommended Posts

I have the following code that seems to error out when a file already exists.  It is part of an on going lisp.

Do this, then save, do this, then save. etc...

However, When the file name exists, and I try to overwrite the file, it stops the continuation of the routine.

the only way I know how to get it to work, is to delete the existing file before starting the routine.

 

(command "_.saveas" "" (getfiled "Saveas:" (strcat (getvar 'dwgprefix) "FileName") "dwg" 1))

 

Command:
Yes or No, please.
; error: Function cancelled
A drawing with this name already exists.
Do you want to replace it? <N> Y

 

I did lookup the following code, but am unsure how to tie this together.

 

(if (findfile (strcat filename ".dwg"))
(command "_.save" filename "_y");;replaces the existing file 
(command "_.save" filename)(Alert "Did not Save over existing file.") ;;does not replace the existing file
)

 

If there is a better idea, I hope you dont mind to share.

Thank you

Edited by rcb007
Link to comment
Share on other sites

There should be no problem IMHO with vla-xxx functions...

(vla-saveas (vla-get-activedocument (vlax-get-acad-object)) (strcat (getvar 'dwgprefix) (if (getvar 'dwgtitled) (getvar 'dwgname) "Drawingname")))

 

Though untested...

  • Like 1
Link to comment
Share on other sites

If I am overwriting text files I will often delete them first, however if the routine crashes in between the delete and create it all goes wrong.... Keep the delete and create close to each other in the progam

 

 

Modifying your idea above, split the saveas part from the get file name part, then test the filename if it exists, something like this

 

(setq MyFileName (getfiled "Saveas:" (strcat (getvar 'dwgprefix) "FileName") "dwg" 1))
(if (findfile (strcat MyFileNAme ".dwg"))
  (progn ;filename exsits
    (command "_.save" filename "_y")
    (princ (strcat MyFileName " was over written"))
  ) ; end progn
  (progn ; Filename doesn't exist
    (command "_.save" filename)
    (princ "A new file was created.") ;;does not replace the existing file
  ) ; end progn
)

 

  • Like 1
Link to comment
Share on other sites

16 hours ago, ronjonp said:

@rcb007 You could also set EXPERT to 2 or higher before saving to suppress the prompt.

I think you hit it on the head... Seems likes its working as it should. 

Thank you for pointing that out.

 

@Steven P, that is an interesting way. Not bad.

 

  • Like 1
Link to comment
Share on other sites

On 8/22/2022 at 10:39 AM, marko_ribar said:

There should be no problem IMHO with vla-xxx functions...

(vla-saveas (vla-get-activedocument (vlax-get-acad-object)) (strcat (getvar 'dwgprefix) (if (getvar 'dwgtitled) (getvar 'dwgname) "Drawingname")))

 

Though untested...

Tested and worked perfectly.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...