How Can I Extract Just the File Name from the Full Path to the File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I extract just the file name from the full path to the file?

-- SD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SD. As you might know, Ted Williams was the last major league baseball player to bat .400 for an entire season. Coming into the final day of the year, Williams was actually hitting something like .3997 which, when rounded up, would give him an official batting average of .400. The manager of the Red Sox offered to let Williams sit out the final doubleheader, enabling him to ensure his place in baseball history. But Ted Williams refused to take the easy way out: not only did he want to play, but he wanted to reach .400 legitimately, not due to some mathematical hocus-pocus like rounding up.

So what happened? Williams went 6-for-8 in the final doubleheader, hit .406 for the season, and punched his ticket to the Baseball Hall of Fame. And all because he refused to take the easy way out. No doubt there's a lesson in there for all of us.

As it turns out, the Scripting Guys faced a similar dilemma when it came time to answer your question. This is actually a fairly easy question to answer; in fact, the solution requires just three lines of code. That's fine, but it doesn't really sound like enough information to build an entire column out of. So, will the Scripting Guys be like Ted Williams and refuse to take the easy way out? Will they go above and beyond the call of duty and write a long and thoughtful column on a more complicated subject? Will they do the right thing?

You've got to be kidding, right? Here's what we came up with instead:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\Scripts\Test.txt")
Wscript.Echo "File name: " & objFSO.GetFileName(objFile)

So why is this question is so easy to answer? Because the FileSystemObject has several methods for extracting the individual pieces of a file path, including one method - GetFileName - that can grab just the file name for you. To do that we simply create an instance of the Scripting.FileSystemObject, then use the GetFile method to bind to the file in question (in this case, C:\Scripts\Test.txt). After that we call the GetFileName method, passing the object reference to C:\Scripts\Test.txt as the sole parameter to that method:

Wscript.Echo "File name: " & objFSO.GetFileName(objFile)

And what do we get back? We get back the name of the file, just like you were hoping to:


The end.

Actually, that's a good point: there's no doubt that Ted Williams would have done more than simply type three lines of code. OK, how about this? Here's a revised script that shows some of the other items that the FileSystemObject can extract from a file path:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\Scripts\Test.txt")

Wscript.Echo "Absolute path: " & objFSO.GetAbsolutePathName(objFile)
Wscript.Echo "Parent folder: " & objFSO.GetParentFolderName(objFile)
Wscript.Echo "File name: " & objFSO.GetFileName(objFile)
Wscript.Echo "Base name: " & objFSO.GetBaseName(objFile)
Wscript.Echo "Extension name: " & objFSO.GetExtensionName(objFile)

And here's what we get back:

Absolute path: C:\scripts\test.txt
Parent folder: C:\scripts
File name: test.txt
Base name: test
Extension name: txt

Better? Admittedly it's not quite the same thing as going 6-for-8 in a doubleheader. But we like to think of it as being the scripting equivalent.

Incidentally, this approach works with UNC paths as well as with local file paths. Suppose we have a file with the UNC path \\atl-fs-01\Public\Test.txt. If we run GetFileName against that file we'll get back the following:

Absolute path: \\atl-fs-01\public\test.txt
Parent folder: \\atl-fs-01\public
File name: test.txt
Base name: test
Extension name: txt


The only catch to all this is that the file must exist; otherwise you'll get a "File not found" error. But suppose you have a series of file paths that don't exist. What do you do then? Give up?

Come on: would Ted Williams give up? Of course not. Instead, he'd write a script like this:

strName = "C:\Scripts\Test.txt"
arrNames = Split(strName, "\")
intIndex = Ubound(arrNames)
Wscript.Echo "File name: " & arrNames(intIndex)

What we're doing here is taking a file path (C:\Scripts\Test.txt) and stashing that path in a variable named strName. We then use the VBScript Split function to chop that file into an array, splitting the path on the \ character:

arrNames = Split(strName, "\")

When we do that we end up with an array consisting of the following elements:




Does that help us? You bet it does. As you can see, the very last element in the array just happens to be our file name. To retrieve that value we need to first use the Ubound function to determine the index number of that item:

intIndex = Ubound(arrNames)

And then, with the index number in hand, we can echo back the file name:

Wscript.Echo "File name: " & arrNames(intIndex)

It's a tiny bit more complicated than using the FileSystemObject, and it gets even more complicated if we wanted to tease out, say, the file extension or the parent folder. But if all we want is the file name it's not so bad.

Does that answer your question, SD? If it does, well, we hope you'll remember the Scripting Guys when it comes time to elect inductees to the Scripting Hall of Fame. And if it doesn't, well, maybe you could still vote for us when it comes time to elect inductees to the Scripting Hall of Fame. After all, we're pretty sure that's where Ted Williams would have preferred to be immortalized. Too bad he had to settle for the Baseball Hall of Fame

Comments (13)

  1. Biju says:

    Thanks a Lot Scriptin guy!!!

    It really helped me a lot!!!

  2. Zeke4 says:

    I am trying to do a similar thing, except I only have a the path and file name without an extension. Or lets say a partial file name. How would I search for file and return the full path of the file. In my case I have everything except the extension, being the extension may change depending if it is a Word, Excel, etc file.

    Any help would be greatly appreciated.

    Thank You


  3. Jd says:

    Why is it that in JScript, line 4 seems to not have a Name property on the File object (file variable) while on line 7 it does?

    1. var fso = new ActiveXObject("Scripting.FileSystemObject");

    2. var file = fso.CreateTextFile("C:\test.txt");

    3. file.WriteLine("foo"); file.Close();

    4. WScript.Echo("File name: " + file.Name); /* "undefined" */

    5. WScript.Echo("File name via fso: " + fso.GetFileName("C:\test.txt")); /* "test.txt" */

    6. file = fso.GetFile("C:\test.txt");

    7. WScript.Echo("File name via fso.GetFile: " + file.Name); /* "test.txt" */

    After creating the file, I would expect to be able to pass the reference to it to another function and have that function be able to pull it's file name and created date etc from it but it doesn't behave that way. It's like the object returned from CreateTextFile is some type of crippled File object (despite what the documentation alludes to).

  4. Tom L says:

    Here's how I do it, whether the file exists or not….

    p = "C:Automationresultsfile.txt"

    i = instrrev(p, "", -1)

    d = left(p, i-1)

    f = mid(p, i+1)




  5. pansadi says:

    GetFileName() and GetFile() both require FileSystemObject, what about to achieve this without FSO? Also with GetFile() will an error occurs if the specified file does not exist.

  6. Bhakti says:

    very useful


  7. derikogay says:

    Well, you can use Long Path Tool for such problems, it really works good.

  8. rockstar programmer says:

    it’s ok

  9. Scott says:

    (Get-ChildItem "C:ScriptsTest.txt").Name

  10. payal( says:

    Please help
    (1) i have one xml with lots many tags
    (2) i have many audio files with random generated number as a name and that name is in XML file
    (3) please tell me how can i rename those audio files with one of tag details present in xml file. i have to search that audio file as the name of the audio file is present in xml and under that section i will use one of the tag to rename it


    Gambardella, Matthew

    An in-depth look at creating applications with XML.

    Ralls, Kim

    A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.

    Corets, Eva

    After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.

    Corets, Eva

    In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.

    Galos, Mike

    Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.

    (a)audio file name is >> which is defined as a book id in xml file , now i want to change it to computer.wav (which is in genre tag under book id tag of bk101)

    thank you

Skip to main content