小程序,遍历

qq_32890755 2017-05-07 03:03:41
遍历某个文件夹下的所有文件里面的图片(包括子文件,孙文件),然后,把遍历得到的所有图片保存到指定的文件夹下(以图片生成的时间顺序排列),使用c++或者python或c# 可以做到吗?谢谢大神的指导!!
...全文
564 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_32890755 2017-05-09
  • 打赏
  • 举报
回复
引用 11 楼 starytx 的回复:
引用 10 楼 qq_32890755 的回复:
[quote=引用 8 楼 starytx 的回复:] [quote=引用 7 楼 qq_32890755 的回复:] starytx,编译报错,呜呜
贴出错误信息?vs2013下编译通过
很多的标识符未定义[/quote]再加这几个头文件看看 #include "stdafx.h" #include <sstream> #include <iostream> #include <windows.h>[/quote] 虽然还是报错 ,仍需要谢谢 ~
qq_32890755 2017-05-09
  • 打赏
  • 举报
回复
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
#include <tchar.h>
#include<iostream>
#include<windows.h>;
using namespace std;

// 根据后缀名判断是否是图片文件
bool IsImage(LPCTSTR szExten)
{
std::wstring str(szExten);
transform(str.begin(), str.end(), str.begin(), tolower); // 转化为小写
if (//L"bmp" == str ||
//L"png" == str ||
L"jpg" == str //||
//L"jpeg" == str ||
)//L"gif" == str)
{
return true;
}

return false;
}


void find(LPCTSTR lpPath, std::vector<const std::wstring> &fileList)
{
TCHAR szFind[MAX_PATH] = { 0 };
WIN32_FIND_DATA FindFileData;

wcscpy_s(szFind, lpPath);
wcscat_s(szFind, _T("\\*.*"));

HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind) return;

while (1)
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (FindFileData.cFileName[0] != '.')
{
TCHAR szFile[MAX_PATH] = { 0 };
wcscpy_s(szFile, lpPath);
wcscat_s(szFile, _T("\\"));
wcscat_s(szFile, (FindFileData.cFileName));
find(szFile, fileList);
}
}
else
{
std::wstring strFileName = FindFileData.cFileName;
std::wstring suffixStr = strFileName.substr(strFileName.find_last_of('.') + 1);
if (IsImage(suffixStr.c_str()))
{
std::wstring strFilePath = lpPath;
strFilePath += _T("\\");
strFilePath += FindFileData.cFileName;
//std::cout << FindFileData.cFileName << std::endl;
fileList.push_back(strFilePath);
}
}
if (!FindNextFile(hFind, &FindFileData)) break;
}
FindClose(hFind);
}


int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, ("chs")); // 显示中文
std::vector<const std::wstring> fileList;
find(_T("D:\\saveimage"), fileList); // 遍历"C:\testFrom"文件夹,找到所有图片文件路径
std::wstring strTo;
for (int i = 0; i != fileList.size(); ++i)
{
strTo = L"D:\\卡口名称\\日期\\"; // 目标文件目录L"C:\\testTo\\";
strTo += fileList[i].substr(fileList[i].rfind('\\'));
wcout << fileList[i].c_str() << endl;
CopyFile(fileList[i].c_str(), strTo.c_str(), FALSE);
}
return 0;
}


starytx 2017-05-09
  • 打赏
  • 举报
回复
引用 10 楼 qq_32890755 的回复:
引用 8 楼 starytx 的回复:
[quote=引用 7 楼 qq_32890755 的回复:] starytx,编译报错,呜呜
贴出错误信息?vs2013下编译通过
很多的标识符未定义[/quote]再加这几个头文件看看 #include "stdafx.h" #include <sstream> #include <iostream> #include <windows.h>
qq_32890755 2017-05-09
  • 打赏
  • 举报
回复
引用 8 楼 starytx 的回复:
引用 7 楼 qq_32890755 的回复:
starytx,编译报错,呜呜
贴出错误信息?vs2013下编译通过
很多的标识符未定义
qq_32890755 2017-05-09
  • 打赏
  • 举报
回复
引用 8 楼 starytx 的回复:
引用 7 楼 qq_32890755 的回复:
starytx,编译报错,呜呜
贴出错误信息?vs2013下编译通过
在vs2015下报错哎
赵4老师 2017-05-08
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
starytx 2017-05-08
  • 打赏
  • 举报
回复
仅供参考,图片文件的判断只是简单的根据后缀来判断。
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;

// 根据后缀名判断是否是图片文件
bool IsImage(LPCTSTR szExten)
{
	std::wstring str(szExten) ;
	transform(str.begin(), str.end(), str.begin(), tolower);  // 转化为小写
	if (L"bmp" == str ||
		L"png" == str ||
		L"jpg" == str ||
		L"jpeg" == str ||
		L"gif" == str)
	{
		return true;
	}
	return false;
}

void find(LPCTSTR lpPath, std::vector<const std::wstring> &fileList)
{
	TCHAR szFind[MAX_PATH] = { 0 };
	WIN32_FIND_DATA FindFileData;

	wcscpy_s(szFind, lpPath);
	wcscat_s(szFind, _T("\\*.*"));

	HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
	if (INVALID_HANDLE_VALUE == hFind)    return;

	while (1)
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (FindFileData.cFileName[0] != '.')
			{
				TCHAR szFile[MAX_PATH] = { 0 };
				wcscpy_s(szFile, lpPath);
				wcscat_s(szFile, _T("\\"));
				wcscat_s(szFile, (FindFileData.cFileName));
				find(szFile, fileList);
			}
		}
		else
		{
			std::wstring strFileName = FindFileData.cFileName;
			std::wstring suffixStr = strFileName.substr(strFileName.find_last_of('.') + 1);
			if (IsImage(suffixStr.c_str()))
			{
				std::wstring strFilePath = lpPath;
				strFilePath += _T("\\");
				strFilePath += FindFileData.cFileName;
				//std::cout << FindFileData.cFileName << std::endl;  
				fileList.push_back(strFilePath);
			}
		}
		if (!FindNextFile(hFind, &FindFileData))    break;
	}
	FindClose(hFind);
}


int _tmain(int argc, _TCHAR* argv[])
{
	setlocale(LC_ALL, ("chs")); // 显示中文
	std::vector<const std::wstring> fileList;
	find(_T("C:\\testFrom"), fileList);		// 遍历"C:\testFrom"文件夹,找到所有图片文件路径
	std::wstring strTo;
	for (int i = 0; i != fileList.size(); ++i)
	{
		strTo = L"C:\\testTo\\";			// 目标文件目录
		strTo += fileList[i].substr(fileList[i].rfind('\\'));
		wcout << fileList[i].c_str() <<endl;
		CopyFile(fileList[i].c_str(), strTo.c_str(), FALSE);
	}
	return 0;
}
starytx 2017-05-08
  • 打赏
  • 举报
回复
引用 7 楼 qq_32890755 的回复:
starytx,编译报错,呜呜
贴出错误信息?vs2013下编译通过
qq_32890755 2017-05-08
  • 打赏
  • 举报
回复
starytx,编译报错,呜呜
ooolinux 2017-05-07
  • 打赏
  • 举报
回复
findfirst 、findnext、找到目录则递归,以前好像看过类似代码,我也不会写。
qq_32890755 2017-05-07
  • 打赏
  • 举报
回复
有具体的源码看看嘛
Canceled_bdflyao1 2017-05-07
  • 打赏
  • 举报
回复
寻找文件-_-给你个思路,具体代码我没有
flying_music 2017-05-07
  • 打赏
  • 举报
回复
shell比较简单,C++应该比较复杂,python不太懂

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧