[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Get info from files

Posted on 2016-08-22
Medium Priority
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.
Question by:tonelm54
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
LVL 85

Accepted Solution

oBdA earned 1000 total points
ID: 41765108
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


Assisted Solution

alohadin earned 1000 total points
ID: 41765142
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


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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