Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have lot of sub-folders in main folder

Example :

First Main folder name : 317_ABC.

Second main folder name: BMTFT (in this main folder i have lot of sub folders)

30-4 no.5 (inside the files)
30-4 no.6 (inside the files)
30-4 no.8 (inside the files)
37779432 (inside the files)
207758581 (inside the files)
254128794 (inside the files)


How to get the files count with file name and path name (i.e., 317_ABC\BMTFT\30-4 no.5\<files name="">?

Hope this is clear. please any one guide me?
Posted

You must be searching for Directory.GetFiles(path, searchPattern, SearchOption)[^] overload of Directory.GetFiles().
Try this way:
C#
string[] files = directory.GetFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories);

    return files.Length;


Here Path specifies the path, searchPattern specifies your wildcards (e.g., *, *.format) and SearchOption provides the option to include subdirectories.

The Length property of the return array of this search will provide the proper file count for your particular search pattern and option.

Also check similar answer here[^]
 
Share this answer
 
Comments
Pandiarajan A 25-Jun-13 2:32am    
the above code not support VB.net. please advice
Try:
C#
string[] files = Directory.GetFiles(@"D:\Temp\", "*.*", SearchOption.AllDirectories);
Console.WriteLine(files.Length);

This will give you all the files: to check contents you would have to open them all individually.


"the above code is VB.net?"

No, but the differences are trivial. Try:
VB
Dim files As String() = Directory.GetFiles("D:\Temp\", "*.*", SearchOption.AllDirectories)
Console.WriteLine(files.Length)
 
Share this answer
 
v2
Comments
Pandiarajan A 25-Jun-13 2:31am    
the above code is VB.net?
OriginalGriff 25-Jun-13 3:10am    
Answer updated
Pandiarajan A 25-Jun-13 3:51am    
the above code returns the count of file. not meet my requirements
OriginalGriff 25-Jun-13 3:57am    
To quote your question:
"How to get the files count with file name and path name"
The variable "files" holds the file names with paths, and the Lenght property of this shows you how many there are.
Pandiarajan A 25-Jun-13 3:59am    
the above code return the count of files only not full path and sub folder path
You can use this
VB
Dim di As DirectoryInfo = New DirectoryInfo("path")
            di.GetFiles();

Which will return a list of all files in that directory.Then you can get the file count and iterate over them as per your requirement.
 
Share this answer
 
v2
Comments
Pandiarajan A 25-Jun-13 2:31am    
the above code is VB.net?
sudipta biswas 25-Jun-13 3:15am    
Sorry remove the ;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900