一段旧代码 ———— findfirstfile遍历文件夹

本文介绍了一段旧代码,它利用Win32 API中的findfirstfile函数来遍历Windows操作系统中的文件夹,详细探讨了相关成员函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为用到了windows的API,所以有必要贴上一些dependency的东西。
<span style="font-family: Arial, Helvetica, sans-serif;">typedef struct _WIN32_FIND_DATA {</span>
  DWORD    dwFileAttributes;
  FILETIME ftCreationTime;
  FILETIME ftLastAccessTime;
  FILETIME ftLastWriteTime;
  DWORD    nFileSizeHigh;
  DWORD    nFileSizeLow;
  DWORD    dwReserved0;
  DWORD    dwReserved1;
  TCHAR    cFileName[MAX_PATH];
  TCHAR    cAlternateFileName[14];
} WIN32_FIND_DATA, *PWIN32_FIND_DATA, *LPWIN32_FIND_DATA;


 

Members

dwFileAttributes

The file attributes of a file.

For possible values and their descriptions, see File Attribute Constants.

The FILE_ATTRIBUTE_SPARSE_FILE attribute on the file is set if any of the streams of the file have ever been sparse.

ftCreationTime

A FILETIME structure that specifies when a file or directory was created.

If the underlying file system does not support creation time, this member is zero.

ftLastAccessTime

A FILETIME structure.

For a file, the structure specifies when the file was last read from, written to, or for executable files, run.

For a directory, the structure specifies when the directory is created. If the underlying file system does not support last access time, this member is zero.

On the FAT file system, the specified date for both files and directories is correct, but the time of day is always set to midnight.

ftLastWriteTime

A FILETIME structure.

For a file, the structure specifies when the file was last written to, truncated, or overwritten, for example, whenWriteFile or SetEndOfFile are used. The date and time are not updated when file attributes or security descriptors are changed.

For a directory, the structure specifies when the directory is created. If the underlying file system does not support last write time, this member is zero.

nFileSizeHigh

The high-order DWORD value of the file size, in bytes.

This value is zero unless the file size is greater than MAXDWORD.

The size of the file is equal to (nFileSizeHigh * (MAXDWORD+1)) +nFileSizeLow.

nFileSizeLow

The low-order DWORD value of the file size, in bytes.

dwReserved0

If the dwFileAttributes member includes the FILE_ATTRIBUTE_REPARSE_POINT attribute, this member specifies the reparse point tag.

Otherwise, this value is undefined and should not be used.

For more information see Reparse Point Tags.

IO_REPARSE_TAG_CSV (0x80000009) IO_REPARSE_TAG_DEDUP (0x80000013) IO_REPARSE_TAG_DFS (0x8000000A) IO_REPARSE_TAG_DFSR (0x80000012) IO_REPARSE_TAG_HSM (0xC0000004) IO_REPARSE_TAG_HSM2 (0x80000006) IO_REPARSE_TAG_MOUNT_POINT (0xA0000003) IO_REPARSE_TAG_NFS (0x80000014) IO_REPARSE_TAG_SIS (0x80000007) IO_REPARSE_TAG_SYMLINK (0xA000000C) IO_REPARSE_TAG_WIM (0x80000008)
dwReserved1

Reserved for future use.

cFileName

The name of the file.

cAlternateFileName

An alternative name for the file.

This name is in the classic 8.3 file name format.


杂七杂八的贴了这么多,下面直接上代码了。



#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"


int traversalFile(LPCSTR path)
{
	WIN32_FIND_DATA myFindData;
	HANDLE hFind;

	long long myFileSize = 0;
	char tmpPath[MAX_PATH] = {0};
	char folderPath[MAX_PATH] = {0};
	
	strcpy(folderPath,path);
	if(strlen(path) == 1)
		strcat(folderPath,":");
	strcat(folderPath,"\\*.*");

	hFind = FindFirstFile(folderPath,&myFindData);
	if(hFind == INVALID_HANDLE_VALUE)
	{
#define dwFlags FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM
#define lpSource NULL
		DWORD dwMessageId = GetLastError();
#define dwLanguageId MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
		LPSTR lpBuffer = NULL;
		DWORD nSize = 0;
		//va_list *Arguments = NULL;


		FormatMessage(dwFlags,lpSource,dwMessageId,dwLanguageId,(LPSTR)&lpBuffer,nSize,NULL);//*Arguments);
		printf("%s\tError:%s(GetLastError()=%d)\n",folderPath,lpBuffer,GetLastError());
		return -1;		
	}
	else
		do
		{
			if(lstrcmp(myFindData.cFileName,".")==0 || strcmp(myFindData.cFileName,"..") == 0)
			{
				continue;
			}
			if(myFindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)//check folder or not
			{
				strcpy(tmpPath,path);
				if(strlen(tmpPath)==1)
					strcat(tmpPath,":");
				strcat(tmpPath,"\\");
				strcat(tmpPath,myFindData.cFileName);
				traversalFile(tmpPath);
			}
			else
			{
				printf("FileName: %s\n",myFindData.cFileName);
				myFileSize = myFindData.nFileSizeHigh<<=32;
				myFileSize += myFindData.nFileSizeLow;
				printf("Size: %lld  byte(s)\n",myFileSize);
			}
		}while(FindNextFile(hFind,&myFindData));

		FindClose(hFind);
		return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
	traversalFile("G");
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值