Jump to content

Get String After Last '\' in File Address


olympus
 Share

Recommended Posts

How do I get the string after the last '\' in a file's address? Let's say the file is located in C:\Program Files\GreatSoftware\link.html. How do I obtain 'link.html' from the full address? I think I should use StringSplit but I'm not familiar with it.

Please help, thank you.

Link to comment
Share on other sites

Please see _PathSplit in the help file.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Function _PathSplitThere is also the useful

_PathSplit function which splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing.

and _PathMake function to put all the parts back together as a valid path.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Function _PathSplitThere is also the useful

_PathSplit function which splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing.

and _PathMake function to put all the parts back together as a valid path.

How do I use _PathSplit? I referred to the help file and I didn't understand the example involving arrays... can someone make an example with a msgbox output? Thank you.

Link to comment
Share on other sites

Yashied gave you the best solution but I'll turn it into an example

$sPath = "C:\Program Files\GreatSoftware\link.html"
$sFileName = StringRegExpReplace($sPath, "^.+\\(.*)$", $1)
MsgBox(0, "Result", $sfileName)

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

Yashied gave you the best solution but I'll turn it into an example

$sPath = "C:\Program Files\GreatSoftware\link.html"
$sFileName = StringRegExpReplace($sPath, "^.+\\(.*)$", $1)
MsgBox(0, "Result", $sfileName)

Thank you GEO for the example, but what are the meaning of the symbols in between the quotes and teh $1 at the end?
Link to comment
Share on other sites

$sPath = "C:\Program Files\GreatSoftware\link.html"
$sFileName = StringRegExpReplace($sPath, "^.+\\(.*)$", $1)
MsgBox(0, "Result", $sfileName)

Take a look at the docs for StringRegExp for a description of what each part of a regular expression does. Let's see:

^ - beginning of string

. - any character

+ - previous character (group/set) one or more times

\\ - any actual \. Normally \ is used to escape special characters.

( - begin a capturing group. Contents from this group will go into $1

. - any character (again)

* - previous character (or group or set) zero or more times.

) - end capturing group.

$ - end of string.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Take a look at the docs for StringRegExp for a description of what each part of a regular expression does. Let's see:

^ - beginning of string

. - any character

+ - previous character (group/set) one or more times

\\ - any actual \. Normally \ is used to escape special characters.

( - begin a capturing group. Contents from this group will go into $1

. - any character (again)

* - previous character (or group or set) zero or more times.

) - end capturing group.

$ - end of string.

Oh okok. I'll check it out. Thank you!

Link to comment
Share on other sites

Hi olympus... that is regular expression string pattern... don't be afraid of that, you'll get used to it... you can also use string split, then take the last element...

$folder = FileSelectFolder("Select any folder","")  ;select folder, obviously

$path_array = StringSplit($folder,"\") ; Split path into an array element

$folder_name = $path_array[UBound($path_array)-1] ; take the last element

MsgBox(0,"",$folder_name) ; popup message box
Edited by Mison

Hi ;)

Link to comment
Share on other sites

Oh... now I get it, so when we're using the script below:

$sPath = "C:\Program Files\GreatSoftware\link.html"
StringRegExpReplace($sPath, "^.*\\", "")
MsgBox(0, "Result", $sfileName)

...we're telling it to replace all the characters(C:\Program Files\GreatSoftware\ = "^.*\\") before the file name with nothing(""), hence we are only left with the file name(link.html).

Thanks people, the examples were helpful. :)

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...