2

Using PowerShell, how can I get a list of file names within a string, if I only know the extension?

Simplified example:

C:\> $e = "CN=Ken Myer world.jpg more text."
C:\> $f = $e.IndexOf(".jpg")
C:\> write-host $f
17

How do I get the rest of the unknown file name? (Assuming a 5 character file name) I can not plug in a negative number into $e.Substring(17,-5,9) <--does not work.

1
  • RegEx will help here, but you need to figure out a recurring pattern to match. For example, the file names can have spaces but are all terminated by ".jpg" obviously. If they all have "=" before them, you can construct a suitable RegEx easily. See the following pages for help: 1, 2, 3.
    – Karan
    Oct 21, 2012 at 14:18

1 Answer 1

0

$regex = '\b[A-Za-z0-9._%-+]+.jpg'

select-string -Path $DL_file -Pattern $regex -AllMatches | % { $.Matches } | % { $.Value } > $CurrentFileNames

Seems to work for my situation.

Thanks!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .