Counting all files in folder and subfolder

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-19 05:53:39

问题


I am using visual basic and I want to count all the files that exist in a folder and in its subfolders.. I tried this :

Dim counter = My.Computer.FileSystem.GetFiles("C:\Folder") MsgBox("number of files is " & CStr(counter.Count))

but it only counts files in the C:\Folder and not in C:\Folder\Sub-Folder\AnotherSubFolder What should I do? Thank's for help!


回答1:


Use Directory.GetFiles() as defined here: https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx

So you'd just use

Dim counter As Integer = Directory.GetFiles(someString, "*.*", SearchOption.AllDirectories).Length;
MsgBox("Number of files is : " + counter)

someString being the top-level directory you want to start at

"*.*" being the search pattern you want to match. This gets all files. If you wanted only text files, for example, you could do "*.txt".

SearchOption enum has two options: AllDirectories or TopDirectoryOnly if you're only interested in the exact directory passed, obviously.



来源:https://stackoverflow.com/questions/32277848/counting-all-files-in-folder-and-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!