GEOSoft Posted August 13, 2009 Share Posted August 13, 2009 In the past people have generally used _PathSplit() as the prefered method for getting a file name. That's doing it the long way. Here is a shorter method that I'll be adding to my FileX.au3 UDF soon. It returns the file name from either a normal system path or a URL path and includes error checking. Example usage: ; $sFile = "http://dundats.mvps.org/my files/FileName.htm" MsgBox(0, "Test", _File_GetFileName($sFile)) $sFile = "C:\Some\Folder\Path\Another FileName.au3" MsgBox(0, "Test", _File_GetFileName($sFile)) ; _File_GetFileName() ; Func _File_GetFileName($sStr) $sStr = StringRegExpReplace($sStr, "^.*[\\|/](.+\..+)$", "$1") If @Error OR (NOT $sStr) Then Return SetError(1, 1, "") Return $sStr EndFunc ; If you don't want error checking then you can use this simplified version ; Func _File_GetFileName($sStr) Return StringRegExpReplace($sStr, "^.*[\\|/](.+\..+)$", "$1") EndFunc ; George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
ResNullius Posted August 13, 2009 Share Posted August 13, 2009 George, Don't you mean $sStr = StringRegExpReplace($sStr, "^.*[\\|/](.+)$", "$1") Your example fails if the file has no extension specified. Link to comment Share on other sites More sharing options...
GEOSoft Posted August 13, 2009 Author Share Posted August 13, 2009 George, Don't you mean $sStr = StringRegExpReplace($sStr, "^.*[\\|/](.+)$", "$1") Your example fails if the file has no extension specified. That's exactly what I started with but then I decided that I only wanted it to work on files and not on folders so I changed it. I could have used what you have or even $sStr = StringRegExpReplace($sStr, "^.*[\\|/](.+\.?.+)$", "$1") But either of those will also return the last part of the path even if it's a folder. Although that won't be difficult to solve either with FileGetAttrib() in there as well. For an example of this in use look here. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now