Get filename minus extension from full file path using VBScript

I have a path to a file, e.g. \\server\share\folder\subfolder\filename.extension

I want just the filename bit so that I can use it to create a temporary folder.

During the course of thinking about how best to do this I came up with three different versions (thought number 3 is really a refinement of number 2).

I could use a Scripting.Filesystem object, and use the .GetFileName and .GetExtensionName methods. I could do something like:

WScript.Echo getFileNameFSO("\\server\share\folder\subfolder\filename.extension")
Function getFileNameFSO(sFullPath)
 Dim oFSO, sFileName, sFileExtension
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 sFileName = oFSO.GetFileName(sFullPath)
 sFileExtension = oFSO.GetExtensionName(sFullPath)
 getFileNameFSO = Replace(sFileName, "." & sFileExtension, "")
End Function

This actually does not require that the file specified by sFullPath exists.

As an alternative, I also came up with the following function:

WScript.Echo getFileName("\\server\share\folder\subfolder\filename.extension")
Function getFileName(sFile)
  Dim i, iLastSlashPos, iLastDotPos
  iLastSlashPos = 0
  For i = Len(sFile) To 1 Step -1
   If Mid(sFile,i,1) = "\" Then
     iLastSlashPos = i
    Exit For
   End If
  For i = Len(sFile) To 1 Step -1
   If Mid(sFile,i,1) = "." Then
    iLastDotPos = i
    Exit For
   End If
  If iLastDotPos <= iLastSlashPos Then
   iLastDotPos = Len(sFile)+1
  End If
  getFileName = Mid(sFile,iLastSlashPos+1,iLastDotPos-1-iLastSlashPos)
End Function

This one steps backwards through the file path passed in, one character at a time, looking first for a backslash, and then for a dot. It then uses the positions of those two characters in the filepath to extract out only the filename.

Another way of coding the above function would be:

Function getFileNameRev(sFile)
 Dim i, iLastSlashPos, iLastDotPos
 iLastSlashPos = InStrRev(sFile,"\")
 iLastDotPos = InStrRev(sFile,".")
 If iLastDotPos <= iLastSlashPos Then
  iLastDotPos = Len(sFile)+1
 End If
 getFileNameRev = Mid(sFile,iLastSlashPos+1,iLastDotPos-1-iLastSlashPos)
End Function

These all cope with various different input strings:

  • \\server\share\folder\subfolder\filename.extension
  • filename.extension
  • filename

in each case just returning filename, but you might want to check them for robustness in your own environment as they obviously have no error handling in them.

This entry was posted in Scripting and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.