Get Folder, Name or Extension - May 1999 - October 2005 - January 2008
Some years ago I wrote several functions to parse a fully qualified file name into the path [folder], file name with extension, file name without extension and the file's extension. Recently I noted discussion in news groups dealing with this subject so I decided to make this the tip-of-the-month.
2002/10/15 I converted the function for VB.Net. And I added a function called 'AppPath' which returns the executing path - the function is the same as VB6's App.Path function.
2003/02/03 Function AddBackslash is moved to a separate module for the VB6 version. Add a browse button.
2005/10/21 I moved the code to a class module called 'cFilenameParse'. The module 'modPath.bas' is still distributed but all the functionality is now contained in 'cFilenameParse.cls'. And the word 'path' is replaced with 'folder' in most references. modAddBackslash.bas is also still distributed but the functionality is now available in the new class. The code has also been modified for VB.Net.
2005/10/22 I wrote a VB.Net demo program.
2008/01/02 I rewrote the VB.Net version. It now uses System.IO method Path to do the heavy lifting. Note: If you downloaded .Net source code before this date you should download this version. The prior version failed to correctly parse a file name if the file name contained periods.
The Demo Program - VB6
I wrote a VB6 demo program to illustrate the use of the class. The demo locates the windows folder and displays it during the form load process. The demo program's form:
Enter any fully qualified file name into the '.sFolderAndName' text box then click the 'Get Folder, Name or Extension from a Fully Qualified File Name' button to parse the entry. The parsed values are shown in the appropriate text boxes. Use the 'browse' button to locate a file.
Enter any string in the App.Path text box then click the 'Add Backslash' button to add a backslash to the text if it does not already contain one. Use the 'Remove Backslash' button to remove it.
The Demo Program - VB.Net
I wrote a VB.Net demo program to illustrate the use of the class. The demo program's form:
Enter any fully qualified file name into the '.sFolderAndName' text box then click the 'Get Folder, Name or Extension from a Fully Qualified File Name' button to parse the entry. The parsed values are shown in the ListView. Use the 'browse' button to locate a file.
Use the 'Add Backslash' or 'Remove Backslash' commands to add or remove a backslash from .sFolder.
To use the functions in VB6 add file cFilenameParse.cls to your project. To use the functions in VB.Net add file cFilenameParse.vb to your project. Methods and properties found in the classes include:
Properties Read/Write Sample Description .sFolderAndName Write Only .FolderAndName = "c:\Autoexec.bat" The fully qualified file name. .sFolder Read Only Me.Text1.Text = .sFolder The folder including the ending backslash. .sName Read and Write Me.Text1.Text = .sName The file name with extension parsed from the fully qualified file name. Causes the following five properties to be set. .sNameAndExt Read Only Me.Text1.Text = .sNameAndExt Same as .sName .sNameNoExt Read Only Me.Text1.Text = .sNameNoExt The file name without the extension. .sExt Read and Write o.sName = "myfile.txt"
Me.Text1.Text = .sExt
The extension. Created when the .sName property is set. See .sName above. .sExtNoPeriod Read Only Me.Text1.Text = .sExtNoPeriod Same as .sExt. .sExtWithPeriod Read Only Me.Text1.Text = .sExtWithPeriod .sPeriod is appended to .sExt. .sPeriod Read/Write .sPeriod = "." 'Default The name/extension separator. Default is a period. .sSlash Read/Write .sSlash = "\" 'Default The path/filename separator. Default is a backslash. Methods Sample Description .AddBackslash [(sFolder)] sFolder = AddBackslash(sFolder) Add .sSlash to the folder name if needed. .sFolder is used if not supplied. .RemoveBackslash [(sFolder)] sFolder = RemoveBackslash(sFolder) Remove .sSlash from the folder name if needed .HasBackslash [(sFolder)] If HasBackslash(sFolder) Then Return True if sfolder has an ending .sSlash. Methods - VB.Net Only Sample Description .AppPath Dim o As New cFilenameParse
sPath = o.AppPath
Return the executing applications folder.
Download VB Code
Click here to download the VB 6 Source Code [40k]. Click here to download the VB.Net Source Code [32k].
If you download this tip please consider making a modest donation of a $1.00 or more.
Previous Tip Tip Index Next Tip