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

11 篇文章 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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值