64,649
社区成员
发帖
与我相关
我的任务
分享
void _find(string path)
{
//取路径名最后一个"//"之前的部分,包括"//"
string prefix=path.substr(0,path.find_last_of('//')+1);
WIN32_FIND_DATA FindFileData;
HANDLE hFind=::FindFirstFile(path.c_str(),&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
{
cout<<"文件通配符错误"<<endl;
return;
}
while(TRUE)
{
//目录
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//不是当前目录,也不是父目录
if(FindFileData.cFileName[0]!='.')
{
//查找下一级目录
_find(prefix+FindFileData.cFileName+'//'+"*.*");
}
}
//文件
else
{
cout<<FindFileData.cFileName<<endl;
}
if(!FindNextFile(hFind,&FindFileData))
break;
}
FindClose(hFind);
}
#include<iostream>
#include <string>
using namespace std;
void judge(string p);
void main()
{
string f = "D:\temp\a.bat";
string d = "D:\temp\bat";
judge(f);
judge(d);
}
void judge(string p)
{
bool find = false;
for(size_t i = p.size() - 1; i != 0; --i)
{
if(p[i] == '.')
find = true;
else if(p[i] == '\\')
break;
//break at first '\'
}
if(find)
cout<<"This is a file"<<endl;
else
cout<<"This is a path"<<endl;
}
This is a file
This is a path
Press any key to continue . . .
#include <Windows.h>
#include <stdio.h>
#include <strsafe.h>
void _tmain(int argc, _TCHAR* argv[])
{
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
TCHAR szDirPath[] = TEXT("c:\\TextRO\\");
TCHAR szNewPath[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
printf("Could not create new directory.\n");
return;
}
// Start searching for text files in the current directory.
hSearch = FindFirstFile(TEXT("*.txt"), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No text files found.\n");
return;
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
StringCchCopy(szNewPath, MAX_PATH, szDirPath);
StringCchCat(szNewPath, MAX_PATH, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (dwAttrs==INVALID_FILE_ATTRIBUTES) return;
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Could not copy file.\n");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
printf("Copied all text files.\n");
fFinished = TRUE;
}
else
{
printf("Could not find next file.\n");
return;
}
}
}
// Close the search handle.
FindClose(hSearch);
}