Solved

Get info from files

Posted on 2016-08-22
3
30 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
[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 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 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

1
 
LVL 2

Assisted Solution

by:alohadin
alohadin earned 250 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

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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…
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…

730 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