Author Topic: Batch operations on multiple files  (Read 10404 times)

0 Members and 2 Guests are viewing this topic.

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #15 on: September 25, 2020, 09:45:41 AM »
Thanks, that works great.
One other thing, can you help with filtering .dwg files only?

Thanks,
Chris
« Last Edit: September 25, 2020, 10:03:09 AM by framednlv »

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: Batch operations on multiple files
« Reply #16 on: September 25, 2020, 11:14:03 AM »
It would probably be better to let the program crash for those that try to launch this on a file other than a DWG but I'll play along (-i.e. only drag and drop only DWGs).

Two minor changes:
1. Added a filter on the argument counter.
2. Added a simple if then statement in the recursive function.

Code - Bash: [Select]
  1. @echo off
  2. :: process files given as argument to this batch script.
  3. ::
  4. :: Last Updated: 09.25.20 10:08:55 AM
  5. ::
  6. :: BY: John Kaul
  7.  
  8. set script="%~dp0\BatchDrawings_accoreconsole.scr"
  9. set script=%script:\\=\%
  10. :: - Create an argument counter for prompting.
  11. set argc=0
  12. set count=1
  13. :: - Find out how many arguments we have.
  14. for %%x in ("%*.dwg") do set /A argc+=1
  15.  
  16. :processArgs
  17.         if [%1]==[] goto endmark
  18.         if NOT [%~x1]==[.dwg] goto endmark
  19.         title "%~f1"
  20.         call "C:\Program Files\Autodesk\AutoCAD 2021\accoreconsole.exe" /i "%~f1" /s %script% /l en-US >"C:\temp\cleanup.log"
  21.         echo Processing [%count%/%argc%]: %~f1
  22.         set /A count +=1
  23.         if errorlevel 1 goto errormark
  24.         SHIFT
  25.         goto processArgs
  26. :errormark
  27.         echo.
  28.         echo.
  29.         echo ERROR processing %~f1
  30.         pause
  31.         echo.
  32. :endmark
  33. rem
  34. rem     finished
  35. rem
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #17 on: September 26, 2020, 02:02:05 PM »
This seems to work:
Code: [Select]
if /I [%~x1]==[.DWG] (echo "[%count%/%argc%]: Processing... %~f1" ) else (echo "[%count%/%argc%] skipping %~f1" )
if /I [%~x1]==[.DWG] (call "C:\Program Files\Autodesk\AutoCAD 2021\accoreconsole.exe" /i "%~f1" /s %script% /l en-US >"C:\temp\cleanup.log")

There is one problem I found.  If any of the file names or folders have the '&' character it doesn't work.

 
« Last Edit: September 28, 2020, 02:44:04 PM by framednlv »

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #18 on: September 29, 2020, 09:57:58 AM »
I was able to put together a VBS script that does the same as the previously posted bat file.  This will allow you to drag and drop files or a folder to create the bat file, and will run it.  It will skip files that are not dwg files and handles the file names better than the previously posted one.

select and save as a .vbs file:
Code - Visual Basic: [Select]
  1. Set objArgs = Wscript.Arguments
  2. Set objFso = createobject("scripting.filesystemobject")
  3. set oShell = CreateObject("Shell.Application")
  4. strFile_Path = "C:\temp\Cleanup.bat"
  5. accoreconsole = "C:\Program Files\Autodesk\AutoCAD 2021\accoreconsole.exe"      'Put your current autocad path here
  6. scriptFile = "x:\Lisp\BatchCleanup\BatchCleanup.scr"    'Put path to script file here
  7.  
  8. dim filesys, newfolder
  9. set filesys=CreateObject("Scripting.FileSystemObject")
  10. If Not filesys.FolderExists("C:\Temp\") Then
  11.         newfolder = filesys.CreateFolder ("C:\Temp\")
  12. End If
  13.  
  14.  
  15. Set filesys = CreateObject("Scripting.FileSystemObject")
  16. filesys.CreateTextFile("C:\temp\Cleanup.bat"), True
  17. If filesys.FileExists("C:\temp\Cleanup.bat") Then
  18.         filesys.DeleteFile "C:\temp\Cleanup.bat"
  19. End If  
  20.  
  21. 'iterate through all the arguments passed
  22. For i = 0 to objArgs.count
  23.         on error resume next
  24.        
  25. 'try and treat the argument like a folder
  26.         Set folder = objFso.GetFolder(objArgs(i))
  27.        
  28. 'if we get an error, we know it is a file
  29.         If err.number <> 0 then
  30. 'this is not a folder, treat as file
  31.                 ProcessFile(objArgs(i))
  32.         Else
  33.                
  34. 'No error? This is a folder, process accordingly
  35.                 For Each file In folder.Files
  36.                         ProcessFile(file.path)
  37.                 Next
  38.         End if
  39.         On Error Goto 0
  40. Next
  41.  
  42. Function ProcessFile(sFilePath)
  43.         Const ForReading = 1, ForWriting = 2, ForAppending = 8
  44.         Dim fso, f
  45.         Set fso = CreateObject("Scripting.FileSystemObject")
  46.         Set f = fso.OpenTextFile(strFile_Path, ForAppending, True)
  47.         f.write "@echo off" & vbCrLf
  48.         f.write "Title Please wait while I clean up here..." & vbCrLf
  49.         if UCASE(Mid(sFilePath,len(sFilePath)-3,4)) = ".DWG" then
  50.                 f.write "@echo [" & i+1 & "/" & objArgs.count & "]: Processing... " & sFilePath & vbCrLf
  51.                 f.write Chr(34) & accoreconsole & Chr(34) & " /i " & Chr(34) & sFilePath & Chr(34) & " /s " & Chr(34) & scriptFile & Chr(34) & " " & ">C:\temp\cleanup.log" & vbCrLf
  52.                 f.Close
  53.         Else
  54.                 f.write "@echo off" & vbCrLf
  55.                 f.write "@echo [" & i+1 & "/" & objArgs.count & "]: Skipping... " & sFilePath & vbCrLf
  56.                 f.Close
  57.         End If
  58. End Function
  59.  
  60. Dim shell
  61. Set shell = CreateObject("WScript.Shell")
  62. shell.Run strFile_Path
  63.  
  64.  

A side note, I'm not a programmer so the code might not be perfect.



EDIT (John): Corrected code tag language.
« Last Edit: September 29, 2020, 10:54:08 AM by John Kaul (Se7en) »

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: Batch operations on multiple files
« Reply #19 on: September 29, 2020, 11:06:24 AM »
Sorry, I didn't get around to addressing your previous post.

Nice script!

Your line #49 can be cleaned up a bit by using a separate function like the following:

Code - Visual Basic: [Select]
  1. Function getExtension(filespec)
  2.   Dim fso
  3.   Set fso = CreateObject("Scripting.FileSystemObject")
  4.   Set f = fso.GetFile(filespec)
  5.  
  6.   getExtension = fso.GetExtensionName(filespec)
  7. End Function

Use like this:

Code - Visual Basic: [Select]
  1. If(UCase(getExtension(sFilePath))="DWG") Then 'proceed

I do not like line #39; would you be open to a bit of a restructure of the code block of lines 22-39?
You could add a SELECT CASE (-i.e. COND in autolisp) to do some prompting of misc errors and then eventually just quit the script after the for loop.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #20 on: September 29, 2020, 11:38:11 AM »
Please feel free to add/edit/change anything in the code. 

Edit:
I'm going through your comments with more than 1/2 a cup of coffee.


Thanks,
Chris
« Last Edit: September 29, 2020, 03:17:50 PM by framednlv »

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #21 on: September 29, 2020, 04:15:58 PM »
Sorry, I didn't get around to addressing your previous post.

Nice script!

Your line #49 can be cleaned up a bit by using a separate function like the following:

Code - Visual Basic: [Select]
  1. Function getExtension(filespec)
  2.   Dim fso
  3.   Set fso = CreateObject("Scripting.FileSystemObject")
  4.   Set f = fso.GetFile(filespec)
  5.  
  6.   getExtension = fso.GetExtensionName(filespec)
  7. End Function

Use like this:

Code - Visual Basic: [Select]
  1. If(UCase(getExtension(sFilePath))="DWG") Then 'proceed

I do not like line #39; would you be open to a bit of a restructure of the code block of lines 22-39?
You could add a SELECT CASE (-i.e. COND in autolisp) to do some prompting of misc errors and then eventually just quit the script after the for loop.
John,
Thank you for your comments.
I changed/added the getExtension function.  I also noticed the Echo command was erroring with the "&" symbol, so I added quotes to the file name.

I'm not sure what you had in mind for "restructure of the code block of lines 22-39".

I did notice the script doesn't do well with the count when dropping the folder on the script, it list every file as [1/1].  The other item is that it does not do sub folders and this would be nice.  Any thoughts on these two items?

Thanks,
Chris

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Batch operations on multiple files
« Reply #22 on: September 30, 2020, 02:42:19 PM »
This seems to work:
Code: [Select]
if /I [%~x1]==[.DWG] (echo "[%count%/%argc%]: Processing... %~f1" ) else (echo "[%count%/%argc%] skipping %~f1" )
if /I [%~x1]==[.DWG] (call "C:\Program Files\Autodesk\AutoCAD 2021\accoreconsole.exe" /i "%~f1" /s %script% /l en-US >"C:\temp\cleanup.log")

There is one problem I found.  If any of the file names or folders have the '&' character it doesn't work.

People who insist on using special characters in file names are a problem, not the names themselves.  But I'm a little old school.   :crazy2:
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

framednlv

  • Newt
  • Posts: 64
Re: Batch operations on multiple files
« Reply #23 on: September 30, 2020, 04:24:53 PM »

People who insist on using special characters in file names are a problem, not the names themselves.  But I'm a little old school.   :crazy2:

Yea, we can't tell people that hire us what to do or not do.