running a script through a batch file

running a script through a batch file

Michiel.Valcke
Advisor Advisor
6,844 Views
11 Replies
Message 1 of 12

running a script through a batch file

Michiel.Valcke
Advisor
Advisor

I'm trying to set up a workflow where I can run several cleanup scripts on a collection of files.

I started with a simple purge script (which works when loaded manually) and the following batch file from Michael Best's AU class:

FOR %%f IN ("%~dp0*.dwg") DO "C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe" /i"%%f" /s "%~dp0PurgeAll.scr" /l en-US

-PURGE
a
*
N

However, when running the batch file everything seems to work (I get the loading MSDOS window and there are no errors and I see the expected output. But when opening the .dwg's afterwards in acad.exe the actual purging operation does not seem to have been saved to the .dwg's. They still contain unused blocks and layers (while when following the MSDOS progress I could see the notification that they were purged)

So I tried to alter the PurgeAll.scr by adding a QSAVE at the end, but somehow that wants to save the drawing as drawing1.dwg, which halts the batch file as soon as the second drawing is processed. Playing with SAVEAS -SAVE or others have also all halted the batch process.

Has anybody else encountered this problem and how have you solved it?

0 Likes
Accepted solutions (2)
6,845 Views
11 Replies
Replies (11)
Message 2 of 12

maratovich
Advisor
Advisor

Maybe this will help you - AutoRunLisp 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
Message 3 of 12

CADaSchtroumpf
Advisor
Advisor
Accepted solution

Hi,
For me this type of procedure to purge drawings from an entire folder has always worked well.
This purges specific application dictionaries, here for example objects from the COVADIS application (French application) or/and from the Autocad Archictectural application.
This can simply be commented out with a ; at the beginning of the line of the code or adapted according to your needs
He checks the drawing
Purges unused applications for the first time
Purge a 2nd time for all objects
And saves in LT2018 format

All these commands being customizable, the only trick is to remember to double the " with \" between (write-line " ...... "file_scr) compared to a natural lisp writing.

The code can be copied and pasted directly on the command line, preferably from a blank drawing

After choosing a folder, it will automatically create the script (update.scr) and the batch (update.bat) and AcCoreconsole will start its processing.

((lambda ( / f_exe ShlObj Folder FldObj Out_Fld file_scr file_bat)
	(vl-load-com)
	(setq f_exe (findfile "accoreconsole.exe"))
	(setq
		ShlObj (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application")
		Folder (vlax-invoke-method ShlObj 'BrowseForFolder 0 "" 0)
	)
	(vlax-release-object ShlObj)
	(if Folder
		(progn
			(setq
				FldObj (vlax-get-property Folder 'Self)
				Out_Fld (vlax-get-property FldObj 'Path)
			)
			(vlax-release-object Folder)
			(vlax-release-object FldObj)
		)
	)
	(setq file_scr (open (strcat (getvar "ROAMABLEROOTPREFIX") "support\\update.scr") "w"))
	(write-line "((lambda ( / ) (foreach p (entget (namedobjdict)) (if (and (= 3 (car p)) (or (wcmatch (cdr p) \"COVA_*\") (wcmatch (cdr p) \"AEC_*\"))) (dictremove (namedobjdict) (cdr p))))))" file_scr)
	(write-line "(command \"_.audit\" \"_Yes\")" file_scr)
	(write-line "(command \"_.-purge\" \"_Regapps\" \"*\" \"_No\")" file_scr)
	(write-line "(command \"_.-purge\" \"_All\" \"*\" \"_No\")" file_scr)
	(write-line "(command \"_.saveas\" \"_LT2018\" \"\" \"_Yes\")" file_scr)
	(close file_scr)
	(setq file_bat (open (strcat (getvar "ROAMABLEROOTPREFIX") "support\\update.bat") "w"))
	(write-line (eval (read "(strcat \"set accoreexe=\" \"\\\"\" f_exe \"\\\"\")")) file_bat)
	(write-line (eval (read "(strcat \"set source=\" \"\\\"\" Out_Fld \"\\\"\")")) file_bat)
	(write-line (eval (read "(strcat \"set script=\" \"\\\"\" (getvar \"ROAMABLEROOTPREFIX\") \"support\\\\update.scr\" \"\\\"\")")) file_bat)
	(write-line "FOR /f \"delims=\" %%f IN ('dir /b \"%source%\\\*.dwg\"') DO %accoreexe% /i \"%source%\\%%f\" /s %script%" file_bat)
	(close file_bat)
	(startapp (strcat (getvar "ROAMABLEROOTPREFIX") "support\\update.bat"))
	(princ"\Job finished")
	(prin1)
))
Message 4 of 12

Michiel.Valcke
Advisor
Advisor

I like your suggestions, I will definitely look into the lisp file to create and run a batch file.

But in essence this is the same as what I'm doing,
and in my earlier attempts to run it, the coreconsole saves drawings as drawing1.dwg, which halts the routine.

I'll try the lisp approach and see how that changes the behaviour.

0 Likes
Message 5 of 12

Michiel.Valcke
Advisor
Advisor

Unfortunately the above code somehow doesn't start the batch file to run with me, and if I manually activate it, it also refused to run.

I recovered the update.scr and ran it with my batch file and again the saveas command keeps saving all the drawings as drawing1.dwg. It doesn't halt because of how it is written, but at the end none of the .dwg's in my folder are updated and I end up with a single drawing1 in the same folder that is a cleaned up / purged version of the last .dwg that the batch file ran on.

In the below code you can see the MS DOS readout, 
- the batch file opens a .dwg
- executes the audit and purge succesfully
- saves the dwg as drawing1.dwg (overwriting the previous drawing1.dwg)

This is the content of the script file (different than the one from the first post)

(command "_.audit" "_Yes")
(command "_.-purge" "_Regapps" "*" "_No")
(command "_.-purge" "_All" "*" "_No")
(command "_.saveas" "_LT2018" "" "_Yes")



 

Command:
C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (2) - Copy>"C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe" /i"C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (2) - Copy\40.dwg" /s "C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (2) - Copy\update.scr" /l en-US
Redirect stdout (file: C:\Users\MICHIE~1.GEO\AppData\Local\Temp\accc108682).
AcCoreConsole: StdOutConsoleMode: processed-output: enabled,auto
AutoCAD Core Engine Console - Copyright 2021 Autodesk, Inc.  All rights reserved. (S.154.0.0)

Execution Path:
C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe
Current Directory: C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (2) - Copy

Version Number: S.154.0.0 (UNICODE)
LogFilePath has been set to the working folder.
Regenerating model.
.
**** System Variable Changed ****
1 of the monitored system variables has changed from the preferred value. Use SYSVARMONITOR command to view changes1 of the monitored system variables has changed from the preferred value. Use SYSVARMONITOR command to view changes.


AutoCAD menu utilities loaded.
Command:
Command:

Command:
Command: (command "_.audit" "_Yes")
_.audit
Fix any errors detected? [Yes/No] <N>: _Yes


Auditing Header


Auditing Tables


Auditing Entities Pass 1

Pass 1 100     objects audited
Auditing Entities Pass 2

Pass 2 100     objects audited
Auditing Blocks

 1       Blocks audited



Auditing AcDsRecords


Total errors found 0 fixed 0

Erased 0 objects


Command: nil

Command: (command "_.-purge" "_Regapps" "*" "_No")
_.-purge
Nested items = Off    Orphaned data = Off
Enter type of unused objects to purge [Blocks/DEtailviewstyles/Dimstyles/Groups/LAyers/LTypes/MAterials/MUltileaderstyles/Plotstyles/SHapes/textSTyles/Mlinestyles/SEctionviewstyles/Tablestyles/Visualstyles/Regapps/Zero-length geometry/Empty text objects/Orphaned data/All]: _Regapps Enter name(s) to purge <*>: * Verify each name to be purged? [Enter type of unused objects to purge [Blocks/DEtailviewstyles/Dimstyles/Groups/LAyers/LTypes/MAterials/MUltileaderstyles/Plotstyles/SHapes/textSTyles/Mlinestyles/SEctionviewstyles/Tablestyles/Visualstyles/Regapps/Zero-length geometry/Empty text objects/Orphaned data/All]: _Regapps Enter name(s) to purge <*>: * Verify each name to be purged? [Yes/No] <Y>: _No Deleting registered application "ACAD_MLEADERVER".
Deleting registered application "AcadAnnoPO".
2 registered applications deleted.

Command: nil

Command: (command "_.-purge" "_All" "*" "_No")
_.-purge
Nested items = Off    Orphaned data = Off
Enter type of unused objects to purge [Blocks/DEtailviewstyles/Dimstyles/Groups/LAyers/LTypes/MAterials/MUltileaderstyles/Plotstyles/SHapes/textSTyles/Mlinestyles/SEctionviewstyles/Tablestyles/Visualstyles/Regapps/Zero-length geometry/Empty text objects/Orphaned data/All]: _All Enter name(s) to purge <*>: * Verify each name to be purged? [Yes/Enter type of unused objects to purge [Blocks/DEtailviewstyles/Dimstyles/Groups/LAyers/LTypes/MAterials/MUltileaderstyles/Plotstyles/SHapes/textSTyles/Mlinestyles/SEctionviewstyles/Tablestyles/Visualstyles/Regapps/Zero-length geometry/Empty text objects/Orphaned data/All]: _All Enter name(s) to purge <*>: * Verify each name to be purged? [Yes/No] <Y>: _No
No unreferenced blocks found.

No unreferenced layers found.

No unreferenced linetypes found.
Deleting text style "Annotative".
1 text style deleted.

No unreferenced shape files found.
Deleting dimension style "Annotative".
Deleting dimension style "Standard".
2 dimension styles deleted.

No unreferenced mlinestyles found.

No unreferenced plotstyles found.

No unreferenced table styles found.

No unreferenced materials found.

No unreferenced visual styles found.
Deleting multileader style "Annotative".
1 multileader style deleted.

No unreferenced groups found.

No unreferenced Detail view styles found.

No unreferenced Section view styles found.

Command: nil

Command: (command "_.saveas" "_LT2018" "" "_Yes")
_.saveas
Current file format: AutoCAD 2018 Drawing
Enter file format [R14(LT98&LT97)/2000(LT2000)/2004(LT2004)/2007(LT2007)/2010(LT2010)/2013(LT2013)/2018(LT2018)/Standards/DXF/Template] <2018>: _LT2018 Save drawing as <C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (Enter file format [R14(LT98&LT97)/2000(LT2000)/2004(LT2004)/2007(LT2007)/2010(LT2010)/2013(LT2013)/2018(LT2018)/Standards/DXF/Template] <2018>: _LT2018 Save drawing as <C:\Users\michiel.geo-it\Documents\Werkbestanden\test - Copy (2) - Copy\Drawing1.dwg>: A drawing with this name already exists.
Do you want to replace it? <N> _Yes
Command: nil

 

0 Likes
Message 6 of 12

Michiel.Valcke
Advisor
Advisor

I'm beginning to suspect the error is produced by how the accoreconsole implements the save command, and that this might be unavoidable, but then I don't see how anybody else can get their accoreconsole batch processes to save the files in question.

0 Likes
Message 7 of 12

CADaSchtroumpf
Advisor
Advisor

One thing that intrigues me in your return messages is the line:
AutoCAD menu utilities loaded.
For my part I don't have this kind of feedback in my AcCoreConsole version: Version Number: P.46.0.0 (UNICODE)
I don't understand why AutoCAD loads menus while AcCoreConsole doesn't use GUI.

The big disadvantage of the console is that in case of error, there is no error message and the script is not interrupted.
Another thing I noticed is that AcCoreConsole does not recognize lisp vla and vlax instructions as well as vertical product commands: for me for example an AutocadMap command like MAPINSERT (or others) is not recognized and this no return message, not easy to debug 😓

0 Likes
Message 8 of 12

Michiel.Valcke
Advisor
Advisor

@CADaSchtroumpf 

 

You are right it is a * in the * to debug this one, but I haven't given up on it yet. Since I know it should work. It might take some time but if I do find how to make it work I will let you all know.

0 Likes
Message 9 of 12

Michiel.Valcke
Advisor
Advisor
Accepted solution

I didn't find a real solution, but the issue apparently was generated by the way the batch file was formulated.

As long as the batch file was self-referencing to its own location for selecting the .dwg's that need to be treated, the accoreconsole generated the above error.

So instead of 

FOR %%f IN ("%~dp0*.dwg") DO "C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe" /i"%%f" /s "%~dp0PurgeAll.scr" /l en-US

 

I used the following (based on @CADaSchtroumpf  's lisp routine):

 

set accoreexe="C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe"
set source="C:\Users\xxx\test"
set script="C:\Users\xxx\test\update.scr"
FOR /f "delims=" %%f IN ('dir /b "%source%\*.dwg"') DO %accoreexe% /i %source%\%%f /s %script%

 

I still have no explanation for this, but this has solved it for me.

0 Likes
Message 10 of 12

oiverson
Enthusiast
Enthusiast

Late to the game, but if PURGE is all you want from a set of files, you can use the "DWG Convert" tool found off the App Menu:

oiverson_0-1673460836484.png

 

Click the "Conversion Setups" button:

oiverson_1-1673460873846.png

Click "New":

oiverson_2-1673460899614.png

Give it a name like "PURGE":

oiverson_3-1673460927121.png

I use "In-place (overwrite)" and then just tick the box "Purge Drawings" over on the right (could also check/fix errors):

oiverson_4-1673460996741.png

Now, you can just drag your files into the main window, select the new conversion PURGE and hit "Convert"

oiverson_5-1673461072260.png

You can also save the list for future reference and all that.

 

I also use this to swap out old Page-Setups with my current setups (sadly, you can't set a setup current using this tool)

 

Hope this helps someone!

 

Message 11 of 12

Michiel.Valcke
Advisor
Advisor

I intend to try and do a lot more things than purge, but that is a cool trick that I didn't know about, kudos.

0 Likes
Message 12 of 12

john.kaulB9QW2
Advocate
Advocate

Glad you may have gotten your problem solved but from looking at the short BATCH SCRIPT example you gave (you got from Michael possibly) your features/functionality is a bit limited -e.g. this will run the script on every drawing in the directory meaning that you'd have to copy that .bat file to every directory, or modify it for different scripts, or, etc, etc. (a whole slew of reasons).

 

I created a better script/workflow that supports either drag-and-drop or can be called with a list of files or even called via a simple registry entry (aka right click).  I needed a similar thing a few years back but I needed to run the script on a few hundred drawings out of a thousand, so I created my batch methodology a bit different. Also, I added instructions on how to run my .bat file with Briscad if that's a need as well.

 

Do a search for "batch drawing processing" on theswamp.org's show your stuff forum for a "ready to use" version. 

another swamper