Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

$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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.