📄 21.cpp
字号:
#define _WIN32_WINNT 0x0400
#include<ctime>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include <windows.h>
#ifdef UNICODE
#undef UNICODE
#endif
int withoutfile,timelimit,timelimit1,timelimit2;
char WithoutFilePath[MAX_PATH];
char sFindFileName[MAX_PATH];
char name1[MAX_PATH];
char path1[MAX_PATH];
char a,b,c;
int changtime;
int flag=0;
BOOL MyFindFile(LPCSTR sFindPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder,
ULONGLONG &uCountFile)
{ SYSTEMTIME sys;
GetLocalTime(&sys);
SYSTEMTIME jojo1;
SYSTEMTIME jojo2;
char sPath[MAX_PATH];
char sFormatFileName[MAX_PATH+2] = "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
lstrcpy(sFormatFileName, sFindPath);
lstrcat(sFormatFileName, "\\*");
lstrcat(sFormatFileName, sFindFileName);
lstrcat(sFormatFileName, "*");
hFind = FindFirstFile(sFormatFileName,//欲搜索的文件名。可包含通配符,并可包含一个路径或相对路径名
&FindFileData); //这个结构用于装载与找到的文件有关的信息。该结构可用于后续的搜索
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{FileTimeToSystemTime(&FindFileData.ftLastWriteTime,&jojo1);
FileTimeToSystemTime(&FindFileData.ftCreationTime,&jojo1);
while (!fFinished)
{
lstrcpy(sPath, sFindPath);
lstrcat(sPath, "\\");
lstrcat(sPath, FindFileData.cFileName);
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
{ cout<<"Folder "<<(int)uCountFolder<<". - "<<sPath<<endl; ++uCountFolder;};
}
else {if(timelimit1==1){if((changtime>0&&sys.wDay<=(jojo1.wDay+5))||
(changtime<0&&(sys.wDay-5)>=jojo1.wDay))flag=1;
else flag=0;};
if(timelimit2==1){if(
(changtime>0&&sys.wDay<=(jojo2.wDay+5))||
(changtime<0&&(sys.wDay-5)>=jojo2.wDay)
)flag=1;else flag=0;};
if(timelimit1==0&&timelimit2==0)flag=1;
if(flag){ cout << " File " << (int)uCountFile << ". - " << sPath <<endl; ++uCountFile;}}
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
else
{
break;
}
}
}
FindClose(hFind);
}
return TRUE;
}
BOOL MyFindFolder(LPCSTR sPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sTemp[MAX_PATH];
char sFormatFileName[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
MyFindFile(sPath, sFindFileName, uCountFolder, uCountFile);
lstrcpy(sFormatFileName, sPath);
lstrcat(sFormatFileName, "\\*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
while (!fFinished)
{
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp
(FindFileData.cFileName,".."))
{
lstrcpy(sTemp, sPath);
lstrcat(sTemp, "\\");
lstrcat(sTemp, FindFileData.cFileName);
if(lstrcmp(WithoutFilePath,FindFileData.cFileName))
MyFindFolder(sTemp, sFindFileName, uCountFolder, uCountFile);
}
}
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
FindClose(hFind);
}
return TRUE;
}
void GetFileName(LPCSTR sFullPath, LPSTR sFilePath, LPSTR sFileName)
{
LPSTR p = (LPSTR)(sFullPath + lstrlen(sFullPath) - 1); //P指向的是sFullPath的最后一个字符
//LPSTR被定义成是一个指向以NULL('\0')结尾的8位ANSI字符数组指针,
bool Flag(false);
// 而LPWSTR是一个指向以NULL结尾的16位双字节字符数组指针。
while (p != sFullPath)
{
if ('\\' == *p || '/' == *p)
{
Flag = true;
break;
}
p--;
}
if (Flag)
{
lstrcpy(sFileName, p + 1); //让sFileName指向斜杠后
lstrcpy(sFilePath, sFullPath); //sFullPath 就是sFileName
sFilePath[p-sFullPath] = '\0';
}
else
{
lstrcpy(sFileName, sFullPath);
GetFullPathName(".", MAX_PATH, sFilePath, NULL); //没有斜杠:查找当前目录的文件?
}
}
ULONGLONG MyStartFind(LPCSTR sFindFileName)
{
char sPath[MAX_PATH];
char sFileName[MAX_PATH];
ULONGLONG uCountFolder(0);
ULONGLONG uCountFile(0);
GetFileName(sFindFileName, sPath, sFileName);
MyFindFolder(sPath, sFileName, uCountFolder, uCountFile);
if (uCountFolder + uCountFile)
{
cout << "------------------------------------------------------------"<< endl;
cout << "Total Folders:"<< (int)uCountFolder << " " << endl;
cout << "Total Files: "<< (int)uCountFile << " " << endl;
}
else
cout << "Couldn't Find File." << endl;
return uCountFolder + uCountFile;
}
void main()
{
cout<<"*************文件查找***************"<<endl;
cout<<"输入文件名,如:jojo.txt"<<endl; cout<<"Filename:";cin>>name1;
cout<<"输入起始路径,如:C:\\Windows"<<endl<<"PATH:";cin>>path1;
cout<<"选择不查找的目录y/n:";
cin>>a;
switch(a){case 'y': withoutfile=1;cout<<"请输入不查找的文件夹名称:";cin>>WithoutFilePath;break;
default:withoutfile=0;}
cout<<"选择时间限制y/n:";
cin>>b;
switch(a){case 'y': cout<<"输入1\\2确定选择"<<endl<<"1.按照修改时间"<<endl<<"2.按照创建时间"<<endl;
cin>>timelimit;
switch(timelimit){case 1:timelimit1=1;timelimit2=0;
cout<<"输入天数,-n代表n天前修改,n天前到现在修改过的"<<endl;
cin>>changtime;
break;
case 2:timelimit1=0;timelimit2=1;
cout<<"输入天数,-n代表n天前创建的,n天前到现在修改过的"<<endl;
cin>>changtime;
break;
default:timelimit1=0;timelimit2=0;}
}
lstrcat(path1,name1);
lstrcpy(sFindFileName, path1);
MyStartFind(sFindFileName);
}
/*
typedef struct _WIN32_FIND_DATAA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
CHAR cFileName[ MAX_PATH ];
CHAR cAlternateFileName[ 14 ];
#ifdef _MAC
DWORD dwFileType;
DWORD dwCreatorType;
WORD wFinderFlags;
#endif
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -