Jump to content

Recommended Posts

Posted (edited)

Sorry if this question sounds noobish, but i'm trying to extract some filenames from a list and i'm unable to do it so far.

I have this:

C:picture1.jpg

C:picture2.jpg

And i want to extract this:

picture1.jpg

picture2.jpg

So... for example

$sString = "c:image.jpg"
StringSplit($sString, "")
$result=StringTrimRight.....??? I'm lost here.
MsgBox(0, "Result", $result)

The path varies, and i want to extract the extension and file name from right to left until it reaches the ''.

Edited by telmob
  • Moderators
Posted

telmob,

This should extract the filename and extension from any path:

Global $aPath[4] = ["C:picture1.jpg", "C:picture2.jpg", "D:anyPathpicture3.jpg", "Z:AndAnotherOnepicture4.jpg"]

For $i = 0 To 3
    $sFileNameExt = StringRegExpReplace($aPath[$i], "^.*", "")
    MsgBox(0, "Result", $aPath[$i] & @CRLF & @CRLF & $sFileNameExt)
Next

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • Moderators
Posted

telmob,

Allow me to explain:

^   - Start at the beginning of the string
.*  - Look for any number of characters until we find
  - a "" (we need to escape it with another )

""  - And replace all that with nothing

Because SREs are naturally "greedy", they always take the final "" of the path as the one to use and so just leave the filename and extension. :)

Clearer now? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

whoo... very nice.

Much clearer now. I'll 'favoritize' stringregexp, it seems it will come in handy many times.

  On 11/19/2012 at 2:03 PM, 'JohnOne said:

Using you own code, here is how you would do it.

$sString = "c:image.jpg"

$astringsplit = StringSplit($sString, "")

MsgBox(0, "Result", $astringsplit[ $astringsplit[0] ]) ; $astringsplit[0] is the last element in the array

Seems easier to understand. :)

Thank you both Melba and John, terrific help.

  • 3 weeks later...
Posted

What about StringInStr and StringTrimLeft as well?

Local $sFilePath = 'C:Picture.jpg'
ConsoleWrite(StringTrimLeft($sFilePath, StringInStr($sFilePath, '', 2, -1)) & @CRLF)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Building on M23's example

#include<array.au3>

local $file_list = '"C:picture1.jpg, C:picture2.jpg", D:anyPathpicture3.jpg, "Z:AndAnotherOnepicture4.jpg"'
local $file_names_array = stringregexp($file_list,'(?i)(?U).*(w+.w+)[",]',3)

_arraydisplay($file_names_array)

note - I do NOT know how to get the last entry if the final quote is missing.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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
  • Recently Browsing   0 members

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