C++ 扫描当前路径下文件并删除大文件
C++获取当前路径 扫描文件路径下规定后缀名称的文件 计算文件大小
1. 获取当前路径
使用<Windows.h>中的GetCurrentDirectory方法实现,单独编写验证程序如下:
# include <iostream>
# include <Windows.h>
using namespace std;
int main ( void )
{
char buf[ 1000 ] ;
GetCurrentDirectory ( 1000 , buf) ;
cout << buf << endl;
system ( "pause" ) ;
return 0 ;
}
DWORD GetCurrentDirectory (
[ in] DWORD nBufferLength,
[ out] LPTSTR lpBuffer
) ;
-- -- -- -- -- - ++ ++ -- -- -- -- --
参数说明:
[ in] nBufferLength:存储目录字符串的缓冲区长度,以TCHARs为单位,需要包括终止字符NULL 的空间
[ out] lpBuffer:指针,指向一块缓冲区,用于接收结果(当前目录绝对路径)字符串,以NULL 为结尾。
返回值
执行成功:返回写入缓冲区的字符数(不包括终止null);
执行失败:返回值为0 ;
缓冲区不够大时将返回指定缓冲区所需大小,以字符为单位(包括终止null)。
故可以通过GetCurrentDirectory ( 0 , NULL ) ; 获取缓冲区所需大小,再进行缓冲区初始化完成创建。
使用 Visual Studio 2019 编译器,出现报错"char *" 类型的实参与"LPWSTR" 类型的形参不兼容。
---> 解决:
配置解决方案的属性,在(配置属性-> 高级-> 高级属性)中找到字符集,将其更改为使用多字节字符集。
2. 文件扫描
按照规定后缀名称扫描文件夹,得到规定后缀名的文件列表
void GetAllFormatFiles ( string path, vector< string> & files, string format)
{
intptr_t hFile = 0 ;
struct _finddata_t fileinfo;
string p;
if ( ( hFile = _findfirst ( p. assign ( path) . append ( "\\*" + format) . c_str ( ) , & fileinfo) ) != - 1 )
{
do
{
if ( ( fileinfo. attrib & _A_SUBDIR) )
{
if ( strcmp ( fileinfo. name, "." ) != 0 && strcmp ( fileinfo. name, ".." ) != 0 )
{
GetAllFormatFiles ( p. assign ( path) . append ( "\\" ) . append ( fileinfo. name) , files, format) ;
}
}
else
{
files. push_back ( p. assign ( path) . append ( "\\" ) . append ( fileinfo. name) ) ;
}
} while ( _findnext ( hFile, & fileinfo) == 0 ) ;
_findclose ( hFile) ;
}
}
3. 获取文件大小
size_t getFileSize ( const char * fileName) {
if ( fileName == NULL ) {
return 0 ;
}
struct stat statbuf;
stat ( fileName, & statbuf) ;
size_t filesize = statbuf. st_size;
return filesize;
}
long long GetFileSize ( string& strPath)
{
std:: ifstream read_file_ ( strPath. c_str ( ) , ios:: binary) ;
long long fileSize = 0 ;
if ( read_file_. is_open ( ) )
{
read_file_. seekg ( 0 , ios_base:: end) ;
istream:: pos_type file_size = read_file_. tellg ( ) ;
read_file_. seekg ( 0 , ios_base:: beg) ;
return ( long long ) file_size;
}
return 0 ;
}
4. 完整应用例程
# include <string>
# include <vector>
# include <io.h>
# include <iostream>
# include <fstream>
# include <Windows.h>
using namespace std;
# include "LogM.h"
size_t getFileSize ( const char * fileName) {
if ( fileName == NULL ) {
return 0 ;
}
struct stat statbuf;
stat ( fileName, & statbuf) ;
size_t filesize = statbuf. st_size;
return filesize;
}
long long GetFileSize ( string& strPath)
{
std:: ifstream read_file_ ( strPath. c_str ( ) , ios:: binary) ;
long long fileSize = 0 ;
if ( read_file_. is_open ( ) )
{
read_file_. seekg ( 0 , ios_base:: end) ;
istream:: pos_type file_size = read_file_. tellg ( ) ;
read_file_. seekg ( 0 , ios_base:: beg) ;
return ( long long ) file_size;
}
return 0 ;
}
void GetAllFormatFiles ( string path, vector< string> & files, string format)
{
intptr_t hFile = 0 ;
struct _finddata_t fileinfo;
string p;
if ( ( hFile = _findfirst ( p. assign ( path) . append ( "\\*" + format) . c_str ( ) , & fileinfo) ) != - 1 )
{
do
{
if ( ( fileinfo. attrib & _A_SUBDIR) )
{
if ( strcmp ( fileinfo. name, "." ) != 0 && strcmp ( fileinfo. name, ".." ) != 0 )
{
GetAllFormatFiles ( p. assign ( path) . append ( "\\" ) . append ( fileinfo. name) , files, format) ;
}
}
else
{
files. push_back ( p. assign ( path) . append ( "\\" ) . append ( fileinfo. name) ) ;
}
} while ( _findnext ( hFile, & fileinfo) == 0 ) ;
_findclose ( hFile) ;
}
}
int main ( )
{
# if 1
char buf[ 1000 ] ;
GetCurrentDirectory ( 1000 , buf) ;
std:: cout << "当前路径:" << buf << std:: endl;
string filePath = "E:\\VS2019\\test_demo\\test_demo" ;
vector< string> files;
string format = ".log" ;
GetAllFormatFiles ( filePath, files, format) ;
int size = files. size ( ) ;
for ( int i = 0 ; i < size; i++ )
{
cout << files[ i] << endl;
std:: cout << "file's size1 = " << getFileSize ( files[ i] . c_str ( ) ) << std:: endl;
std:: cout << "file's size2 = " << GetFileSize ( files[ i] ) << std:: endl;
int size = getFileSize ( files[ i] . c_str ( ) ) * 1024 ;
if ( size > 4000 )
{
remove ( files[ i] . c_str ( ) ) ;
}
}
# else
log_. FileScan ( ".txt" ) ;
# endif
system ( "pause" ) ;
return 0 ;
}