【发布时间】:2014-02-17 07:37:44
【问题描述】:
我试图弄清楚如何从给定目录中删除所有文本文件。我正在使用 Visual c++ 2010 express,使用 winapi。如果您知道该文件的确切名称,我知道如何删除该文件,但我想删除该目录中的所有文本文件。这是我最近的尝试:
void deleteFiles( WCHAR file[] )
{
// WCHAR file[] is actually the directory path. i.e C:\Users\TheUser\Desktop\Folder\
// Convert from WCHAR to char for future functions
char filePath[ MAX_PATH ];
int i;
for( int i = 0; file[ i ] != '\0'; i++ )
{
// Cycle through each character from the array and place it in the new array
filePath[ i ] = file[ i ];
}
// Place the null character at the end
filePath[ i ] = '\0';
// Generate WIN32_FIND_DATA struct and FindFirstFile()
WIN32_FIND_DATA fileData;
FindFirstFile( file, &fileData );
// Display the filename
MessageBox( NULL, fileData.cFileName, L"Check", MB_OK );
}
消息框只显示被选中的文件夹,而不是文件名。为什么会这样?
【问题讨论】:
-
如果不使用,为什么要声明“filePath”?
-
这是我之前尝试过的不同解决方案。我想我忘了把它拿出来。
标签: c++ file winapi delete-file