Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Paths and filenames

Does VBA have built-in functions for extracting just the filename
from a path string? It would also be nice if I could extract the
filename minus the Windows extension. For example, I
might have this string:

"C:\tmp\files\mydata\yearly.docx"

I would need to extract the "yearly" portion. Extracting the whole
filename of "yearly.docx" would be useful too.




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Paths and filenames

Hi Robert,

Am Mon, 11 May 2015 13:06:56 -0700 schrieb Robert Crandal:

"C:\tmp\files\mydata\yearly.docx"

I would need to extract the "yearly" portion. Extracting the whole
filename of "yearly.docx" would be useful too.


look for Instr and InstrRev:

mystr = "C:\tmp\files\mydata\yearly.docx"
mystr = Mid(mystr, InStrRev(mystr, "\") + 1)
mystr = Left(mystr, InStr(mystr, ".") - 1)
MsgBox mystr


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Paths and filenames

Hi Robert,

Am Mon, 11 May 2015 13:06:56 -0700 schrieb Robert Crandal:

Does VBA have built-in functions for extracting just the filename
from a path string? It would also be nice if I could extract the
filename minus the Windows extension. For example, I
might have this string:


in Word you can use
ThisDocument.FullName 'Path and name with extension
ThisDocument.Path 'Path only
ThisDocument.Name 'Name with extension

The same in Excel:
ThisWorkbook.FullName
ThisWorkbook.Path
ThisWorkbook.Name


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Paths and filenames

Il giorno luned́ 11 maggio 2015 22:07:02 UTC+2, Robert Crandal ha scritto:
Does VBA have built-in functions for extracting just the filename
from a path string? It would also be nice if I could extract the
filename minus the Windows extension. For example, I
might have this string:
"C:\tmp\files\mydata\yearly.docx"
I would need to extract the "yearly" portion. Extracting the whole
filename of "yearly.docx" would be useful too.


Hi,
try:

Option Explicit

Const s = "C:\tmp\files\mydata.old\yearly.1.docx"

Sub Test()
Test1
Test2
Test3
End Sub

Sub Test1()
Dim p() As String
Dim q() As String
Dim b As String
Dim e As String

p = Split(s, "\")
q = Split(p(UBound(p)), ".")
e = q(UBound(q))
b = Replace(p(UBound(p)), "." & e, "")

Debug.Print s
Debug.Print b, e
Debug.Print
End Sub

Sub Test2()
Dim d As Long
Dim p As String
Dim b As String
Dim e As String

d = InStrRev(s, ".")
p = Left$(s, d - 1)
b = Mid$(p, InStrRev(p, "\") + 1)
e = Mid$(s, d + 1)

Debug.Print s
Debug.Print b, e
Debug.Print
End Sub

Sub Test3()
Dim fso As Object 'Scripting.FileSystemObject
Dim b As String
Dim e As String

Set fso = CreateObject("Scripting.FileSystemObject")
With fso
b = .GetBaseName(s)
e = .GetExtensionName(s)
End With
Set fso = Nothing

Debug.Print s
Debug.Print b, e
Debug.Print
End Sub

--
Ciao! :)
Maurizio
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Paths and filenames

Does VBA have built-in functions for extracting just the filename
from a path string? It would also be nice if I could extract the
filename minus the Windows extension. For example, I
might have this string:

"C:\tmp\files\mydata\yearly.docx"

I would need to extract the "yearly" portion. Extracting the whole
filename of "yearly.docx" would be useful too.


I'd go with Claus' 2nd reply because those fit your request for
'built-in functions'! I'd just add...

Dim vTmp
vTmp = Split(ThisWorkbook.Name, ".")

...so vTmp(0) contains the filename and vTmp(1) contains the extension.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Paths and filenames

"GS" wrote:

I'd go with Claus' 2nd reply because those fit your request for 'built-in
functions'! I'd just add...

Dim vTmp
vTmp = Split(ThisWorkbook.Name, ".")

..so vTmp(0) contains the filename and vTmp(1) contains the extension.


Thank you everyone for your help. Having multiple different solutions
was exactly what I preferred.



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
Help with paths Albert Browne Excel Programming 2 September 14th 08 12:12 PM
Paths Ross[_2_] New Users to Excel 1 April 4th 07 11:35 PM
Using File System Object to retrieve paths filenames and dates dd Excel Programming 3 March 2nd 07 05:02 PM
Macro To Print Files By A Listing of Filenames And Paths Chris_In_Raleigh Excel Programming 0 November 7th 06 07:58 PM
Help with filenames and paths. jim9912 Excel Discussion (Misc queries) 2 March 30th 06 05:52 PM


All times are GMT +1. The time now is 10:18 PM.

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"