Solved

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

Posted on 2014-04-16
11
247 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
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
 
LVL 39

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 39

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 39

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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