Author Topic: Help With Lisp to Delete & Rename Files  (Read 3104 times)

0 Members and 1 Guest are viewing this topic.

Vince

  • Newt
  • Posts: 55
Help With Lisp to Delete & Rename Files
« on: March 14, 2013, 12:19:06 PM »
Hi Swamp Members,

I wrote a simple lisp routine to Export a drawing file to a new file name and then delete the original file and finally rename the new file back to the original file name.  The first part of the code works but the File delete and File rename does not.

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;
(DEFUN C:DRFS ()
  (SetVar "CMDECHO" 0)
  (vl-load-com)

;;;;;;
;;;;;;Get File Name
;;;;;;
  (Setq Dnam (GetVar "DWGNAME"))
  (Setq Dpath (GetVar "DWGPREFIX"))

  (Setq OldNam (Strcat Dpath Dnam))

  (Setq Nnam (- (STRLEN Dnam) 4))
  (Setq Fnam (SUBSTR Dnam 1 Nnam))

;;;;;;
;;;;;;Create New File Name
;;;;;;
  (Setq NewNam (STRCAT Dpath Fnam "-new.dwg"))

;;;;;;
;;;;;;Export Clean New File
;;;;;;
  (Command "Export" NewNam "*")

;;;;;;
;;;;;;Close - Rename & Delete  Files
;;;;;;

  (Command "Close" "Y")

  (vl-file-delete OldNam)
 
  (vl-file-rename NewNam OldNam)

  (SetVar "CMDECHO" 1)
)

Can anyone take a look and see what I have done incorrectly and let me know how to resolve the problem to get the code to work properly....??


Your assistance will be appreciated....!!



Regards,
Vince

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Help With Lisp to Delete & Rename Files
« Reply #1 on: March 14, 2013, 12:31:31 PM »
... Export a drawing file to a new file name and then delete the original file and finally rename the new file back to the original file name. ...

What doe this accomplish? It seems like you end up with the same thing?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Vince

  • Newt
  • Posts: 55
Re: Help With Lisp to Delete & Rename Files
« Reply #2 on: March 14, 2013, 12:38:21 PM »
Hi ronjonp,

The original files were received from a consultant and the file size was very large mostly with data that could not be removed or purged.  The Export function seems to reduce the fille size back to what it should be (ex: 10mb file is reduced to a 500kb file) thus making the drawing file much easier to utilize while not taking up a lot of space on the file system.

Everything works except for the vl-File-Delete and vl-File-Rename which I wanted to accomplish automatically so the user would not need to do it.

Any suggestions....??



Thank you,
Vince

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Help With Lisp to Delete & Rename Files
« Reply #3 on: March 14, 2013, 12:57:44 PM »
See if this helps with the file size:

Code: [Select]
(command "._-wblock" (strcat (getvar 'dwgprefix) (getvar 'dwgname)) "*")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Vince

  • Newt
  • Posts: 55
Re: Help With Lisp to Delete & Rename Files
« Reply #4 on: March 14, 2013, 01:24:07 PM »
ronjonp,

Yes it did on some files but there are still a few that the file size did not reduce.  I tried to use the Export option and overwrite the existing file and that seems to work......but I would still like to find out why the File Delete & File Rename did not work....??


Thank you for you response.....!



Regards,
Vince

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Help With Lisp to Delete & Rename Files
« Reply #5 on: March 14, 2013, 01:44:02 PM »
The file rename and delete will not work because you are exiting the drawing first with (Command "Close" "Y").

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Vince

  • Newt
  • Posts: 55
Re: Help With Lisp to Delete & Rename Files
« Reply #6 on: March 14, 2013, 01:47:31 PM »
How would you recommend resolving this.....??



Vince

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Help With Lisp to Delete & Rename Files
« Reply #7 on: March 14, 2013, 01:57:05 PM »
Try something like this. It bypasses creating the temporary drawing. (not sure if it yields the same results though)

Code: [Select]
(defun c:clean (/ exprt)
  (setq exprt (getvar 'expert))
  (setvar 'expert 5)
  (command "Export" (strcat (getvar 'dwgprefix) (getvar 'dwgname)) "*")
  (setvar 'expert exprt)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC