Last active
April 6, 2021 00:24
-
-
Save saj1th/636afe1c27c7eab4a4460d9868238b0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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