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

Deleting a script file in lisp from AutoCAD

20 REPLIES 20
Reply
Message 1 of 21
cassidy_ng
3548 Views, 20 Replies

Deleting a script file in lisp from AutoCAD

I have a lisp routine that creates a script file and then it runs the script file. After the script has finished running, I want to delete the script file from autocad, but for some reason it doesn't delete.

I've tried using (vl-file-delete path)

Any suggestions?
20 REPLIES 20
Message 2 of 21
devitg
in reply to: cassidy_ng

Could you show the lisp , please ?
Message 3 of 21
cassidy_ng
in reply to: cassidy_ng

I've attached the lisp routine.
Message 4 of 21
cassidy_ng
in reply to: cassidy_ng

(defun c:BATCH (/ dwgs file dwgName scrFile folderName)
(vl-load-com)
(setq folderName
(browsefolder "Select folder to perform batch job: ")
)
(setq dwgs (vl-directory-files folderName "*.dwg"))
(setq scrFile (open (strcat folderName "\\batchJob.scr") "w"))
(foreach file dwgs
(setq dwgName (strcat "\"" folderName "\\" file "\""))
(write-line ".Open" scrFile)
(write-line dwgName scrFile)
(write-line ".zoom e" scrFile)
(write-line ".close" scrFile)
)
(close scrFile)
(command ".script" (strcat folderName "\\batchJob.scr"))
(vl-file-delete folderName "\\batchJob.scr")
(princ)
)
;;; Function to browse folder
(defun browsefolder (title / shlobj folder fldobj)
(vl-load-com)
(setq
shlobj (vla-getinterfaceobject
(vlax-get-acad-object)
"Shell.Application"
)
folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0)
)
(vlax-release-object shlobj)
(if folder
(progn
(setq fldobj (vlax-get-property folder 'self)
folderName (vlax-get-property fldobj 'path)
)
(vlax-release-object folder)
(vlax-release-object fldobj)
folderName
)
)
)
Message 5 of 21
Kent1Cooper
in reply to: cassidy_ng

Shouldn't this:

(vl-file-delete folderName "\\batchJob.scr")

be this instead?

(vl-file-delete (strcat folderName "\\batchJob.scr"))

--
Kent Cooper


cassidy_ng wrote...
....
(setq folderName
(browsefolder "Select folder to perform batch job: ")
)
(setq dwgs (vl-directory-files folderName "*.dwg"))
(setq scrFile (open (strcat folderName "\\batchJob.scr") "w"))
(foreach file dwgs
(setq dwgName (strcat "\"" folderName "\\" file "\""))
(write-line ".Open" scrFile)
(write-line dwgName scrFile)
(write-line ".zoom e" scrFile)
(write-line ".close" scrFile)
)
(close scrFile)
(command ".script" (strcat folderName "\\batchJob.scr"))
(vl-file-delete folderName "\\batchJob.scr")
....
Kent Cooper, AIA
Message 6 of 21
cassidy_ng
in reply to: Kent1Cooper

I tried it and it still doesn't work.  The variable is already passed as a string in the beginning.

Message 7 of 21
scamaru
in reply to: Kent1Cooper

Hi Kent,

I tried it and it still doesn't work for me too.

There is another solution?


Thanks in advance.

Message 8 of 21
Kent1Cooper
in reply to: scamaru


@antistar wrote:

....

I tried it and it still doesn't work for me too.

....


I can't imagine why it would not work, though I haven't loaded it up and tried it.  If (strcat folderName "\\batchJob.scr") works in the (open) function to start making the Script, and in the (command) function to run the Script, then it must be a valid file designator, and should work in (vl-file-delete) to remove it, too.  I suggest you carefully comb through the code to make sure all function and file and variable names are spelled exactly the same way, etc., but I have no other ideas.  Does "it doesn't work" simply mean that the file is not deleted, or is there more to it, e.g. any error message?

Kent Cooper, AIA
Message 9 of 21
scamaru
in reply to: Kent1Cooper

Hi Kent, thanks for reply.

The routine works, but the file is not deleted and not show any error message.

Would not it be something related to the format of the "folderName" variable?

Message 10 of 21
mid-awe
in reply to: scamaru

Check your file permissions. It could be that the file is created without read/write/create/delete permissions. Find the file on your HDD select, right-click, properties near the bottom of the dialog you should see read only unchecked and not greyed out. Also, it may be that you'll have to delete it in a seperate function after releasing the file from the original.

Message 11 of 21

>(command ".script" (strcat folderName "\\batchJob.scr"))
>(vl-file-delete folderName "\\batchJob.scr")

 

Try to put:

(command ".script" (strcat folderName "\\batchJob.scr"))

(alert "End Script")

 

and you never see "End Script", I think that anything made after the launch of the script will not be executed.

Marc'Antonio Alessi

http://alessi.xoom.it//alessi/Programmi.htm
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
2D Parametric for 2000-2013
Message 12 of 21
mid-awe
in reply to: cassidy_ng

Try using (vl-filename-mktemp "batchJob.scr")

It's working for me.

Message 13 of 21
scamaru
in reply to: mid-awe

 mid-awe , thanks for reply.

How changed the routine to work for you?
Message 14 of 21

The command vl-filename-mktemp doesn't work.  After running the script batchJob.scr, the file is still there.  I am beginning to wonder if AutoCAD doesn't continue running the lsp file after running the script file.

 

Message 15 of 21
mid-awe
in reply to: scamaru

This is what I did, but there may be other ways.

(setq	*fname*	 (vl-filename-mktemp "batchJob.scr")
        scrFile (open (strcat folderName "\\" *fname*) "w")
)

 And to remove the file:

(vl-file-delete *fname*)

 

Message 16 of 21

u will have to use VBA... for lisp you lose focus.

if you do not want to use VBA  then you can create a variable, put it on the blackboard (vl-bb-set

then in your mnl or acaddoc.lsp file check for this variable and accordingly do whatever u need.

HTH

Message 17 of 21

Maybe you can delete it while is running, try to delete a last step in the script, but I dubt...

 

Sorry for my english.

Marc'Antonio Alessi

http://alessi.xoom.it//alessi/Programmi.htm
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
2D Parametric for 2000-2013
Message 18 of 21

Matc'A ... good idee, add the following line just before (close scrFile)

 

  (write-line "(vl-file-delete (strcat folderName (chr 34) \\batchJob.scr  (chr 34)  ))" scrfile)

 

and givi it a shot

 

m
 

Message 19 of 21
mid-awe
in reply to: cassidy_ng

From the time that my file finished running I had to include a short delay before deleting the file.

(command "delay" "3000")

 Beyond that I don't know why mine is working and you are still looking for a solution. Sorry I could not be of more help.

Message 20 of 21
pbejse
in reply to: AlessiMarc'Antonio


@AlessiMarc'Antonio wrote:

>(command ".script" (strcat folderName "\\batchJob.scr"))
>(vl-file-delete folderName "\\batchJob.scr")

 

Try to put:

(command ".script" (strcat folderName "\\batchJob.scr"))

(alert "End Script")

 

and you never see "End Script", I think that anything made after the launch of the script will not be executed.


I agree with this "debugging" approach. Most likely the script prematurely ended and never reach the line to delete the file. If a command is cancelled for some reasons, and the script will end

 

So check your script  cassidy_ng

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost