Windows API函数获取指定文件目录下文件路径(vc6.0通过)
2012-10-10 00:13
501 查看
// 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; }
相关文章推荐
- 通过文件选择框获取指定的目录路径
- 通过vbs获取远程host文件并保存到指定目录
- 10.获取客户端指定路径下的文件目录,并传输至服务器端--客户端
- 使用windows API函数获取目录下所有文件名---目录下文件的扫描和处理
- android获取指定路径下目录文件
- java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~
- Window下获取执行文件的目录的路径函数
- 获取指定目录下指定扩展名文件的绝对路径,存储到文本文件中
- Windows API函数删除指定文件目录下所有内容vc6.0通过
- C#路径中获取文件全路径、目录、扩展名、文件名称常用函数
- android 获取指定路径下所有文件,将文件从一个目录copy到另一个目录,执行linux命令
- android获取指定路径下目录文件
- php 递归获取指定目录下,所有子级、孙级文件路径。
- PHP - Manual手册 - 函数参考 - Directory 目录函数 - scandir列出指定路径中的文件和目录
- 10.获取客户端指定路径下的文件目录,并传输至服务器端--服务器端
- android获取指定路径下目录文件
- 在指定路径创建文件夹, 获取EXE文件的当前目录
- MFC获取指定目录下的所有文件函数
- Qt上获取指定目录下或者及其子目录下各文件或者子目录的全路径
- C#路径中获取文件全路径、目录、扩展名、文件名称常用函数