C++如何实现 统计 一个文件夹中的文件的 个数 并用 FOR循环 依次读取文件的文件名

C++如何实现 统计 一个文件夹中的文件的 个数 并用 FOR循环 依次读取文件的文件名,第1张

给出 临时工作文件名和路径,要查的文件夹路径和文件夹名。

用 DOS 命令获文件名

#include <stdioh>

#include <stdlibh>

#define Buff_size 4096

FILE fin;

void main (int argc, char argv[])

{

char current_dir[72], namelist[72],current_file_name[72];

char command[200];

int i,j,n=0;

char buff;

buff = (char ) malloc( Buff_size sizeof (char));

if (!buff) {

printf("\007No enough memory -- Can not alloc the Buff\n");

exit(2);

};

strcpy(namelist,"C:\\temp\\abclis"); // 临时工作文件名和路径

strcpy(current_dir, "D:\\zzz"); //要查的文件夹路径和文件夹名

sprintf(command,"DIR/B/A-D %s > %s", current_dir, namelist);

system(command);

if ( (fin = fopen(namelist,"r") ) == NULL ) {

printf("\007Cann't open work file: %s ", namelist);exit(1);

};

while ( fgets( current_file_name, 72, fin) !=NULL ) n=n+1;

rewind(fin);

printf("\007total %d files\n",n);

for (i=0;i<n;i++){

if ( fgets( current_file_name, 72, fin) ==NULL) exit(0);

printf("%s",current_file_name);

}

exit(0);

}

递归获取本文件夹(包括子文件夹)中的文件:

int CountDirectory(CString path)

{

int count = 0;

CFileFind finder;

BOOL working = finderFindFile(path + "\\");

while (working)

{

working = finderFindNextFile();

if (finderIsDots())

continue;

if (finderIsDirectory())

count += CountDirectory(finderGetFilePath());

else

count++;

}

return count;

}

只获取本文件夹中的文件:

int CountDirectory(CString path)

{

int count = 0;

CFileFind finder;

BOOL working = finderFindFile(path + "\\");

while (working)

{

working = finderFindNextFile();

if (finderIsDots())

continue;

if (!finderIsDirectory())

count++;

}

return count;

}

package comexamples_27;

import javaioFile;

import androidappActivity;

import androidosBundle;

public class MainActivity extends Activity {

public static int i = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState);

setContentView(Rlayoutactivity_main);

new Thread() {

public void run() {

String path = "/sdcard/androidesk/";

getFiles(path);

Systemoutprintln(path + " 文件夹下面共有 " + i + " 张文件");

};

}start();

}

 

private void getFiles(String string) {

// TODO Auto-generated method stub 

File file = new File(string);

File[] files = filelistFiles();

for (int j = 0; j < fileslength; j++) {

String name = files[j]getName();

if (files[j]isDirectory()) {

String dirPath = files[j]toString()toLowerCase(); 

Systemoutprintln(dirPath);

getFiles(dirPath + "/");

} else if (files[j]isFile() & nameendsWith("jpg") || nameendsWith("png") || nameendsWith("bmp") || nameendsWith("gif") || nameendsWith("jpeg")) {

Systemoutprintln("FileName===" + files[j]getName());

i++;

}

}

}

}

清单文件 manifest 节点下面添加 读取权限

<uses-permission android:name="androidpermissionREAD_EXTERNAL_STORAGE" />

using System;

using SystemCollectionsGeneric;

using SystemIO;

using SystemLinq;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int count = GetFileCount("d:\test", "exe");

            ConsoleWriteLine("文件数量:{0}",count);

        }

 

        //参数:

        //  string dir 指定的文件夹

        //  string ext 文件类型的扩展名,如"txt" , “exe"

        static int GetFileCount(string dir, string ext)

        {

            int count = 0;

            DirectoryInfo d = new DirectoryInfo(dir);

            foreach (FileInfo fi in dGetFiles())

            {

                if (fiExtensionToUpper() == extToUpper())

                {

                    count++;

                }

            }

            return count;

        }

    }

}

以上就是关于C++如何实现 统计 一个文件夹中的文件的 个数 并用 FOR循环 依次读取文件的文件名全部的内容,包括:C++如何实现 统计 一个文件夹中的文件的 个数 并用 FOR循环 依次读取文件的文件名、vc如何获取文件夹中文件个数、安卓开发 如何获取sd卡中的某个文件夹的图片个数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9742850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存