Solved

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

Posted on 2014-04-16
11
246 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
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
Extract them from what, exactly?  Please describe your scenario in more detail.
0
 
LVL 34

Expert Comment

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

Open in new window

HTH,
Dan
0
 
LVL 16

Expert Comment

by:Learnctx
Comment Utility
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
 
LVL 39

Expert Comment

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

Author Comment

by:LuckyLucks
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 34

Expert Comment

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

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
It seems to just hang with a >> (unlike the previous line with .pdf)
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Mine doesn't.
0
 
LVL 16

Accepted Solution

by:
Learnctx earned 500 total points
Comment Utility
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
Comment Utility
thanks to all.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
This article will help you understand what HashTables are and how to use them in PowerShell.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now