Solved

Powershell Script to Export Custom Directory Listing to CSV

Posted on 2014-11-19
8
165 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
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 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

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

25 Experts available now in Live!

Get 1:1 Help Now