Author Posts

May 21, 2013 at 5:55 am

Hello Powershell Expert,

I have a question on how to write folder names to external text files based on their total size. Suppose there are lots of folders in a parent folder and I'd like to write all these subfolders' names to text files; in each text files, the total size of all the folders listed there should be less than  a fix amount of size, for example, 4GB.

To make it clear, think about a simple case: suppose there are 4 subfolders in a parent folder, C:\

C:\a

C:\b

C:\c

C:\d

with size 1G, 3.5G, 1G, 1G respectively. We know subfolder C:\b can't be grouped with any others otherwise their total size will be more than 4GB. So, we can write all these subfolders' names sequentially to 3 text files :

name1.txt contains:

C:\a

name2.txt contains:

C:\b

name3.txt contains:

C:\c

C:\d

How can I make it? Thanks very much!

May 22, 2013 at 4:35 am

This is definitely possible, but I can't help but ask: why would you want to do such a thing?

May 22, 2013 at 5:26 am

Thanks for your reply.

We can think about a scenario that we move all subfolders to several folders then zip respectively, with limitation of 4GB for each grouped folder.

Now, if we don't want to move such subfolders physically, we can create separated text files to control it; each text file just contains the subfolders which will be zipped together.

Thank you for any comments, hints and snippets.

May 22, 2013 at 9:38 am

Ok, can you share what you have come up with so far? These forums work best when you come in with what you have been working on and ask for questions on how to take it to the next step or get help with errors you are having.

May 23, 2013 at 11:46 am

Thanks! I came out some codes but only one subfolder written to name1.txt:

`$input  = "C:"
$output = "D:"

$size = 4GB

$i=1
$flag = $true
$items = dir -Path $input

foreach($item in $items){
    if($flag -eq $true){
       Add-Content $output\name$i.txt  $item.FullName
        $flag = $false
    }

    $folder_Size = dir -Path $item.FullName -Recurse|Measure-Object length -sum

    if(($folder_Size.sum + (gci $item.FullName -recurse |Measure-Object length -Sum).Sum) -ge $size){
       $i++
       Add-Content $output\name$i.txt  $item.FullName
    }

    if($folder_Size.sum -lt $size){}
    else{
        $flag = $true
        $i++
    }
}`

Could you help me to figure out where the logic error is? Thanks.

May 23, 2013 at 12:20 pm

You're taking an approach similar to what I would have quickly thrown together. You have a logic error in your size comparison right now though because you calculate the size of the folder and then you check to see if twice that size is greater than 4GB. If so, you add it. See lines 16-21 in your sample above.

You should probably add another variable called $sizeSoFar (initialized to 0), that is used to track the total size added to the current file. Then you could do something like this:

$sizeSoFar = 0
foreach ($item in $items) {
$folder_Size = dir -Path $item.FullName -Recurse | Measure-Object -Sum
if ($folder_Size -gt $size) {
Write-Error "Folder too big! $($item.FullName) is greater than $size!"
continue
}
if ($folder_Size.Sum + $sizeSoFar -gt $size) {
$i++
$sizeSoFar = 0
}
$sizeSoFar += $folder_Size.Sum
Add-Content $output\name$i.txt $item.FullName
}

Note, I haven't tested this, I just share it as an example of how you might use it to get the job done.

May 24, 2013 at 10:51 am

Great thanks! It works.