Processing a Directory of Files

I was recently asked a question about the best way to create a PDF file for every drawing in a specified directory and it’s subdirectories.  My answer is to write a small Visual Basic application to do it.  Visual Basic Express (which is free) is fully capable of creating an application like this. 

Below is the minimal code to get all of the files within a directory and perform some type of process on each one. The first line calls the .Net GetFiles function and passes in the directory name, a search pattern, and an argument indicating that all subdirectories should also be searched. This used to be a lot of work in VBA and is now a single line with .Net. Next it iterates over the list of filenames that were found and opens each one in Inventor, calls the SaveAsPDF function, and then closes the document.  The SaveAsPDF function is a function I wrote that’s not shown below but it calls the PDF translator and exports the drawing to PDF.  By simply replacing the SaveAsPDF call with a call to a different function you can change this program to do any type of processing you want.

' Get all of the drawing files in the directory and subdirectories. Dim drawings() As String = System.IO.Directory.GetFiles( _                            txtPath.Text, _                            "*.idw", _                            System.IO.SearchOption.AllDirectories)  ' Iterate through the found drawings. For Each drawing As String In drawings     Dim drawDoc As Inventor.DrawingDocument     drawDoc = invApp.Documents.Open(drawing)      ' Save the PDF.     SaveAsPDF(drawDoc, _               System.IO.Path.ChangeExtension(drawing, "pdf"))      ' Close the drawing.     drawDoc.Close(True) Next

The above code doesn’t have any error handling or any user interface. Once you add that and a couple of bells and whistles the code grows a bit. I’ve created a full application in Visual Basic Express 2010 that has a user interface and the bells and whistles. The user interface is shown below.

SaveAsPDF

You can see in the image above that some files failed to process.  This is because they were created in a later version of Inventor so the version of Inventor I’m using is unable to open them. The good news is that the program handles that case correctly and reports it.

The program is written to work with Inventor 2013 and later. You can download the executable here: SaveAsPDF_Exe.zip (28.5K)

And you can download the complete source code of the Visual Basic Express 2010 project here: SaveAsPDF_Source.zip (13.9K)


Comments

2 responses to “Processing a Directory of Files”

  1. Is it possible to get a version for dwgs? That would be great. Thanks

  2. im getting error on the first line..>>txtPath<<
    so ive replace it with the folder path but still error.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading