Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extracting filename from a text and putting it in a  new text file

Posted on 2014-04-16
11
Medium Priority
?
266 Views
Last Modified: 2014-04-18
I need to extract the filenames 09103ea6-16d8-461f-955e-97bddc17b0ed.pdf and 09103ea6-16d8-461f-955e-97bddc17wewed.pdf and put them into a new textfile
as:
09103ea6-16d8-461f-955e-97bddc17b0ed.pdf
09103ea6-16d8-461f-955e-97bddc17wewed.pdf


*WARNING* Data file ["F:\Data2014\09103ea6-16d8-461f-955e-97bddc17b0ed.pdf"] was not found or subfolder ["BYAM"] not created.
*WARNING* Data file ["F:\Data2014\09103ea6-16d8-461f-955e-97bddc17wewed.pdf"] was not found or subfolder ["GAM"] not created.
0
Comment
Question by:LuckyLucks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 41

Expert Comment

by:footech
ID: 40005682
Extract them from what, exactly?  Please describe your scenario in more detail.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40005685
Try this:
get-content file.txt | %{$_ -replace ".*\\([\w-]+\.pdf).*", "$1" } | out-file new.txt

Open in new window

HTH,
Dan
0
 
LVL 17

Expert Comment

by:Learnctx
ID: 40006156
This works for me. Dan's wasn't working for me in PowerShell 4 at least because of the double quotations.

Get-Content inputfile.txt | Foreach-Object {$_ -replace ".*\\([\w\-]+\.pdf).*", '$1'} | out-file newfile.txt

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 41

Expert Comment

by:footech
ID: 40007098
No points for me, but just an FYI.  Yes, you have to use one of the following:
'$1'
"`$1"
0
 

Author Comment

by:LuckyLucks
ID: 40007175
I could have other extensions like xls, XLS, xlsm, doc, docx, DOC, DOCX, htm, HTM, msg. Infact if you could modify it so that the extension is irrelevant as long as its contained within the first [].
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40007199
get-content file.txt | %{$_ -replace ".*\\([\w-]+\.\w+)\"\].*", '$1' } | out-file new.txt

Open in new window

0
 
LVL 41

Expert Comment

by:footech
ID: 40007265
Depending on how much variation there is in the extension, or even if there's no extension at all, you may want to use the following.
Get-Content file.txt | Foreach-Object {$_ -replace '.*\\([\w .-]+?)"].*', '$1'} | out-file new.txt

Open in new window

0
 

Author Comment

by:LuckyLucks
ID: 40007319
It seems to just hang with a >> (unlike the previous line with .pdf)
0
 
LVL 41

Expert Comment

by:footech
ID: 40007388
Mine doesn't.
0
 
LVL 17

Accepted Solution

by:
Learnctx earned 2000 total points
ID: 40007949
I'm assuming while the file extension will change change nothing else will. If so just match on any character after the period.

Get-Content inputfile.txt | Foreach-Object {$_ -replace ".*\\([\w\-]+\.\w+).*", '$1'} | out-file newfile.txt

Open in new window

0
 

Author Closing Comment

by:LuckyLucks
ID: 40008963
thanks to all.
0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question