Solved

Get info from files

Posted on 2016-08-22
3
29 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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