473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to check total # of files and folders

As the subject says how can I check total # of files and folders in a particular folder and determine the total size of that folder. I will be using VB.NET. A small code snippet will be great.
Thanks
Jul 25 '08 #1
10 6712
Curtis Rutland
3,256 Expert 2GB
No code snippet, but an explanation:

To do this right, you need to use the System.IO namespace, so for VB.NET, you need to put Imports System.IO at the very top of the page.

The two objects that will be most useful to you are DirectoryInfo and FileInfo. DirectoryInfo can get all files and all directories in a particular directory. However, this will only get the top level. If you want to get all the files in all the subdirectories, you will need to write a recursive function. You should look up recursion if you aren't familiar with it. Recursion is a tricky but very important concept to learn.

Let me know if you need any more help.
Jul 25 '08 #2
No code snippet, but an explanation:

To do this right, you need to use the System.IO namespace, so for VB.NET, you need to put Imports System.IO at the very top of the page.

The two objects that will be most useful to you are DirectoryInfo and FileInfo. DirectoryInfo can get all files and all directories in a particular directory. However, this will only get the top level. If you want to get all the files in all the subdirectories, you will need to write a recursive function. You should look up recursion if you aren't familiar with it. Recursion is a tricky but very important concept to learn.

Let me know if you need any more help.
Thanks for the help. Actually I used to be a developer one year ago, now I am in infrastructure, so I am kinda forgetting the stuff. I know how to use recursive loop, I will use that. All I need to know how to get the size of the root folder.
Jul 25 '08 #3
Curtis Rutland
3,256 Expert 2GB
Thanks for the help. Actually I used to be a developer one year ago, now I am in infrastructure, so I am kinda forgetting the stuff. I know how to use recursive loop, I will use that. All I need to know how to get the size of the root folder.
Well, that's the thing. You can't get the root folder size without recursively looping through all the subdirs, getting all the files and adding up their sizes.

Hope that helps.
Jul 25 '08 #4
Well, that's the thing. You can't get the root folder size without recursively looping through all the subdirs, getting all the files and adding up their sizes.

Hope that helps.
I have this recursive function to check the total files and folders. It is working fine on small folders, however on large folders program hung and Visual studio generated an exception. How can I make it run faster. Also, how come Windows return files, folders and size so quickly?

Expand|Select|Wrap|Line Numbers
  1. Private Sub SubDirectory(ByVal path As String, ByRef txtBox As TextBox)
  2.         Dim folder As String
  3.  
  4.         If Directory.Exists(path) Then
  5.  
  6.             For Each folder In Directory.GetDirectories(path)
  7.                 totalFolders += 1
  8.                 txtBox.Text &= folder & vbCrLf
  9.                 SubDirectory(folder, txtBox)
  10.             Next
  11.         End If
  12.  
  13.     End Sub
Jul 25 '08 #5
Curtis Rutland
3,256 Expert 2GB
What is the exception that is thrown? Stack Overflow? Out of Memory? I'm not sure how to streamline this, maybe someone else will know.
Jul 25 '08 #6
What is the exception that is thrown? Stack Overflow? Out of Memory? I'm not sure how to streamline this, maybe someone else will know.
Something like this.

The CLR has been unable to transition from COM context 0x1b0328 to COM context 0x1b0498 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.


Also, I have zip files in the folder. Does it affect the performance. I am using the following code to get file size.

Expand|Select|Wrap|Line Numbers
  1. For Each fl In Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
  2.                     totalFiles += 1
  3.                     fInfo = New FileInfo(fl)
  4.                     fileSize += fInfo.Length()
  5. Next
Jul 25 '08 #7
Curtis Rutland
3,256 Expert 2GB
It would appear that I have mislead you...I was wrong. Using that Code you posted should get all files in all subdirectories. The SearchOption.AllDirectories should take care of that. So using that, you shoudl just be able to use that on the root dir, and skip the recursive call. Sorry to send you spinning your wheels.
Jul 25 '08 #8
TRScheel
638 Expert 512MB
The following code should work to your purposes. Very dumbed down and you will have to expand on it for your needs. The code should be fairly self explanatory but if you need me to elaborate I will.

Expand|Select|Wrap|Line Numbers
  1. string folderPath = "";
  2. DirectoryInfo di = new DirectoryInfo(folderPath));
  3. int totalFiles = 0;
  4. int totalFolders = 0;
  5. double totalSizeMB = 0;
  6.  
  7. totalFolders = di.GetDirectories("*", SearchOption.AllDirectories).Count();
  8. foreach (var innerDI in di.GetDirectories("*", SearchOption.AllDirectories))
  9. {
  10.  
  11.     totalFiles += innerDI.GetFiles("*", SearchOption.AllDirectories).Count();
  12.     foreach (var file in innerDI.GetFiles("*", SearchOption.AllDirectories))
  13.     {
  14.         totalSizeMB += file.Length / 1024.0 / 1024.0;
  15.     }
  16. }
  17.  
  18. MessageBox.Show(string.Format("# of Files: {0}\n# of Folders: {1}\nSize in MB: {2}", totalFiles, totalFolders, totalSizeMB));
  19.  
Jul 25 '08 #9
The following code should work to your purposes. Very dumbed down and you will have to expand on it for your needs. The code should be fairly self explanatory but if you need me to elaborate I will.

Expand|Select|Wrap|Line Numbers
  1. string folderPath = "";
  2. DirectoryInfo di = new DirectoryInfo(folderPath));
  3. int totalFiles = 0;
  4. int totalFolders = 0;
  5. double totalSizeMB = 0;
  6.  
  7. totalFolders = di.GetDirectories("*", SearchOption.AllDirectories).Count();
  8. foreach (var innerDI in di.GetDirectories("*", SearchOption.AllDirectories))
  9. {
  10.  
  11.     totalFiles += innerDI.GetFiles("*", SearchOption.AllDirectories).Count();
  12.     foreach (var file in innerDI.GetFiles("*", SearchOption.AllDirectories))
  13.     {
  14.         totalSizeMB += file.Length / 1024.0 / 1024.0;
  15.     }
  16. }
  17.  
  18. MessageBox.Show(string.Format("# of Files: {0}\n# of Folders: {1}\nSize in MB: {2}", totalFiles, totalFolders, totalSizeMB));
  19.  
Thanks man. I will convert this C# code to VB.net. Hopefully it works better than my code snippet.
Jul 26 '08 #10
mh22gw
1
I found this page and this is just what I was looking for. I have some problems when I use this code. I just copy and paste it on my page.

When I add my path like this
string folderPath = Server.MapPath("userFiles/mh22gw");

It will not work, it only shows number of subfolders. When I add my path like this:
string folderPath = Server.MapPath("userFiles");

Then it works fine.

I have tried to use Server.MapPath but it will ot work then either. Can you help me? I would like to have it work in this way
string folderPath = Server.MapPath("userFiles/" + User.Identity.Name);
Dec 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
2
by: Ed Eichman | last post by:
Hi Guys, When I create an ASP.NET web app, which files do I need to check into my version control? I assume that the "bin" directory is skipped, but I'm not sure about the hidden directories...
2
by: AtulSureka | last post by:
Hi, I want to determine the total number of files and folders in the given directory. I have a solution to go recursively on each folder and make a count . But I do not want to go recursively...
6
by: Fred W. | last post by:
When my application starts I need to check folder permissions to ensure they have "Full Control" before I let them proceed on. How can I check this permission. Thank you, Fred
4
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
0
MMcCarthy
by: MMcCarthy | last post by:
This is a module that scans for files and folders on a specified path and describe them in comma separated values file in a text format. The information is stored in this file consecutively like:...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
3
by: datosivakumar | last post by:
How to find out the space usage for all the databases in MS SQL thru SQL Query Analyzer? Normaly i used to view thru SQL Server Enterprise Manager Screen (view --> Taskpad) by selecting 1 db per...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.