Sign in to follow this  
Followers 0
olympus

Get String After Last '\' in File Address

12 posts in this topic

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.

Share this post


Link to post
Share on other sites



Please see _PathSplit in the help file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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!"

Share this post


Link to post
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?

Share this post


Link to post
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...

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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 ;)

Share this post


Link to post
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. :)

Share this post


Link to post
Share on other sites

Yupp, that's the idea... replace unwanted characters with empty string("") a.k.a delete them.. ;P


Hi ;)

Share this post


Link to post
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
Sign in to follow this  
Followers 0