Solved

Powershell Script to Export Custom Directory Listing to CSV

Posted on 2014-11-19
8
170 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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