Trying to get just the file name

I'm using VBA to read a text file made of files paths (path & name). I'm opening them in word and trying to resave them into .txt format for notepad/wordpad. There is no easy way to be able to find the last "\" and then use that to take all the text after it.

example: "C:\my docs\this file.doc" i would want the "this file" portion of the text


I found what seems to be a useful method (sourcename) but have no idea how to implement it


If f1 = fso.FileExists(file) Then
Documents.Open filename:=line, ConfirmConversions:=False, ReadOnly _
:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _
:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _
, Format:=wdOpenFormatAuto, XMLTransform:="" 'open file

** here is where i'm looking to get just the file name**
**temp is the variabel below that should hold the file name**

ActiveDocument.SaveAs filename:=temp + ".txt", FileFormat:=wdFormatText, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=True, _
LineEnding:=wdCRLF 'saves file in chosen format

ActiveDocument.Close 'close file
End If

any help would be very much appreciated




Answer this question

Trying to get just the file name

  • Mauro_Net

    you can get the base file name with the FileSystemObject more directly using the GetBaseName method instead of the GetFileName method:

    Dim fso as New FileSystemObject

    Dim fileName as string

    fileName = fso.GetBaseName("C:\My Dir\My File.ext)

    fileName will contain "My File"

    rusty


  • peetha

    Hi,

    This code will do what your looking for

        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
       
        Dim filename As String
        filename = fs.GetFileName("C:\MyDir\MyApp.exe")  

        'file extension has three characters and the dot (4 characters)
        MsgBox Mid(filename, 1, Len(filename) - 4)



  • beefeater

    THIS WAS EXACTLY WHAT I WAS LOOKING FOR.

    THANK YOU SO MUCH FOR YOUR HELP.



  • Trying to get just the file name