⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test2.cpp

📁 查找文件夹内所有文件并列出
💻 CPP
字号:
// Test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

#define MAC_FILENAMELENOPATH 50
int j=0;
void FindFileInDir(char* rootDir, char* strRet);

int main(/*int argc, char *argv[]*/)
{
	char* strPath = "f:";
	char filePath[102400];
	

	ZeroMemory(filePath, 102400);

	FindFileInDir(strPath, filePath);

	printf("%s", filePath);

	return 0;
}

void FindFileInDir(char* rootDir, char* strRet)
{
	
	char fname[MAC_FILENAMELENOPATH];
	ZeroMemory(fname, MAC_FILENAMELENOPATH);

	WIN32_FIND_DATA fd;
	ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
	HANDLE hSearch;

	char filePathName[256];
	char tmpPath[256];
    SYSTEMTIME stime; 

	ZeroMemory(filePathName, 256);
	ZeroMemory(tmpPath, 256);

	strcpy(filePathName, rootDir);

      

	if( filePathName[strlen(filePathName) -1] != '\\' )
	{
		strcat(filePathName, "\\");
	}

	strcat(filePathName, "*.*");

	hSearch = FindFirstFile(filePathName, &fd);

	if (hSearch == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
  return;




do
{

	//Is directory

	if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )		
	{
		strcpy(tmpPath, rootDir);
		if( tmpPath[strlen(tmpPath) -1] != '\\' )
	{
		strcat(tmpPath, "\\");
	}

		
		strcat(tmpPath, fd.cFileName);
	
	

	j+=	sprintf(strRet+j, "\t%s\   (%s)   \n", fd.cFileName,tmpPath);
			
		FindFileInDir(tmpPath, strRet);

	}
	
	else	if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
				{
		        

		
	              	FileTimeToSystemTime(&fd.ftCreationTime, &stime) ; // 转换成系统时间格式
	          
	         
					j+=sprintf(strRet+j, "\t\t%s  ", fd.cFileName);

					j+=sprintf(strRet+j,"%04d-%02d-%02d\n", 

						stime.wYear , stime.wMonth , stime.wDay);
	
				
				}

}while (FindNextFile(hSearch, &fd));

	FindClose(hSearch);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -