Solved

Powershell Script to Export Custom Directory Listing to CSV

Posted on 2014-11-19
8
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 40

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 40

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

729 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