Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 204
Default Separate Filename and Extension

I'd like to be able to separate the extensions from filenames, do I have to
find the last '.' ? If so what's the best way?

In the back of my mind there is a statement that reurns the filename with
out the extension - or did I dream that?

Thanks

M

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Separate Filename and Extension

Use something like this

=InstrRev(myWB.name,".")

HTH,
Barb Reinhardt

"Michelle" wrote:

I'd like to be able to separate the extensions from filenames, do I have to
find the last '.' ? If so what's the best way?

In the back of my mind there is a statement that reurns the filename with
out the extension - or did I dream that?

Thanks

M

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Separate Filename and Extension


Try these 2 statements in the immediate pane of any workbook that's been
saved:

?mid(thisworkbook.Name,instrrev(thisworkbook.Name, "."))
?left(thisworkbook.Name,instrrev(thisworkbook.Name ,".")-1)


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=134200

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 204
Default Separate Filename and Extension

Thanks guys, I didn't know about instrrev...

M


"p45cal" wrote in message
...

Try these 2 statements in the immediate pane of any workbook that's been
saved:

?mid(thisworkbook.Name,instrrev(thisworkbook.Name, "."))
?left(thisworkbook.Name,instrrev(thisworkbook.Name ,".")-1)


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread:
http://www.thecodecage.com/forumz/sh...d.php?t=134200


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Separate Filename and Extension

You can use a scripting object which automatically does it for you, but it is
easier doing it like thiis. Find the last slash in the string

folder = left(FName,instrrev(FName,"\") - 1)
BaseName = mid(FName,instrrev(FName,"\") + 1)


"Michelle" wrote:

I'd like to be able to separate the extensions from filenames, do I have to
find the last '.' ? If so what's the best way?

In the back of my mind there is a statement that reurns the filename with
out the extension - or did I dream that?

Thanks

M



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Separate Filename and Extension

Try something like the following:

Dim FName As String
Dim ExcludeDot As Boolean
Dim Extension As String
FName = "C:\Test\book2.xlsm"
ExcludeDot = True
Extension = Mid(FName, InStrRev(FName, ".") + Abs(ExcludeDot))
Debug.Print Extension

If ExcludeDot is True the extension will not contain the leading
period. If ExcludeDot is False, the extension will begin with the
leading dot. It is assume that FName does in fact have an extension.

In my standard code library, I use code like the following:

Type TFileNameStruct
Root As String
Path As String
File As String
Extension As String
End Type

Sub GetFileNameStruct(ByVal FileName As String, _
ByRef TStruct As TFileNameStruct)
Dim N As Long
If StrComp(Left(FileName, 2), "\\", vbBinaryCompare) = 0 Then
TStruct.Root = Left(FileName, _
InStr(3, FileName, "\") - 1)
Else
TStruct.Root = Left(FileName, _
InStr(1, FileName, ":", vbBinaryCompare))
End If

N = InStrRev(FileName, "\")
TStruct.Path = Left(FileName, N - 1)
TStruct.File = Mid(FileName, N + 1)
N = InStrRev(FileName, ".")
If N 0 Then
TStruct.Extension = Mid(FileName, N)
End If
End Sub

I then call this code with something like

Sub Test()
Dim TStruct As TFileNameStruct
Dim FileName As String
FileName = "C:\Test\Sub\Book1.xlsm"
GetFileNameStruct FileName, TStruct
With TStruct
Debug.Print .Root, .Path, .File, .Extension
End With
End Sub





Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Sun, 13 Sep 2009 12:17:04 +0100, "Michelle"
wrote:

I'd like to be able to separate the extensions from filenames, do I have to
find the last '.' ? If so what's the best way?

In the back of my mind there is a statement that reurns the filename with
out the extension - or did I dream that?

Thanks

M

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert filename in header without file extension Andrew King Excel Discussion (Misc queries) 4 January 12th 10 09:48 PM
Get Filename without the extension Larry S Excel Programming 9 October 23rd 08 08:44 AM
Converting a Variable Filename to a Constant Filename Magnivy Excel Programming 2 August 15th 06 06:13 PM
Getting only the filename with no extension Barry Clark Excel Worksheet Functions 2 June 26th 06 07:51 PM
Transformation d'images avec une extension .jpg ou .jpeg en images avec extension .bmp ou .ico imej-clavier Excel Discussion (Misc queries) 1 May 28th 05 05:52 PM


All times are GMT +1. The time now is 12:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"