Solved

split file path and names into "fields" and create csv file with list

Posted on 2011-02-16
2
671 Views
Last Modified: 2012-05-11
I am looking to do a directory listing of files and split up the path and file names into fields and create a csv file to import into a document mangement system - so from this dir list:
d:\ap\pcard\fax\mary_1234.tif
d:\ap\pcard\fax\joe_1342.tif
d:\ap\pcard\email\john_2468.pdf
d:\ap\pcard\scan\jill_2222.pdf
I would want this file:
date, source, name, acct#, file path
2/17/2011,fax,mary,1234,d:\ap\pcard\fax\mary_1234.tif
2/17/2011,fax,joe,1342,d:\ap\pcard\fax\joe_1342.tif
2/17/2011,email,john,2468,d:\ap\pcard\email\john_2468.pdf
etc
--
I have parts of this figured out but can't seem to put it all together.
0
Comment
Question by:saladoil
2 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34909838
Hi there,

Is that directory structure entirely predictable? Never deviates from the format you describe?

And the date in the first field, where'd that come from? I'm going to assume it's last write time for the purposes of writing an example :)

Lots of Regular Expressions here, if the path varies this might fall apart a bit.
Get-ChildItem d:\ap\pcard -Recurse |
  Where-Object { !$_.PsIsContainer } |
  Select-Object `
    @{n='Date';e={ $_.LastWriteTime }},
    @{n='Source';e={ $_.FullName -Replace '^([^\\]*\\){3}|\\.+$' }},
    @{n='Name';e={ (Split-Path $_.FullName -Leaf) -Replace '_.*' }},
    @{n='Acct';e={ $_.FullName -Replace '.*_|\..*' }},
    FullName

Open in new window

Chris
0
 

Author Closing Comment

by:saladoil
ID: 34910353
great - just had to add |export-csv d:\ap\temp\out.csv to wrtie it to a file.
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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