Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Fixed arithmetic overflow in DirectoryCleanser.vb
Browse files Browse the repository at this point in the history
  • Loading branch information
Laim committed Mar 29, 2021
1 parent 156747e commit 90ffc56
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
45 changes: 43 additions & 2 deletions Source Code/Classes/DirectoryCleanser.vb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
End Function

Public Shared Function DirFileSize(ByVal DirPath As String, ByVal FileType As String)
Dim FileSize As Integer = 0
Dim FileSize As Int64 = 0
For Each item As IO.FileInfo In New IO.DirectoryInfo(DirPath).GetFiles()
If item.Extension = "." + FileType Or FileType = "" Then
FileSize += item.Length
End If
Next

Return FileSize
Return FormatFileSize(FileSize)
End Function

Public Shared Function DirFileList(ByVal DirPath As String, ByVal FileType As String)
Expand All @@ -37,4 +37,45 @@

Return FileList.ToString
End Function

'' Credit: https://stackoverflow.com/a/39449400
Public Shared Function FormatFileSize(ByVal lngFileSize As Long) As String

Dim x As Int64 : x = 0
Dim Suffix As String : Suffix = ""
Dim Result As Single : Result = lngFileSize

Do Until Int(Result) < 1000
x = x + 1
Result = Result / 1024
Loop

Result = Math.Round(Result, 2)

Select Case x
Case 0
Suffix = "Bytes"
Case 1 'KiloBytes
Suffix = "KB"
Case 2 'MegaBytes
Suffix = "MB"
Case 3 'GigaBytes
Suffix = "GB"
Case 4 'TeraBytes
Suffix = "TB"
Case 5 'PetaBytes
Suffix = "PB"
Case 6 'ExaBytes
Suffix = "EB"
Case 7 'ZettaBytes
Suffix = "ZB"
Case 8 'YottaBytes
Suffix = "YB"
Case Else
Suffix = "Too big to compute :)"
End Select

FormatFileSize = Format(Result, "#,##0.00") & " " & Suffix

End Function 'FormatFileSize
End Class
2 changes: 1 addition & 1 deletion Source Code/Forms/frmFileMover.vb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Public Class frmFileMover
Try
txtFolderStats.Text = ""
txtFolderStats.Text = "Old Directry File Count : " + DirFileCount(txtOldDirectory.Text, txtFileType.Text).ToString + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "Old Directory File Size : " + DirFileSize(txtOldDirectory.Text, txtFileType.Text).ToString + " bytes" + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "Old Directory File Size : " + DirFileSize(txtOldDirectory.Text, txtFileType.Text).ToString + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "New Directry Current File Count : " + DirFileCount(txtNewDirectory.Text, txtFileType.Text).ToString + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "New Directory Current File Size : " + DirFileSize(txtNewDirectory.Text, txtFileType.Text).ToString + " bytes"
Catch ex As Exception
Expand Down
2 changes: 1 addition & 1 deletion Source Code/Forms/frmFileRemover.vb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Public Class frmFileRemover
Try
txtFolderStats.Text = ""
txtFolderStats.Text = txtFolderStats.Text + "Directry File Count : " + DirFileCount(txtDirectory.Text, txtFileType.Text).ToString + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "Directory File Size : " + DirFileSize(txtDirectory.Text, txtFileType.Text).ToString + " bytes" + vbCrLf
txtFolderStats.Text = txtFolderStats.Text + "Directory File Size : " + DirFileSize(txtDirectory.Text, txtFileType.Text).ToString + vbCrLf
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Expand Down
6 changes: 3 additions & 3 deletions Source Code/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("Laim McKenzie")>
<Assembly: AssemblyProduct("Directory Cleanser")>
<Assembly: AssemblyCopyright("Copyright © Laim McKenzie. 2019")>
<Assembly: AssemblyCopyright("Copyright © Laim McKenzie 2021")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>
Expand All @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("1.1.1.0")>
<Assembly: AssemblyFileVersion("1.1.1.0")>
<Assembly: AssemblyVersion("1.2.0.0")>
<Assembly: AssemblyFileVersion("1.2.0.0")>

0 comments on commit 90ffc56

Please sign in to comment.