windows中遍历读取指定文件目录下的文件

#include "stdafx.h"

#include "windows.h"

#include <vector>

#include <string>

#include "iostream"

using namespace std;

typedef std::vector<std::string> file_lists;

static int str_compare(const void *arg1, const void *arg2)

{

//比较字符串arg1 and arg2

return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());

}

file_lists ScanDirectory(const std::string &path, const std::string &extension)

{

//WIN32_FIND_DATA:Contains information about the file that is found

//by the FindFirstFile,FindFirstFileEx, or FindNextFile function

WIN32_FIND_DATA wfd;

HANDLE hHandle;

string searchPath, searchFile;

file_lists vFilenames;

int nbFiles = 0;

searchPath = path + "/*" + extension;

//Searches a directory for a file or subdirectory with a name that matches a specific name

hHandle = FindFirstFile(searchPath.c_str(), &wfd);

if (INVALID_HANDLE_VALUE == hHandle)

{

fprintf(stderr, "ERROR(%s, %d): Cannot find (*.%s)files in directory %s/n", __FILE__, __LINE__, extension.c_str(), path.c_str());

exit(0);

}

do

{

//. or ..

if (wfd.cFileName[0] == ‘.‘)

{

continue;

}

// if exists sub-directory

//dwFileAttributes:The file attributes of a file

if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

{

//FILE_ATTRIBUTE_DIRECTORY:The handle identifies a directory

continue;

}

else //if file

{

searchFile = path + "/" + wfd.cFileName;

vFilenames.push_back(searchFile);

nbFiles++;

}

}while (FindNextFile(hHandle, &wfd));

//Call this member function to continue a file search begun with a call to CGopherFileFind::FindFile

FindClose(hHandle);

//Closes a file search handle opened by the FindFirstFile,FindFirstFileEx,or FindFirstStreamW function

// sort the filenames,Performs a quick sort

qsort((void *)&(vFilenames[0]), (size_t)nbFiles, sizeof(string), str_compare);

return vFilenames;

}

int main(int argc, char* argv[])

{

file_lists files = ScanDirectory("E:", ".jpg");

if (files.empty())

{

cout<<"no image file find in current directory.."<<endl;

system("pause");

exit(-1);

}

int size = files.size();

cout<<"there are "<<size<<" image files totally...."<<endl;

for (int i=0; i<size; i++)

{

cout<<files[i].c_str()<<endl;

}

system("pause");

return 0;

}

//上述是在VS2010环境中编译,运行会在E:/目录下寻找所有后缀为.jpg的文件并打印出来

//#include "stdafx.h"是Visual Studio工具的一个预编译软件。如果你不是用vs工具,或者,你在VS里面没有设置预编译选项,则不需要这一行。

//其次在VS的工程属性里面,不要用unicode字符,改为no set.

//以下是在matlab中的遍历读取文件目录的功能函数

function fileList = readFileListFromFolder(folderName, fileSuffix)

% Read all the files with special suffix to a file

% list.

% A simple use case:

% fileList = readFileListFromFolder(‘test\‘, ‘txt‘);

%Check the input folderName

if ~isempty(findstr(folderName,‘ ‘))

disp(‘There are some space in the folder name: ‘);

disp([‘ ‘folderName]);

disp(‘Please change to use the version2 instead of this one!‘);

end

if strcmp(folderName(end), ‘\‘)

[s, w] = dos([‘dir‘ folderName  ‘*.‘  fileSuffix  ‘/s /B >fileList.txt‘]);

else

[s, w] = dos([‘dir‘ folderName  ‘\*.‘  fileSuffix  ‘/s /B >fileList.txt‘]);

end

fileList = importdata(‘fileList.txt‘);

dos(‘del fileList.txt‘);

end

时间: 2024-08-30 04:04:33

windows中遍历读取指定文件目录下的文件的相关文章

iOS案例:读取指定目录下的文件列表

// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rights reserved. // /* *读取指定目录下的文件列表 */ #import <Foundation/Foundation.h> void myQuickMethod(); int main(int argc, const char * argv[]) { //文件操作对象 NSFil

Spring Boot中如何读取resources目录下的文件

在Java编码过程中,我们常常希望读取项目内的配置文件,按照Maven的习惯,这些文件一般放在项目的src/main/resources下,因此,合同协议PDF模板.Excel格式的统计报表等模板的存放位置是resources/template/test.pdf,下面提供两种读取方式,它们分别在windows和Linux环境(linux下jar包)都可以正常运行. 方法一 ClassPathResource String pdfFilePath = "template/test.pdf"

php 获取文件目录下的文件和子文件 、包括显示出指定的文件名后缀文件。

1 /* 获取文件目录下的文件和子文件*/ 2 3 function getfile($path){ 4 5 foreach(glob($path.'\*' as $file)){ 6 7 echo $file; 8 //判断$file 是否是目录 ,是目录递归再次遍历 9 if(is_dir($file)){ 10 getfile($path) 11 12 } 13 } 14 15 } 16 17 18 /*获取文件目录下指定后缀名的文件*/ 19 20 21 function getFile

Java 读取指定目录下的文件名和目录名

需求:读取指定目录下的文件名和目录名 实现如下: package com.test.common.util; import java.io.File; public class ReadFile { /* * 读取指定路径下的文件名和目录名 */ public void getFileList() { File file = new File("D:\\"); File[] fileList = file.listFiles(); for (int i = 0; i < file

matlab读取指定路径下的图像

利用matlab读取指定路径下的图像 %% 读入指定路径imgFolder下的图像imgName imgFolder = 'F:\博\快盘\图像+数据\images\文章实验图'; %指定路径 imgName = 'tile3_24.png'; %指定路径下的图像的名字 % read the image imgInput= imread(fullfile(imgFolder,imgName)); %读入图像

PHP遍历指定目录下的文件以及文件夹

这个东西在网上找了很久才找到..但是关于去除.和..烦有点不懂额..于是就把那个判断去掉了,改成了str_replace的形式. <?php//定义变量 $n1=array(".",".."); $n2=array("",""); //定义要打开的目录为 upload $dir = "upload/"; //打开目录并遍历所有文件及去除个别文件夹 if (is_dir($dir)) { if ($dh

C#遍历指定路径下的文件夹

通过指定路径访问路径下的文件,在C#的开发中主要利用了Directory类和DirectoryInfo类,简要介绍Directory类中的成员:命名空间 System.IO 命名空间 1.CreateDirectory,已重载,用于创建指定路径下的所有目录: 2.Delete,删除指定目录: 3.Exists,确定给定目录是否引用磁盘现有目录:说白点就是判断路径是否存在: 4.GetCreationTime,获取目录的创建时间和日期: 4.GetCurrentDirectory,获取应用程序的当

C#递归读取指定路径下的所有文件并保存至TreeView

1.代码如下: /// <summary> /// 递归读取指定路径下的所有文件信息 /// </summary> /// <param name="path"></param> /// <param name="node"></param> private void DIGuiGetFile(string path, TreeNode node) { if (!Directory.Exists

Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本

1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引. # -*- coding:utf8 -*- import sys import os import pandas as pd import shutil import stat def find(path,ip): # open the excel file df = pd.read_excel(pat