Mar 5, 2024 Replies: 26 Author: Erel Note that it ignores subfolders. B4X: Sub SizeOfFilesInFolder(f As String) As Long Dim total As Long For Each filename As String In File.ListFiles(f) If File.IsDirectory(f, filename) = False Then total = total + File.Size(f, filename) End If Next Return total End Sub Click to expand...
Author: Erel Note that it ignores subfolders. B4X: Sub SizeOfFilesInFolder(f As String) As Long Dim total As Long For Each filename As String In File.ListFiles(f) If File.IsDirectory(f, filename) = False Then total = total + File.Size(f, filename) End If Next Return total End Sub
LucaMs Expert Licensed User Longtime User Mar 7, 2024 #21 LucaMs said: I ask miss ChatGPT (right, @ilan? ) Click to expand... As usual it got it all wrong!
Daestrum Expert Licensed User Longtime User Mar 7, 2024 #22 I will try and help Code: #if java import java.io.*; import java.nio.file.*; public static long folderSize(String pth) throws IOException { Path folder = Paths.get(pth); long size = Files.walk(folder) .filter(p -> p.toFile().isFile()) .mapToLong(p -> p.toFile().length()) .sum(); return size / 1_048_576; // (MiB) } #end if Is called from B4J as Code: ' various ways to do this Dim myFolderSize As Long = (Me).As(JavaObject).RunMethod("folderSize",Array("yourPath")) If you want to see under 1MiB folders change the return line to Code: return (size < 1_048_576) ? size : (size / 1_048_576); Last edited: Mar 7, 2024
I will try and help Code: #if java import java.io.*; import java.nio.file.*; public static long folderSize(String pth) throws IOException { Path folder = Paths.get(pth); long size = Files.walk(folder) .filter(p -> p.toFile().isFile()) .mapToLong(p -> p.toFile().length()) .sum(); return size / 1_048_576; // (MiB) } #end if Is called from B4J as Code: ' various ways to do this Dim myFolderSize As Long = (Me).As(JavaObject).RunMethod("folderSize",Array("yourPath")) If you want to see under 1MiB folders change the return line to Code: return (size < 1_048_576) ? size : (size / 1_048_576);
MrKim Well-Known Member Licensed User Longtime User Mar 11, 2024 #23 LucaMs said: Windows 7 Explorer. When I hover the mouse cursor over a folder, sometimes a popup appears giving me the size of it but not always . So I'm afraid I'll have to develop a little (B4J) tool that lists the folder names and their size. Click to expand... Right click and select properties will give you the size.
LucaMs said: Windows 7 Explorer. When I hover the mouse cursor over a folder, sometimes a popup appears giving me the size of it but not always . So I'm afraid I'll have to develop a little (B4J) tool that lists the folder names and their size. Click to expand... Right click and select properties will give you the size.
LucaMs Expert Licensed User Longtime User Mar 11, 2024 #24 MrKim said: Right click and select properties will give you the size. Click to expand... Thank you. I know, but if you have 30 subfolders or more, it becomes a long process.
MrKim said: Right click and select properties will give you the size. Click to expand... Thank you. I know, but if you have 30 subfolders or more, it becomes a long process.
MrKim Well-Known Member Licensed User Longtime User Mar 11, 2024 #25 LucaMs said: Thank you. I know, but if you have 30 subfolders or more, it becomes a long process. Click to expand... Take a look at this: https://windirstat.net/ free and open source. I have used it in the past.... Unless, of course, you are looking for an excuse to have the fun of "rolling your own" ?
LucaMs said: Thank you. I know, but if you have 30 subfolders or more, it becomes a long process. Click to expand... Take a look at this: https://windirstat.net/ free and open source. I have used it in the past.... Unless, of course, you are looking for an excuse to have the fun of "rolling your own" ?
LucaMs Expert Licensed User Longtime User Mar 11, 2024 #26 MrKim said: Unless, of course, you are looking for an excuse to have the fun of "rolling your own" ? Click to expand... Yes, it's a "good" reason to develop a small tool (which only I will use, everything already works for you, surely, with Windows 10+). Actually I should modify, improve lmB4XTableEx; well, I'll do both, although not today.
MrKim said: Unless, of course, you are looking for an excuse to have the fun of "rolling your own" ? Click to expand... Yes, it's a "good" reason to develop a small tool (which only I will use, everything already works for you, surely, with Windows 10+). Actually I should modify, improve lmB4XTableEx; well, I'll do both, although not today.
P PaulMeuris Well-Known Member Licensed User Mar 12, 2024 #27 Using a recursive sub in B4J will do the same as the Java code @Daestrum showed. After a few tests i have found that the Java code is a little bit faster. In the Java code there is no test for the permissions (java.nio.file.AccessDeniedException). The first calculation takes longer than the second calculation for the same folder. Here's the compact version in B4J without the extra tests for comparison. B4X: Private Sub get_size_of_folder(folder As String) As Long Dim lst As List = File.ListFiles(folder) Dim fsize As Long For i = 0 To lst.Size-1 If File.IsDirectory(folder, lst.Get(i)) Then Dim subfolder As String = folder & "\" & lst.Get(i) fsize = fsize + get_size_of_folder(subfolder) Else fsize = fsize + File.Size(folder, lst.Get(i)) End If Next Return fsize End Sub Calculating the size of a root (drive) folder (c:\ or e:\) will take a long time... Last edited: Mar 12, 2024
Using a recursive sub in B4J will do the same as the Java code @Daestrum showed. After a few tests i have found that the Java code is a little bit faster. In the Java code there is no test for the permissions (java.nio.file.AccessDeniedException). The first calculation takes longer than the second calculation for the same folder. Here's the compact version in B4J without the extra tests for comparison. B4X: Private Sub get_size_of_folder(folder As String) As Long Dim lst As List = File.ListFiles(folder) Dim fsize As Long For i = 0 To lst.Size-1 If File.IsDirectory(folder, lst.Get(i)) Then Dim subfolder As String = folder & "\" & lst.Get(i) fsize = fsize + get_size_of_folder(subfolder) Else fsize = fsize + File.Size(folder, lst.Get(i)) End If Next Return fsize End Sub Calculating the size of a root (drive) folder (c:\ or e:\) will take a long time...