Solved

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

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

Expert Comment

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

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 34

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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