计算文件夹和子文件夹中的所有文件 [英] Counting all files in folder and subfolder

查看:81
本文介绍了计算文件夹和子文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Basic,我想计算一个文件夹及其子文件夹中存在的所有文件.

调暗计数器= My.Computer.FileSystem.GetFiles("C:\ Folder")MsgBox(文件数为"& CStr(counter.Count))

,但它仅计数 C:\ Folder 中的文件,而不计数 C:\ Folder \ Sub-Folder \ AnotherSubFolder 中的文件我应该怎么办?谢谢您的帮助!

解决方案

使用 Directory.GetFiles()定义如下:

someString 是您要从其开始的顶级目录

"*.*" 是您要匹配的搜索模式.这将获取所有文件.例如,如果只需要文本文件,则可以执行"*.txt" .

SearchOption 枚举有两个选项: AllDirectories TopDirectoryOnly (如果您对确切目录感兴趣)通过了,很明显.

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!

解决方案

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.

这篇关于计算文件夹和子文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆