FileSelection/FileSelection.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#region Variables
$maxsize = -1
$items
$bins
#endregion

#region Helper functions
function GetItems()
{
    Param(
        [String]$Path
        )

    $script:items = @()

    $its = Get-ChildItem -Path $Path
    foreach($it in $its)
    {
        $newItem = New-Object PSObject
        $newItem | Add-Member -MemberType NoteProperty -Name Name -Value $it.Name
        $newItem | Add-Member -MemberType NoteProperty -Name FullName -Value $it.FullName
        $size = (Get-Size $it.FullName ).MB
        $newItem | Add-Member -MemberType NoteProperty -Name Size -Value $size
        $script:items += $newItem
    }
}

# Function to add new bin
function addBin()
{
    $bin = New-Object PSObject
    $bin | Add-Member -MemberType NoteProperty -Name Items -value (New-object System.Collections.Arraylist)
    $bin | Add-Member -MemberType NoteProperty -Name Size -Value 0
    $script:bins += $bin
}

function NextFit()
{
    $script:bins = @()

    $binIndex = 0;

    addBin

    foreach($i in $script:items)
    {
        $freeBinSpace = $script:maxsize - $script:bins[$binIndex].size
        if( $freeBinSpace -ge $i.Size)
        {
            $script:bins[$binIndex].Items.Add($i.FullName) > $null
            $script:bins[$binIndex].size += $i.size
        }
        else
        {
            addBin
            $binIndex ++

            $script:bins[$binIndex].Items.Add($i.FullName) > $null
            $script:bins[$binIndex].size += $i.size
        }
    }

}

function FirstFit()
{
    $script:bins = @()
    
    $binIndex = 0;

    addBin

    $added = 0

    foreach($i in $script:items)
    {
        for( $b=0; $b -lt $script:bins.Count; $b++)
        {
            $freeBinSpace = $script:maxsize - $script:bins[$b].size
            if( $freeBinSpace -ge $i.Size)
            {
                $script:bins[$b].Items.Add($i.FullName) > $null
                $script:bins[$b].size += $i.size
                $added = 1
                break
            }
        }

        if( $added -ne 0)
        {
            addbin
            $script:bins[$script:bins.Count-1].Items.Add($i.FullName) > $null
            $script:bins[$script:bins.Count-1].size += $i.size
        }
    }

}

function BestFit()
{
    $script:bins = @()

    # Add the first bin
    addBin

    # Variables to hold the best fit bin
    $binwithminimumemptyspace = -1
    $minimumemptyspace = $maxsize

    # Loop through files/folders
    foreach($i in $script:items)
    {
        # Loop through bins for specific file/folder
        $binwithminimumemptyspace = -1
        $minimumemptyspace = $maxsize

        for( $b=0; $b -lt $script:bins.Count; $b++)
        {
            # Get bin's free space
            $freeBinSpace = $script:maxsize - $script:bins[$b].size

            # Reject bin if free space is less than needed
            if( ($freeBinSpace -lt $i.size) -or ($freeBinSpace -lt 0) )
            {
                continue
            }

            # Compare to the bin where the item fits best
            if( ($freeBinSpace - $i.size) -lt $minimumemptyspace )
            {
                $binwithminimumemptyspace = $b
                $minimumemptyspace = ($freeBinSpace - $i.size)
            }
        }


        # Check if bin is found or we need to add new bin
        if( $binwithminimumemptyspace -lt 0 )
        {
            addBin
            $binwithminimumemptyspace = ($script:bins.Count - 1)
        }

        # Add item to the bin
        $script:bins[$binwithminimumemptyspace].Items.Add($i.FullName) > $null
        $script:bins[$binwithminimumemptyspace].size += $i.size
    }
}
#endregion

#region FileSelection
Function FileSelection()
{
    Param(
        [Parameter(Mandatory=$true,Position=1)]
        [String]$Path,
        [ValidateSet("BestFit","FirstFit","NextFit","All")] 
        [String]$Method = "BestFit",
        [Parameter(Mandatory=$true,Position=2)]
        [Int]$MaximumSize,
        [switch]$Seperate
    )

    if( -Not(Test-Path $Path))
    {
        Write-Error "Could not locate source directory."
        return
    }

    $script:maxsize = $MaximumSize

    GetItems -Path $Path

    if( $Method -eq "BestFit" )
    {
        BestFit
    }
    if( $Method -eq "NextFit")
    {
        NextFit
    }
    if( $Method -eq "FirstFit")
    {
        FirstFit
    }

    if( $Method -eq "All")
    {
        NextFit
        $nextfitbins = $Script:bins

        BestFit
        $bestfitbins = $Script:bins

        FirstFit
        $firstfitbins = $Script:bins

        $bestsolution = $bestfitbins

        if( $nextfitbins.Count -lt $bestsolution.Count )
        {
            $bestsolution = $nextfitbins
        }

        if( $firstfitbins.Count -lt $bestsolution.Count )
        {
            $bestsolution = $firstfitbins
        }
    }

    if( $Seperate )
    {
        $bincounter = 0
        $fullpath = (Resolve-Path $Path).Path + "\"

        for($j=0; $j -lt $Script:bins.Count; $j++)
        {
            $bincounter ++
            $directoryname = ($fullpath + "Folder " + $bincounter)

            New-Item -ItemType directory -Path $Path -Name ("Folder " + $bincounter)

            foreach($i in $bins[$j].items)
            {
                Move-Item -Path $i -Destination $directoryname -Force
            }
        }
    }
    else
    {
        $bins | Select-Object -Property Items, Size
    }
}
#endregion

#region Exports
Export-ModuleMember -Function FileSelection
#endregion