• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

Get info from files

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
tonelm54
Asked:
tonelm54
2 Solutions
 
oBdACommented:
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
 
alohadinSECommented:
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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now