Working with Filenames

It’s common to need to manipulate filenames within a program.  When using VBA there’s very little built-in functionality to help with this.  When using VB.Net I had assumed I needed to do the same thing there that I had been doing in VBA.  It wasn’t until fairly recently that I discovered a set of utilities in the .Net Framework that makes working with filenames much easier.

The .Net Framework is one of the huge advantages of doing development with any of the .Net languages.  Especially compared to VBA which has very little built-in functionality.  As I find things in the .Net Framework I find useful I’ll add blog posts in case some of you are like me and are still discovering its capabilities.

For those of you using VBA, here are some functions that I wrote to work with filenames.  Using these I can extract the various bits of a filename and piece it back together to get the new filename I need.

' Return the path of the input filename.          Public Function FilePath(ByVal fullFilename As String) As String           ' Extract the path by getting everything up to and              ' including the last backslash "".               FilePath = Left$(fullFilename, InStrRev(fullFilename, "") - 1)       End Function                   ' Return the name of the file, without the path.          Public Function Filename(ByVal fullFilename As String) As String           ' Extract the filename by getting everything to              ' the right of the last backslash.               Filename = Right$(fullFilename, Len(fullFilename) – _                      InStrRev(fullFilename, ""))       End Function                   ' Return the base name of the input filename, without          ' the path or the extension.           Public Function BaseFilename(ByVal fullFilename As String) As String           ' Extract the filename by getting everttgubg to              ' the right of the last backslash.               Dim temp As String           temp = Right$(fullFilename, Len(fullFilename) – _                  InStrRev(fullFilename, ""))                 ' Get the base filename by getting everything to              ' the left of the last period ".".               BaseFilename = Left$(temp, InStrRev(temp, ".") - 1)       End Function                   ' Return the extension of the input filename.          Public Function FileExtension(ByVal fullFilename As String) As String           ' Extract the filename by getting everthing to      ' the right of the last backslash.               Dim temp As String           temp = Right$(fullFilename, Len(fullFilename) – _                  InStrRev(fullFilename, ""))                 ' Get the base filename by getting everything to              ' the right of the last period ".".               FileExtension = Right$(temp, Len(temp) - InStrRev(temp, ".") + 1)       End Function                   ' Test the filename functions.       Public Sub TestFileFunctionsVBA()           Dim fullFilename As String           fullFilename = "C:SamplesModelsPartsOilPanOilPan.ipt"           Debug.Print FilePath(fullFilename)           Debug.Print Filename(fullFilename)           Debug.Print BaseFilename(fullFilename)           Debug.Print FileExtension(fullFilename)       End Sub

Below are the results as seen in the Immediate window.

C:SamplesModelsPartsOilPan    OilPan.ipt     OilPan     .ipt

Using VB.Net this is all much easier.  The .Net Framework supports the IO class which has a lot of file related functionality.  For working with file paths the IO class provides access to the Path class.  Below is a VB.Net function that does the same thing as the VBA code above but it uses .Net Framework functions instead of custom functions.  You’ll need to use the Imports statement for System.IO or specify the full namespace path as demonstrated in the GetDirectoryName call below.

Imports System.IO            ' Test the .Net Framework Path functions.         Public Sub TestFileFunctionsVBNet()           Dim fullFilename As String           fullFilename = "C:SamplesModelsPartsOilPanOilPan.ipt"           Debug.Print(System.IO.Path.GetDirectoryName(fullFilename))           Debug.Print(Path.GetFileName(fullFilename))           Debug.Print(Path.GetFileNameWithoutExtension(fullFilename))           Debug.Print(Path.GetExtension(fullFilename))       End Sub

As you can see the results are identical to the VBA sample without having to write the custom functions.

C:SamplesModelsPartsOilPan      OilPan.ipt       OilPan       .ipt

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading