Solved

Get info from files

Posted on 2016-08-22
3
24 Views
Last Modified: 2016-10-13
Ive been trying to figure a way of getting access to all files in a subdirectory, and then loop through. Ive found examples where it will just dump out the files per line, however what I want is to go through each file and get at:-
  • FileName
  • Directory
  • FileSize
  • Created
  • LastModified

The idea is then to export them into a database table and then do some queries on them.

Any easy suggestions on how to do a foreach loop for each file in a directory (recursive to include sub-directories).

Im assuming to do this as a function and then call the function for each sub-directory, but struggeling to get the array of files.
0
Comment
Question by:tonelm54
3 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
No need for a function.
Assuming PS 3.0 or later:
Get-ChildItem C:\Temp -Recurse -File | Select-Object -Property Name, DirectoryName, Length, CreationTime, LastWriteTime

Open in new window

PS 2.0:
Get-ChildItem C:\Temp -Recurse -File | Where-Object {!$_.PsIsContainer} | Select-Object -Property Name, DirectoryName, Length, CreationTime, LastWriteTime

Open in new window

To export the results to a csv, add this at the end of the line above:
 | Export-Csv -Path C:\Temp\FileList.csv -NoTypeInformation

Open in new window

1
 
LVL 1

Assisted Solution

by:alohadin
alohadin earned 250 total points
Comment Utility
And if you want the File Size in kilobytes:
Get-ChildItem C:\Temp -Recurse -File | Select-Object -Property Name, DirectoryName, @{Name="Kbytes";Expression={"{0:n2} kB" -f ($_.Length / 1Kb)}} , CreationTime, LastWriteTime

Open in new window


Or in Megabytes:
Get-ChildItem C:\Temp -Recurse -File | Select-Object -Property Name, DirectoryName, @{Name="File Size";Expression={"{0:n2} MB" -f ($_.Length / 1MB)}} , CreationTime, LastWriteTime

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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