如何获取目录中的文件大小。 [英] How to get the files size in directory.
本文介绍了如何获取目录中的文件大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
请告诉我如何获取特定目录中的文件大小。我读了GetDiskFreeSpace()它将返回给定驱动器中的可用空间,如C:\。我想计算c:\ MyFolder中文件占用的大小。我在MyFolder中有多个文件,我想计算文件大小的总数。是否有任何函数来获取目录的大小。请告诉我。
谢谢大家。
解决方案
# include < stdio.h >
#include < sys / types.h >
#include < dirent.h >
int main(void)
{
DIR * dp;
int i;
struct dirent * ep;
dp = opendir(./);
if(dp!= NULL)
{
while(ep = readdir(dp))
i ++;
(无效)closedir(dp);
}
else
perror(无法打开目录);
printf(当前目录中有%d个文件。\ n,i);
返回0;
}
首先,您需要获得总数。的文件。然后获取每个文件的大小。
获得总数。对于文件,你可以使用FindFirstFile / FindNextFile(需要在循环中调用FindNextFile,直到所有文件都被处理完毕。)
参见:http://msdn.microsoft.com/en-us/library/windows/ desktop / aa365200(v = vs.85).aspx [ ^ ]使用FindFirstFile / FindNextFile的示例。
然后使用GetFileSizeEx获取每个文件的大小。
如果您使用的是MFC,则可以使用CFileFind类而不是FindFirstFile和FindNextFile。
http ://www.codeproject.com/Articles/19142/XFolderSize-A-class-to-determine-folder-size。
如果你可以使用Win32,这可能有助于获得文件大小
Hello all,
Please tell me how to get the files size in specific directory. I read GetDiskFreeSpace() it will return available free space in given drive like C:\. I want to calculate how much size the files occupied in c:\MyFolder. I have number of files in MyFolder, I want to calculate the total no of files size. Is there any function to get the size of a directory. Please tell me.
Thank u all.
解决方案
#include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; int i; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) i++; (void) closedir (dp); } else perror ("Couldn't open the directory"); printf("There's %d files in the current directory.\n", i); return 0; }
Firstly, you need to get the total no. of files. Then get size of each file.
To get total no. of files, you can use FindFirstFile/FindNextFile (FindNextFile needs to be called in a loop, until all files have been processed).
See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx[^] for a sample using FindFirstFile/FindNextFile.
Then use GetFileSizeEx to get size of each file.
If you are using MFC, you can use CFileFind class instead of FindFirstFile and FindNextFile.
http://www.codeproject.com/Articles/19142/XFolderSize-A-class-to-determine-folder-size.
If you can use Win32, this might be helpful to get the file size.
这篇关于如何获取目录中的文件大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文