Solved

Powershell Script to Export Custom Directory Listing to CSV

Posted on 2014-11-19
8
163 Views
Last Modified: 2014-11-20
Okay, I can come up with the follwoing code and it works:
$now=Get-Date -format "yyyyMMdd"
Get-ChildItem "G:\Import\DataIn\*.*_$now`*.*" | Sort-Object fullname | Select CreationTime, FullName, Length | Export-Csv -Force -NoTypeInformation "G:\Import\Logs\Filelist$now.csv"

Open in new window

However, in my output, I want to separate the date and time into two separate columns. Furthermore, I want to have a separate file that contains two 'fields' and will be in the format of filename.filetype, description. The filename.filetype portion will match the *.* above (e.g. everything from the start of the filename, up to the first '.', and following, up to the first underscore, and I want to include the value of description from the file.
So, if I have a directory that contains the following filenames:
input_file1.txt_20141119_041203
input_file2.txt_20141119_041214
input_file3.txt_20141119_041240

And my 'description' file contains:
input_file1.txt,The description for file1
input_file2.txt,The description for file2
input_file3.txt,The description for file3

And I run this code on 11.19.2014, the expected output into csv format should be:
"yyyy/mm/dd", "hh:mm:ss","input_file1.txt_20141119_041203","The description for file1","filesize"
"yyyy/mm/dd", "hh:mm:ss","input_file2.txt_20141119_041214","The description for file2","filesize"
"yyyy/mm/dd", "hh:mm:ss","input_file3.txt_20141119_041240","The description for file3","filesize"

Where yyyy/mm/dd represents the file creation date of each file, hh:mm:ss represents the file creation time of each file, "filesize" represents the file size of each file and "The description for file#" is the description (second) field of the "description" file after matching "input_file#.txt" portion of the filename (example actual filenames are: "acctstd.txt_yyyymmdd_hhmmss", "TOS_SAN.xml_yyyymm_hhmmss", "sect298.html_yyyymm_hhmmss", in which case, the "description file would contain entries for "acctstd.txt", "TOS_SAN.xml" and "sect298.html"). Hope this is clear.

p.s. If I have a file that does not have a corresponding match in the 'description' file, I would just want to include "unknown" for the Description.
0
Comment
Question by:dbbishop
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40453296
I assume your file description details are saved in a csv file and doe snot have any header. If yes try the following code..
Replace the path C:\Description.csv with the file path which contain the file description information.
$disc = @{}
Import-Csv C:\Description.csv -Header Name,Disc | foreach { $disc[$_.Name] = $_.Disc }

$now=Get-Date -format "yyyyMMdd"
Get-ChildItem "G:\Import\DataIn\*.*_$now`*.*" | Sort-Object fullname | Select `
 @{N="CreationDate";E={$_.CreationTime.tostring("yyyy/MM/dd")}},`
 @{N="CreationTime";E={$_.CreationTime.tostring("HH:mm:ss")}},`
 @{N="Description";E={$disc.($_.Name -replace "_\d{8}_\d{6}")}},`
FullName, Length | 
Export-Csv -Force -NoTypeInformation "G:\Import\Logs\Filelist$now.csv"

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 40453299
Here's how you would split the date into two fields.
Get-ChildItem "G:\Import\DataIn\*.*_$now`*.*" | Sort-Object fullname | Select @{n="Date";e={(Get-Date $_.CreationTime -f "MM/dd/yyyy")}}, @{n="Time";e={(Get-Date $_.CreationTime -f "HH:mm:ss")}}, FullName, Length | Export-Csv -Force -NoTypeInformation "G:\Import\Logs\Filelist$now.csv"

Open in new window


For the second part you say
I want to have a separate file that contains two 'fields'...
Are you saying you already have this file?
And what you want is to include information from this file in the output from the above command?
0
 
LVL 15

Author Comment

by:dbbishop
ID: 40453467
footech,

Yes, csv formatted file, no header, already created.
Call it filedescriptions.txt
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40453493
Hmm.. I think I misread the question.. Are you reading the description from each file? or you want to include the description in report which is pulled from another file?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:footech
ID: 40453783
Subsun, I think your code does exactly what he's asking for.
0
 
LVL 15

Author Comment

by:dbbishop
ID: 40453864
Subsun,

Very minor changes (e.g. use Name instead of FullName for filename and changed order of output). and other minor changes, like timestamp included 1000ths of seconds (e.g. _20141191_041134032, so had to revise the regex expression, but otherwise "mostly" works. Can you think of an easy way to output "Unknown" for description if the entry is not matched in the description table?

Sorry about the confusion above, you made the statement "I assume your file description details are saved in a csv file and doe snot have any header." and I responded to footech :-)
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 40453939
Lol..  I have seen in some questions people call footech as subsun.. :-D

To add Unknown try replacing line 8 with following..
@{N="Description";E={$out = $disc.($_.Name -replace "_\d{8}_\d{6}");if ($out -ne $null){$out}else{"Unknown"})}}`

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40455178
Fantastic. I think there was an extra '}' in the last solution, but other than that, works like a charm!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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