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

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

  • Comments 9
  • Likes
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:

test.txt

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

Nice.

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:

C:

Scripts

Test.txt

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks a Lot Scriptin guy!!!

    It really helped me a lot!!!

  • 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

    Zeke

  • 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).

  • Check this out:

    stedanarh.wordpress.com/.../03

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

    p = "C:\Automation\results\file.txt"

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

    d = left(p, i-1)

    f = mid(p, i+1)

    p=C:\Automation\results\file.txt

    d=C:\Automation\results

    f=file.txt

  • 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.

  • very useful

    Thanks

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

  • Awesome!