今天花了好长时间了解文件和目录相关的API ,写了简单的验证程序,如有问题之处,希望各位大神指正。代码比较多久不一一列举API的声明之类的信息了。
/***********************************
功能:输出文件属性信息
参数:文件属性
*************************************/
void PrintFileAttributeMessage(DWORD dwFileAttribute)
{
printf("文件属性:\n");
if (dwFileAttribute & FILE_ATTRIBUTE_ARCHIVE)
{
printf("A file or directory that is an archive file or directory\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_TEMPORARY)
{
printf("A file that is being used for temporary storage\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_OFFLINE)
{
printf("The data of a file is not available immediately\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
{
printf("The file or directory is not to be indexed by the content indexing service.\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_HIDDEN)
{
printf("The file or directory is hidden\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_SYSTEM)
{
printf("A file or directory that the operating system uses a part of, or uses exclusively.\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_NORMAL)
{
printf("A file that does not have other attributes set\n");
}
else if (dwFileAttribute & FILE_ATTRIBUTE_READONLY)
{
printf("A file that is read-only. Applications can read the file, but cannot write to it or delete it. \n");
}
}
/***********************************
功能:获取文件属性信息
参数:
*************************************/
void GetFileAttributeInfo()
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要获取属性的文件名:\n");
scanf("%s",szPathName);
DWORD dwFileAttribute = GetFileAttributes(szPathName);
if (INVALID_FILE_ATTRIBUTES == dwFileAttribute)
{
printf("获取文件属性失败\n");
return ;
}
PrintFileAttributeMessage(dwFileAttribute);
}
/***********************************
功能:将系统时间转为中文格式输出
参数:系统时间
*************************************/
void PrintFileTime(SYSTEMTIME &systemTime)
{
map<int,LPSTR> dayofWeekMap;
dayofWeekMap[0] = "日";
dayofWeekMap[1] = "一";
dayofWeekMap[2] = "二";
dayofWeekMap[3] = "三";
dayofWeekMap[4] = "四";
dayofWeekMap[5] = "五";
dayofWeekMap[6] = "六";
printf("%d 年 %d 月 %d 日,星期%s ",systemTime.wYear,systemTime.wMonth ,systemTime.wDay ,dayofWeekMap[systemTime.wDayOfWeek]);
printf("时间:%02d : %02d : %02d \n",systemTime.wHour ,systemTime.wMinute,systemTime.wSecond);
}
/***********************************
功能:输出文件信息
参数:文件信息
*************************************/
void PrintFileInfo(WIN32_FIND_DATA &findFileData )
{
printf("文件名:%s\n",findFileData.cFileName);
PrintFileAttributeMessage(findFileData.dwFileAttributes);
SYSTEMTIME systemTime;
if (FileTimeToSystemTime(&findFileData.ftCreationTime,&systemTime))
{
printf("创建时间:\n");
PrintFileTime(systemTime);
}
if (FileTimeToSystemTime(&findFileData.ftLastAccessTime,&systemTime))
{
printf("最后访问时间:\n");
PrintFileTime(systemTime);
}
if (FileTimeToSystemTime(&findFileData.ftLastWriteTime,&systemTime))
{
printf("最后写文件时间:\n");
PrintFileTime(systemTime);
}
}
/***********************************
功能:遍历文件目录并输出目录下所有文件信息
参数:路径
*************************************/
void EnumerateFile(LPSTR lpPath)
{
CHAR szFilePath[MAX_PATH];
CHAR szFullPath[MAX_PATH];
WIN32_FIND_DATA FindFileData;
wsprintf(szFilePath,"%s\\%s",lpPath,"*.*");
HANDLE hFile = FindFirstFile(szFilePath,&FindFileData);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("查找文件失败\n");
return ;
}
else
{
do
{
if (lstrcmp(FindFileData.cFileName,TEXT(".")) == 0 ||
lstrcmp(FindFileData.cFileName,TEXT("..")) == 0)
{
continue;
}
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
wsprintf(szFullPath,"%s\\%s",lpPath,FindFileData.cFileName);
EnumerateFile(szFullPath);
}
else
{
PrintFileInfo(FindFileData);
}
}while (FindNextFile(hFile,&FindFileData));
}
}
/***********************************
功能:显示功能菜单和实现相关操作
参数:
*************************************/
void Menu()
{
printf("**************** 菜单 ****************** \n");
printf("0 -----------------------------退出\n");
printf("1 -----------------------------创建目录\n");
printf("2 -----------------------------获取当前目录\n");
printf("3 -----------------------------设置当前目录\n");
printf("4 -----------------------------获取模块路径\n");
printf("5 -----------------------------查找文件\n");
printf("6 -----------------------------遍历目录\n");
printf("7 -----------------------------获取文件属性\n");
printf("8 -----------------------------设置文件属性\n");
printf("9 -----------------------------删除目录\n");
printf("请输入选择的操作:\n");
int iOption;
scanf("%d",&iOption);
while(true)
{
if (iOption < 0 || iOption >9)
{
printf("输入有误,请重新输入:\n");
int iOption;
scanf("%d",&iOption);
}
else
{
switch (iOption)
{
case 0:
return;
case 1:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要创建的目录:\n");
scanf("%s",szPathName);
if (!CreateDirectory(szPathName,NULL))
{
printf("创建目录失败\n");
}
}
break;
case 2:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
if (!GetCurrentDirectory(MAX_PATH,szPathName))
{
printf("获取当前目录失败\n");
}
else
{
printf("当前目录为:%s\n",szPathName);
}
}
break;
case 3:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要设置的目录:\n");
scanf("%s",szPathName);
if (!SetCurrentDirectory(szPathName))
{
printf("设置目录失败\n");
}
ZeroMemory(szPathName,MAX_PATH);
if (!GetCurrentDirectory(MAX_PATH,szPathName))
{
printf("创建目录失败\n");
}
else
{
printf("修改之后的当前目录为:%s\n",szPathName);
}
}
break;
case 4:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
if (!GetModuleFileName(NULL,szPathName,MAX_PATH))
{
printf("获取当前模块路径失败\n");
}
else
{
printf("当前模块路径为:%s\n",szPathName);
}
}
break;
case 5:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要查找的文件:\n");
scanf("%s",szPathName);
WIN32_FIND_DATA findFileData = {0};
HANDLE hFile;
hFile = FindFirstFile(szPathName,&findFileData);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("找不到指定文件或目录\n");
}
else
{
PrintFileInfo(findFileData);
while (FindNextFile(hFile,&findFileData))
{
PrintFileInfo(findFileData);
}
}
FindClose(hFile);
}
break;
case 6:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要遍历的目录:\n");
scanf("%s",szPathName);
EnumerateFile(szPathName);
}
break;
case 7:
{
GetFileAttributeInfo();
}
break;
case 8:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要改变属性的文件:\n");
scanf("%s",szPathName);
if (!SetFileAttributes(szPathName,FILE_ATTRIBUTE_READONLY))
{
printf("改变文件属性失败\n");
}
}
break;
case 9:
{
CHAR szPathName[MAX_PATH];
ZeroMemory(szPathName,MAX_PATH);
printf("请输入要删除的目录:\n");
scanf("%s",szPathName);
if (!RemoveDirectory(szPathName))
{
printf("删除目录失败\n");
}
}
break;
}
printf("请选择操作项:\n");
scanf("%d",&iOption);
}
}
}