0

Somewhat confused as what I need to use to acheive my goal. Here is what I need to do:

  • Recursively, get a list of files
  • Get the filename, lastwritedate, owner and append to an array
  • When the array reaches 500, output it to a csv filename.csv. Have a file for each 500 line output
  • Continue on, outputting files with details (500 lines) until end

I'm not conceptually grasping if I need a recursive function or can do some tricky while/do/for loop work.

Here is what I have so far:

$array = @()
$files = ls c:\users –recurse
$counter = 0
$filename = 0
foreach ($file in $files){
   if($counter -ne 500){
        write-host "Doing loop, counter is " $counter
        $owner = Get-Acl $file.FullName
        $data = new-object -type PSObject
        $data | add-member -membertype noteproperty -name "File" -value $file.FullName
        $data | add-member -membertype noteproperty -name "LastWriteTime" -value $file.LastWriteTime
        $data | add-member -membertype noteproperty -name "Created" -value $file.CreationTime
        $data | add-member -membertype noteproperty -name "Owner" -value $file.Owner
        $array += $data
        $counter ++    
   else($counter -eq 500){
        $counter = 0
        $array = @()
        $array | export-csv "c:\temp\filelist\file-$filename.csv"
        $filename ++
        }       
   }
}

I've actually tried a bunch of different things, loop types etc. I just cant quiet figure out how to get the script to "goto start of for loop" as my understanding isn't there yet.

The reason I'm limiting to 500 lines is because this will run on a fileserver with millions of files, and a simple get-childitem wasn't quiet handling it gracefully.

I'm happy to not be given the direct answer, but given some hints as to what I need to do would be great (this isn't homework, I like learning powershell ha :))

1 Answer 1

1

I think I would nest a For loop within another For loop do to what you want. Something like:

For($x=0;$x -lt $Files.count;$x=$x+500){
    $Array=@()
    For($y=0;$y -lt 500; $y++){
        $Array += $Files[($x+$y)] | Select FullName,LastWriteTime,CreationTime,@{l='Owner';e={($_|Get-ACL).Owner}}
    }
    $Array|Export-CSV C:\Path\To\Export-$x.csv -NoType
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I will have a study of this to understand what you did.... ...Let me get this straight... - You ran a for loop for the number of all files. It incriments up in 500. This sets the empty array per 500.... - You then did a foreach on basically each int. in the 500 (so 500 interations).... - For one single interation, you append the data to the array.... - Once the 500 is cimplete, append to the CSV.... Is that right? Thanks!....
Almost all correct, very well followed in general. I do not append, I actually generate multiple files. You could append, but then your CSV may get very unwieldy. Right now it generates file like Export-500.csv, Export-1000.csv, Export-1500.csv and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.