Powershell Script to Export Custom Directory Listing to CSV

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:

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.
LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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="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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
dbbishopAuthor Commented:

Yes, csv formatted file, no header, already created.
Call it filedescriptions.txt
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

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?
Subsun, I think your code does exactly what he's asking for.
dbbishopAuthor Commented:

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 :-)
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

dbbishopAuthor Commented:
Fantastic. I think there was an extra '}' in the last solution, but other than that, works like a charm!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.