Skip to content

Instantly share code, notes, and snippets.

@saj1th
Last active April 6, 2021 00:24
Show Gist options
  • Save saj1th/636afe1c27c7eab4a4460d9868238b0f to your computer and use it in GitHub Desktop.
Save saj1th/636afe1c27c7eab4a4460d9868238b0f to your computer and use it in GitHub Desktop.
def byteFormat(fileSize: Long): String = {
if (fileSize <= 0) return "0 B"
val units: Array[String] = Array("B", "kB", "MB", "GB", "TB", "PB", "EB")
val grp: Int = (Math.log10(fileSize) / Math.log10(1024)).toInt
f"${fileSize / Math.pow(1024, grp)}%3.3f ${units(grp)}"
}
def computeFileStats(path: String): (String, String) = {
val formatter = java.text.NumberFormat.getIntegerInstance
var (bytes, count) = (0L, 0L)
import scala.collection.mutable.ArrayBuffer
val files = ArrayBuffer(dbutils.fs.ls(path): _*)
while (!files.isEmpty) {
val fileInfo = files.remove(0)
if (!fileInfo.isDir) {
count += 1
bytes += fileInfo.size
} else {
files.append(dbutils.fs.ls(fileInfo.path): _*)
}
}
(formatter.format(count), byteFormat(bytes))
}
val (filesCount, folderSize) = computeFileStats(yourPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment