vc文件操作实例讲解(判断文件及文件夹是否存在/创建及删除目录/查看指定根目录下目录个数)
时间:2018-09-21
本文章向大家介绍vc文件操作大全(判断文件及文件夹是否存在/创建及删除目录/查看指定根目录下目录个数),需要的朋友可以参考一下
一、判断文件及文件夹是否存在
// 判断文件是否存在 BOOL IsFileExist(const CString& csFile) { DWORD dwAttrib = GetFileAttributes(csFile); return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); } // 判断文件夹是否存在 BOOL IsDirExist(const CString & csDir) { DWORD dwAttrib = GetFileAttributes(csDir); return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); } // 判断文件或文件夹是否存在 BOOL IsPathExist(const CString & csPath) { DWORD dwAttrib = GetFileAttributes(csPath); return INVALID_FILE_ATTRIBUTES != dwAttrib; } // 判断文件或文件夹是否存在 BOOL IsPathExistEx(const CString & csPath) { WIN32_FILE_ATTRIBUTE_DATA attrs = { 0 }; return 0 != GetFileAttributesEx(csPath, GetFileExInfoStandard, &attrs); }
二、创建及删除目录
// 创建目录 BOOL CreateFolder(CString strPath) { SECURITY_ATTRIBUTES attrib; attrib.bInheritHandle = FALSE; attrib.lpSecurityDescriptor = NULL; attrib.nLength = sizeof(SECURITY_ATTRIBUTES); return ::CreateDirectory(strPath, &attrib); //return ::CreateDirectory(strPath,NULL); } // 删除目录及子目录 BOOL DeleteDirectory(CString DirName) { CString PUBPATH; PUBPATH = DirName; CFileFind tempFind; DirName += "\*.*"; BOOL IsFinded = (BOOL)tempFind.FindFile(DirName); while(IsFinded) { IsFinded = (BOOL)tempFind.FindNextFile(); if(!tempFind.IsDots()) { CString strDirName; strDirName += PUBPATH; strDirName += "\"; strDirName += tempFind.GetFileName(); if(tempFind.IsDirectory()) { DeleteDirectory(strDirName); } else { SetFileAttributes(strDirName, FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性 DeleteFile(strDirName); } } } tempFind.Close(); if(!RemoveDirectory(PUBPATH)) { return FALSE ; } return TRUE; }
三、查看指定根目录下目录个数
// 获取目录数 int GetDirNum(CString strPath) { int nDirNum = 0; CFileFind tempFind; BOOL bFound; bFound = tempFind.FindFile(strPath + _T("\*.*")); while (bFound) { bFound = tempFind.FindNextFile(); if(!tempFind.IsDots()) { if(tempFind.IsDirectory()) // 判断是否为文件夹 nDirNum++; } } tempFind.Close(); return nDirNum; }
四、文件记录操作
// 文件末尾写入记录 (以时间为例) void WriteRecord(time_t tm, CString strRecordFilePath) { CTime ctm(tm); CString strTime = ctm.Format(_T("%Y-%m-%d %H_%M_%S")); CStdioFile stdioFile; if (stdioFile.Open(strRecordFilePath, CFile::modeWrite)) { stdioFile.SeekToEnd(); stdioFile.WriteString(strTime); stdioFile.Close(); } } // 读取第一条记录 CString ReadRecord(CString strRecordFilePath) { CString strFirstRecord = _T(""); CStdioFile stdioFile; if (stdioFile.Open(strRecordFilePath, CFile::modeReadWrite)) { stdioFile.SeekToBegin(); stdioFile.ReadString(strFirstRecord); stdioFile.Close(); } return strFirstRecord; } // 删除第一条记录 void DeleteRecord(CString strRecordFilePath) { vector<CString> vecStr; CString strTemp; CStdioFile stdioFile; if (stdioFile.Open(strRecordFilePath, CFile::modeNoTruncate|CFile::modeRead)) { stdioFile.ReadString(strTemp); while (stdioFile.ReadString(strTemp)) { vecStr.push_back(strTemp); } stdioFile.Close(); stdioFile.Open(strRecordFilePath, CFile::modeCreate|CFile::modeWrite); for (int i = 0; i < (int)vecStr.size(); i++) { stdioFile.WriteString(vecStr[i]); } stdioFile.Close(); } }
五、CCeFileFind — wince
由于wince中不支持CFileFind,在此提供网上查找的CCeFileFind类,经过实践证明可用。
//================================头文件=================================== #if !defined _CEFILEFIND_H_ #define _CEFILEFIND_H_ // CeFileFind.h : header file #include <afxwin.h> ///////////////////////////////////////////////////////////////////////////// // CCeFileFind window class CCeFileFind : public CWnd { // Construction public: CCeFileFind( ); public: // Operations void Close(); virtual BOOL FindNextFile( ); virtual BOOL FindFile( LPCTSTR pstrName = NULL); public: // Attributes //Gets the length of the found file, in bytes. DWORD GetLength() const; //Gets the name, including the extension, of the found file CString GetFileName() const; //Gets the whole path of the found file. CString GetFilePath() const; //Gets the whole path of the found file. CString GetRoot() const; // to get the time the specified file was created virtual BOOL GetCreationTime( FILETIME* pFileTime ) const; virtual BOOL GetCreationTime( CTime& refTime ) const; //Gets the time that the file was last accessed. virtual BOOL GetLastAccessTime( CTime& refTime ) const; virtual BOOL GetLastAccessTime( FILETIME* pFileTime ) const; //Gets the time the file was last changed and saved. virtual BOOL GetLastWriteTime( FILETIME* pFileTime ) const; virtual BOOL GetLastWriteTime( CTime& refTime ) const; //Indicates the desired file attributes of the file to be found. virtual BOOL MatchesMask( DWORD dwMask ) const; //Determines if the name of the found file has the name "." or "..", //indicating that is actually a directory. virtual BOOL IsDots( ) const; //Determines if the found file is read-only. BOOL IsReadOnly( ) const; //Determines if the found file is a directory. BOOL IsDirectory( ) const; //Determines if the found file is compressed. BOOL IsCompressed( ) const; //Determines if the found file is a system file. BOOL IsSystem( ) const; //Determines if the found file is hidden. BOOL IsHidden( ) const; //Determines if the found file is temporary. BOOL IsTemporary( ) const; //Determines if the found file is normal (in other words, has no other attributes). BOOL IsNormal( ) const; //Determines if the found file is archived. BOOL IsArchived( ) const; // Implementation public: virtual ~CCeFileFind(); private: LPWIN32_FIND_DATA m_pfiledata; LPWIN32_FIND_DATA m_pNextdata; CString m_csRoot; HANDLE m_hFileHandle; char m_chDirSeparator; void AssertDoneNext() const; }; #endif // !defined _CEFILEFIND_H_
//====================================源文件=============================== // CeFileFind.cpp : implementation file // #include "stdafx.h" #include "CeFileFind.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define DELETE_POINTER(ptr) if( ptr != NULL ) { delete ptr; ptr = NULL; } #define DIR_SEPERATOR '\' ///////////////////////////////////////////////////////////////////////////// // CCeFileFind CCeFileFind::CCeFileFind() :m_hFileHandle(NULL), // initialize to NULL m_pfiledata(NULL) { } CCeFileFind::~CCeFileFind() { Close(); } // Operations BOOL CCeFileFind::FindFile(LPCTSTR pstrName) { //Close(); // if NULL , wild card search if( NULL == pstrName ) { m_csRoot = DIR_SEPERATOR; pstrName = _T("\*.*"); } else { m_csRoot = pstrName; int nPos = m_csRoot.ReverseFind( '\' ); if( nPos == 0 ) m_csRoot = '\'; else m_csRoot = m_csRoot.Left( nPos ); } m_pNextdata = new WIN32_FIND_DATA; // search for file m_hFileHandle = FindFirstFile( pstrName, m_pNextdata ); if ( m_hFileHandle == INVALID_HANDLE_VALUE ) { Close(); return FALSE; } // file was found return TRUE; } BOOL CCeFileFind::FindNextFile() { ASSERT(m_hFileHandle != NULL); if (m_hFileHandle == NULL) return FALSE; if (m_pfiledata == NULL) m_pfiledata = new WIN32_FIND_DATA; AssertDoneNext(); LPWIN32_FIND_DATA pTemp = m_pfiledata; m_pfiledata = m_pNextdata; m_pNextdata = pTemp; return ::FindNextFile(m_hFileHandle, m_pNextdata); } void CCeFileFind::Close() { DELETE_POINTER( m_pfiledata ); DELETE_POINTER( m_pNextdata ); if( m_hFileHandle!= NULL && m_hFileHandle != INVALID_HANDLE_VALUE ) { ::FindClose( m_hFileHandle ); m_hFileHandle = NULL; } } BOOL CCeFileFind::MatchesMask(DWORD dwMask) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if ( m_pfiledata != NULL) return (!!(m_pfiledata->dwFileAttributes & dwMask) ); else return FALSE; } CString CCeFileFind::GetRoot() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) return m_csRoot; else return CString(""); } BOOL CCeFileFind::GetLastAccessTime(FILETIME* pTimeStamp) const { ASSERT(m_hFileHandle != NULL); ASSERT(pTimeStamp != NULL); AssertDoneNext(); if (m_pfiledata != NULL && pTimeStamp != NULL) { *pTimeStamp = m_pfiledata -> ftLastAccessTime; return TRUE; } else return FALSE; } BOOL CCeFileFind::GetLastWriteTime(FILETIME* pTimeStamp) const { ASSERT(m_hFileHandle != NULL); ASSERT(pTimeStamp != NULL); AssertDoneNext(); if (m_pfiledata != NULL && pTimeStamp != NULL) { *pTimeStamp = m_pfiledata -> ftLastWriteTime; return TRUE; } else return FALSE; } BOOL CCeFileFind::GetCreationTime(FILETIME* pTimeStamp) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL && pTimeStamp != NULL) { *pTimeStamp = m_pfiledata -> ftCreationTime; return TRUE; } else return FALSE; } BOOL CCeFileFind::GetLastAccessTime(CTime& refTime) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { refTime = CTime( m_pfiledata -> ftLastAccessTime ); return TRUE; } else return FALSE; } BOOL CCeFileFind::GetLastWriteTime(CTime& refTime) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { refTime = CTime( m_pfiledata -> ftLastWriteTime ); return TRUE; } else return FALSE; } BOOL CCeFileFind::GetCreationTime(CTime& refTime) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { refTime = CTime( m_pfiledata -> ftCreationTime ); return TRUE; } else return FALSE; } BOOL CCeFileFind::IsDots() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); // return TRUE if the file name is "." or ".." and // the file is a directory BOOL bResult = FALSE; if (m_pfiledata != NULL && IsDirectory()) { LPWIN32_FIND_DATA pFindData = m_pfiledata; if (pFindData->cFileName[0] == '.') { if (pFindData->cFileName[1] == '' || (pFindData->cFileName[1] == '.' && pFindData->cFileName[2] == '')) { bResult = TRUE; } } } return bResult; } BOOL CCeFileFind::IsArchived( ) const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsCompressed() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_COMPRESSED ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsDirectory() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsHidden() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_HIDDEN ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsNormal() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_NORMAL ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsReadOnly() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_READONLY ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsSystem() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) return TRUE; else return FALSE; } return FALSE; } BOOL CCeFileFind::IsTemporary() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) { if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY ) return TRUE; else return FALSE; } return FALSE; } CString CCeFileFind::GetFilePath() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); CString csResult = m_csRoot; if (csResult[csResult.GetLength()-1] != DIR_SEPERATOR ) csResult += DIR_SEPERATOR; csResult += GetFileName(); return csResult; } CString CCeFileFind::GetFileName() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); CString ret; if (m_pfiledata != NULL) ret = m_pfiledata->cFileName; return ret; } DWORD CCeFileFind::GetLength() const { ASSERT(m_hFileHandle != NULL); AssertDoneNext(); if (m_pfiledata != NULL) return m_pfiledata -> nFileSizeLow; else return 0; } void CCeFileFind::AssertDoneNext() const { // if you trip the ASSERT in the else side, you've called // a Get() function without having done at least one // FindNextFile() call if (m_hFileHandle == NULL) ASSERT( m_pfiledata == NULL && m_pNextdata == NULL); else ASSERT( m_pfiledata != NULL && m_pNextdata != NULL); }
- 总结---4
- 双飞翼布局的改造 box-sizing和margin负值的应用
- CAICT:2017年互联网平台治理白皮书
- 总结---5
- Spark源码分析 之 Driver和Excutor是怎么跑起来的?(2.2.0版本)
- 英男子与机器人配合演出求婚成功 专家呼吁AI聊天机器人少卖萌
- DOM事件第二弹(UIEvent事件)
- 双向链表
- 通用网页调用本地应用程序方案(windows平台)
- vue、rollup、sass、requirejs组成的vueManager
- 顺序栈的实现和两栈共享空间
- 掌握这几个工具,做微信营销活动才能高大上!
- requirejs、vue、vuex、vue-route的结合使用,您认为可行吗?
- 日报:万达网科员工遭集体裁员,首例“花呗”套现案宣判
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 深入理解web协议(二):DNS、WebSocket
- Elasticsearch:使用 function_score 及 soft_score 定制搜索结果的分数
- Elasticsearch:基于 Vector 的打分
- RocketMQ的发送模式和消费模式
- 实现一个简单的JS效果
- 1000000 / 60S 的 RocketMQ 不停机,扩容,平滑升级!
- Mysql支持远程链接访问
- linux shell编程
- Linux下的I/O复用与epoll详解
- 时间序列&日期学习笔记大全(上)
- LeetCode 题解:一顿操作猛如虎,一看击败百分五
- 这是什么沙雕题目?测试用例居然有人身高为 0 ??
- 深入解读flink sql cdc的使用以及源码分析
- markdown mermaid 画图
- 面试汇总(五):操作系统常见面试总结(一):进程与线程的相关知识点