Write Folder Names to Text Files

Tagged: ,

This topic contains 6 replies, has 2 voices, and was last updated by  doit_PS 4 years, 3 months ago.

  • Author
    Posts
  • #6737

    doit_PS
    Participant

    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!

  • #6814

    Poshoholic
    Participant

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

  • #6816

    doit_PS
    Participant

    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.

  • #6831

    Poshoholic
    Participant

    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.

  • #6896

    doit_PS
    Participant

    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.

  • #6903

    Poshoholic
    Participant

    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.

  • #6954

    doit_PS
    Participant

    Great thanks! It works.

You must be logged in to reply to this topic.