ERLANDSEN DATA CONSULTING Excel & VBA Tips   Informasjon på norsk / Information in Norwegian

These pages are no longer updated and are only available for archive purposes.

Click here to visit the pages with updated information.

File names and folder names

The function below can be used to return the file name or the folder name from a full file name:

Function FileOrFolderName(InputString As String, _
    ReturnFileName As Boolean) As String
' returns the foldername without the last pathseparator or the filename
Dim i As Integer, FolderName As String, FileName As String
    i = 0
    While InStr(i + 1, InputString, Application.PathSeparator) > 0
        i = InStr(i + 1, InputString, Application.PathSeparator)
    Wend
    If i = 0 Then
        FolderName = CurDir
    Else
        FolderName = Left(InputString, i - 1)
    End If
    FileName = Right(InputString, Len(InputString) - i)
    If ReturnFileName Then
        FileOrFolderName = FileName
    Else
        FileOrFolderName = FolderName
    End If
End Function

Sub TestFileOrFolderName()
    MsgBox FileOrFolderName(ThisWorkbook.FullName, False), , _
        "This Workbook Foldername:"
    MsgBox FileOrFolderName(ThisWorkbook.FullName, True), , _
        "This Workbook Filename:"
End Sub

 

Document last updated 2000-02-04 12:49:08      Printerfriendly version

User comments:
Ole P. from Norway wrote (2005-04-26 11:32:45 CET):
Re: Complete path
You can find an example here: Create new folders
Alexander Crokos from Jersey City, USA wrote (2005-04-26 01:09:06 CET):
Complete path
How would you go about determining if the whole path exists, excluding filename, then create the whole path.

'c:\x\x\123'

for example...check to see if 123 exists...if not then check to see if c:\x\ exists then create it and so on...

have you ever looked into this...

Alexander Crokos from Jersey City, USA wrote (2005-04-26 00:42:49 CET):
Files/Folders
Excellent.
Ole P. from Norway wrote (2004-06-01 08:26:32 CET):
Re: Files/Folders
The procedure "TestFileOrFolderName" above is the example on how you can use the code.
If you need even more instructions on how to use macros, see the menu "VBA Programming" to the left.
Kevin P Rosteing from Green Bay wrote (2004-05-31 22:14:05 CET):
Files/Folders
You should give examples of how I am supposed to use this code.

 

Erlandsen Data Consulting     http://www.erlandsendata.no/   
Excel & VBA Tips   Copyright ©1999-2017    Ole P. Erlandsen   All rights reserved
E-mail Contact Address