Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
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 '12 at 14:18

$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!

share|improve this answer

You must log in to answer this question.

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