Jump to content

String help :(


huldu
 Share

Recommended Posts

I was messing around with strings today, everything worked fine until i tried to remove things from a string (got a bit complicated).

What i am trying to do is (example: "C:\downloads\test dir\file.jpg") to get the file name, in this case "file" to be seperated from the rest of the line. I can remove the last 4 letters, since its fairly easy with a line of code :P

$var = "C:\downloads\test dir\file.jpg"
$result = StringTrimRight($var, 4)

So now i have ""C:\downloads\test dir\file"

It got alot more complicated now and im not sure how to proceed to remove everything before the last "\". Tried using StringSplit and StringTrimRight, but never got the result i was looking for.

Thanks for any help on this.

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

  • Developers

$total="C:\downloads\test dir\file"
$File = StringMid($total, StringInStr($total, "\", 0, -1) +1)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

$var = StringSplit("C:\downloads\test dir\file.jpg", "\")
$no = $var[0]
$result = StringTrimRight($var[$no], 4)
MsgBox(0,"",$result)


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Surprised nobody has brought up _PathSplit()

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit("C:\downloads\test dir\file.jpg", $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath,"Demo _PathSplit()")
Msgbox(0,"$szDrive=",$szDrive)
Msgbox(0,"$szDir=",$szDir)
Msgbox(0,"$szFName=",$szFName)
Msgbox(0,"$szExt=",$szExt)

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

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