Windows API函数获取指定文件目录下文件路径(vc6.0通过)

// FileOpt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <string>
#include <WINDOWS.H>
#include <TCHAR.H>
#include <vector>
using namespace std;

#define MAX_NUM 262

// 删除指定目录下所有文件及目录
BOOL DelDirFileOpt(string szPath)
{
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	string sFullPath;
	string sFindFilter;
	DWORD dwAttributes = 0;
	
	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}
	
	do
	{
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 )
		{
			continue;
		}
		
		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;
		
		//去掉只读属性
		dwAttributes = GetFileAttributes(sFullPath.c_str());
		if (dwAttributes & FILE_ATTRIBUTE_READONLY)
		{
			dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
			SetFileAttributes(sFullPath.c_str(), dwAttributes);
		}
		
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			printf("进入目录%s\n",sFullPath.c_str());
			DelDirFileOpt(sFullPath.c_str());
			RemoveDirectory(sFullPath.c_str());
			printf("删除目录%s成功\n",sFullPath.c_str());
		}
		else
		{
			if ( _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
			{
				//WipeFile(szPath, wfd.cFileName);
			}
			DeleteFile(sFullPath.c_str());
			printf("文件%s删除成功\n",sFullPath.c_str());
		}
	}while (FindNextFile(hFind, &wfd));
	
	FindClose(hFind);
	
	return TRUE;
}


/*static int g_iFlag = 0;*/

//获取指定目录下匹配的第一个文件路径
string GetPathByFileInDir(string szPath,string strFileName)
{
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	string sFullPath;
	string sFindFilter;
	string strTemp = "";
	DWORD dwAttributes = 0;
	static int iFlag;
	
	printf("进入目录%s\n",szPath.c_str());

	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) == INVALID_HANDLE_VALUE)
	{
		return "";
	}
	
	do
	{
		//printf("%d\n",flag);
		if (0 != iFlag)
		{
			break;
		}
		 
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 )
		{
			continue;
		}
		
		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;
		
		//去掉只读属性
		dwAttributes = GetFileAttributes(sFullPath.c_str());
// 		if (dwAttributes & FILE_ATTRIBUTE_READONLY)
// 		{
// 			dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
// 			SetFileAttributes(sFullPath.c_str(), dwAttributes);
// 		}
		
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			strTemp = GetPathByFileInDir(sFullPath,strFileName);
			
			if ( _tcsicmp(strTemp.c_str(), sFullPath.c_str()) == 0)
			{
				printf("【找到%s】\n",sFullPath.c_str());
				iFlag++;
				strTemp = sFullPath;
			}
		}
		else
		{
			if ( _tcsicmp(wfd.cFileName, strFileName.c_str()) == 0)
			{
				printf("找到%s\n",sFullPath.c_str());
				iFlag++;
				strTemp = sFullPath;
			}
		}
	}while (FindNextFile(hFind, &wfd) );
	

	FindClose(hFind);
	return strTemp;
}

//static vector<string> g_vecStr;
//获取指定目录下匹配的文件路径放到容器中返回
vector<string> GetPathsByFileInDirToVector(string szPath,string strFileName)
{
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	string sFullPath;
	string sFindFilter;
	string strTemp = "";
	DWORD dwAttributes = 0;
	static vector<string> vecStr;
	
	printf("进入目录%s\n",szPath.c_str());
	
	//g_vecStr.clear();
	
	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) == INVALID_HANDLE_VALUE)
	{
		//vecStr.push_back("");
		return vecStr;
	}
	
	do
	{
		
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 )
		{
			continue;
		}
		
		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;
		
		//去掉只读属性
		dwAttributes = GetFileAttributes(sFullPath.c_str());
		// 		if (dwAttributes & FILE_ATTRIBUTE_READONLY)
		// 		{
		// 			dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
		// 			SetFileAttributes(sFullPath.c_str(), dwAttributes);
		// 		}
		
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			(void)GetPathsByFileInDirToVector(sFullPath,strFileName);			
		}
		else
		{
			if ( _tcsicmp(wfd.cFileName, strFileName.c_str()) == 0)
			{
				printf("【找到%s】\n",sFullPath.c_str());
				vecStr.push_back(sFullPath);
			}
		}
	}while (FindNextFile(hFind, &wfd) );
	
	
	FindClose(hFind);
	return vecStr;
}


int main(int argc, char* argv[])
{
	char cCurPath[MAX_NUM] = {0};

	GetCurrentDirectory(MAX_NUM,cCurPath);

// 	string strDelPath = (string)cCurPath + (string)"\\testDir";
// 
// 	DelDirFileOpt(strDelPath.c_str());

	string str = GetPathByFileInDir(cCurPath,"config.cfg");

	printf("====[%s]\n",str.c_str());

	vector<string> vecStr;
	vecStr = GetPathsByFileInDirToVector(cCurPath,"config.cfg");
	
	vector<string>::iterator iterStr;

	for (iterStr=vecStr.begin();iterStr!=vecStr.end();iterStr++)
	{
		printf("******************%s\n",iterStr->c_str());
	}

	return 0;
}

阅读更多
个人分类: C++ 工具
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭