Solved

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

Posted on 2014-04-16
11
255 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 40

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 40

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 40

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 40

Expert Comment

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

Accepted Solution

by:
Learnctx earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

732 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