我给你 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> #include <errno.h>int schflag = 0; long ndir, nfile;int isdir(char *); void search(char *, char *);main(int argc, char **argv) { char *fname; char cbuf[BUFSIZ]; char *cdir; ndir = nfile = 0; if ( argc == 1) { printf("Usage: %s [path] file\n", argv[0]); printf("\tif no path is given as a command line argument,\n"); printf("\tthe current dir is set as the default search dir.\n"); exit(0); } if ( argc == 2) { fname = argv[1]; cdir = (char *) getcwd(cbuf, BUFSIZ); } if( argc == 3) { fname = argv[2]; cdir = argv[1]; } //printf("current dir is %s\n", cdir); search(cdir, fname); if(!schflag) printf("%s: '%s' not found.\n",argv[0], fname); printf("%ld file(s)\t%ld (sub)dir(s)\n", nfile, ndir); }int isdir(char *name) { struct stat stbuf; if( lstat(name, &stbuf) < 0) { printf("stat: %s :%s\n",name, strerror(errno)); return 0; } else return (S_ISDIR(stbuf.st_mode))? 1 : 0 ; }void search(char *path, char *fname) { DIR *dirp; struct dirent *dir; char buffer[BUFSIZ]; if(isdir(path)) { if( (dirp = opendir(path)) == NULL) { printf("Error opening dir %s", path); printf(" :%s\n", strerror(errno)); return; } } else return; while( (dir = readdir(dirp)) != NULL) { if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") ) { } else { bzero(buffer, sizeof(buffer)); strncat(buffer, path, sizeof(buffer)); if(path[strlen(path) - 1] == '/'); else strncat(buffer, "/", sizeof(buffer)); strncat(buffer, dir->d_name, sizeof(buffer)); if(isdir(buffer)) { ndir++; //printf("%s is a dir.\n", buffer); search(buffer, fname); } else { nfile++; // printf("%s is NOT a dir.\n", dir->d_name); // printf("%s/%s\n", path, dir->d_name); if (dir->d_namlen == strlen(fname) && !strcmp(dir->d_name, fname )) { schflag = 1; printf("\33[01;32mfound: %s/%s\33[00m\n", path,f name); } } } } } closedir(dirp); }
ISCardFileAccess::Directory The Directory method retrieves a list of files of the specified type from the current directory.HRESULT Directory( FILETYPE fileType, LPSAFEARRAY *ppFileList ); Parameters fileType [in] Specifies the type of smart card files to list. This can be one of the following values. Value Description SC_TYPE_DIRECTORIES List directory files only. SC_TYPE_FILES List elementary files only. SC_TYPE_ALL_FILES List both directory and elementary files. SC_TYPE_DIRECTORY_FILE Directory file. SC_TYPE_TRANSPARENT_EF Transparent elementary file. SC_TYPE_FIXED_EF Linear fixed elementary file. SC_TYPE_CYCLIC_EF Cyclic elementary file. SC_TYPE_VARIABLE_EF Linear variable elementary file. ppFileList [out, retval] Array of BSTRs representing the list of files matching the specifier in fileType. Return Values The possible return values are the following.Value Meaning S_OK Operation was completed successfully. E_INVALIDARG Invalid parameter. E_NOTIMPL The interface has not implemented this method. E_OUTOFMEMORY Out of memory. E_POINTER A bad pointer was passed in for ppFileList. Res For a list of all the methods defined by the ISCardFileAccess interface, see ISCardFileAccess.In addition to the COM error codes listed above, this interface may return a smart card error code if a smart card function was called to complete the request. For information on smart card error codes, see Smart Card Error Codes.Requirements Windows NT/2000: Requires Windows NT 4.0 SP3 or later. Windows 95/98: Requires Windows 95 OSR2.1 or later. Header: Vendor supplied.See Also ISCardFileAccess
运行结果 [venus@linux tmp]$ ./ff ./ makefile is NOT a dir. /home/venus/tmp/makefile a.c is NOT a dir. /home/venus/tmp/a.c gg.c is NOT a dir. /home/venus/tmp/gg.c a is NOT a dir. /home/venus/tmp/a ff is NOT a dir. /home/venus/tmp/ff b.c is NOT a dir. /home/venus/tmp/b.c g.c is NOT a dir. /home/venus/tmp/g.c c.c is NOT a dir. 我们知道CfileFind未提供直接遍历其子目录的功能,而有时候我们却常常要遍历某一目录下的所有文件及其子目录。如我们要删除一个目录,而这个目录下又有子目录,因为Windows不允许删除非空的目录,因此我们必须能够遍历一个目录下的所有子目录,这可以通过简单的递归实现. 下面让我们从一个简单的例子开始:如何删除某一目录?(假设我们通过DeleteDirectory(LPCTSTR DirName)函数完成这一功能) 要删除一个目录,我们要完成下面几步: 1. 删除该目录下的所有文件 2. 如果该目录中还有子目录我们要递归地调用DeleteDirectory(LPCTSTR DirName)函数,以删除该子目录下的所有文件 3. 调用RemoveDirectory(LPCTSTR lpPathName)删除该目录DeleteDirectory(LPCTSTR DirName)函数的完整实现如下: BOOL DeleteDirectory(LPCTSTR DirName) { CFileFind tempFind; file://声明一个CFileFind类变量,以用来搜索 char tempFileFind[200]; file://用于定义搜索格式 sprintf(tempFileFind,"%s\\*.*",DirName); file://匹配格式为*.*,即该目录下的所有文件 BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind); file://查找第一个文件 while(IsFinded) { IsFinded=(BOOL)tempFind.FindNextFile(); file://递归搜索其他的文件 if(!tempFind.IsDots()) file://如果不是"."目录 { char foundFileName[200]; strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200)); if(tempFind.IsDirectory()) file://如果是目录,则递归地调用 { file://DeleteDirectory char tempDir[200]; sprintf(tempDir,"%s\\%s",DirName,foundFileName); DeleteDirectory(tempDir); } else { file://如果是文件则直接删除之 char tempFileName[200]; sprintf(tempFileName,"%s\\%s",DirName,foundFileName); DeleteFile(tempFileName); } } } tempFind.Close(); if(!RemoveDirectory(DirName)) file://删除目录 { AfxMessageBox("删除目录失败!",MB_OK); return FALSE; } return TRUE; }
E-MAIL:[email protected]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>int schflag = 0;
long ndir, nfile;int isdir(char *);
void search(char *, char *);main(int argc, char **argv)
{
char *fname;
char cbuf[BUFSIZ];
char *cdir; ndir = nfile = 0; if ( argc == 1) {
printf("Usage: %s [path] file\n", argv[0]);
printf("\tif no path is given as a command line argument,\n");
printf("\tthe current dir is set as the default search dir.\n");
exit(0);
}
if ( argc == 2) {
fname = argv[1];
cdir = (char *) getcwd(cbuf, BUFSIZ);
}
if( argc == 3) {
fname = argv[2];
cdir = argv[1];
} //printf("current dir is %s\n", cdir);
search(cdir, fname); if(!schflag)
printf("%s: '%s' not found.\n",argv[0], fname);
printf("%ld file(s)\t%ld (sub)dir(s)\n", nfile, ndir);
}int
isdir(char *name)
{
struct stat stbuf; if( lstat(name, &stbuf) < 0) {
printf("stat: %s :%s\n",name, strerror(errno));
return 0;
}
else
return (S_ISDIR(stbuf.st_mode))? 1 : 0 ;
}void
search(char *path, char *fname)
{
DIR *dirp;
struct dirent *dir;
char buffer[BUFSIZ];
if(isdir(path)) {
if( (dirp = opendir(path)) == NULL) {
printf("Error opening dir %s", path);
printf(" :%s\n", strerror(errno));
return;
}
}
else
return; while( (dir = readdir(dirp)) != NULL) {
if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") ) { }
else {
bzero(buffer, sizeof(buffer));
strncat(buffer, path, sizeof(buffer));
if(path[strlen(path) - 1] == '/');
else
strncat(buffer, "/", sizeof(buffer));
strncat(buffer, dir->d_name, sizeof(buffer));
if(isdir(buffer)) {
ndir++;
//printf("%s is a dir.\n", buffer);
search(buffer, fname);
}
else {
nfile++;
// printf("%s is NOT a dir.\n", dir->d_name);
// printf("%s/%s\n", path, dir->d_name);
if (dir->d_namlen == strlen(fname) && !strcmp(dir->d_name, fname
)) {
schflag = 1;
printf("\33[01;32mfound: %s/%s\33[00m\n", path,f
name);
}
}
}
}
}
closedir(dirp);
}
The Directory method retrieves a list of files of the specified type from the current directory.HRESULT Directory(
FILETYPE fileType,
LPSAFEARRAY *ppFileList
);
Parameters
fileType [in]
Specifies the type of smart card files to list. This can be one of the following values. Value Description
SC_TYPE_DIRECTORIES List directory files only.
SC_TYPE_FILES List elementary files only.
SC_TYPE_ALL_FILES List both directory and elementary files.
SC_TYPE_DIRECTORY_FILE Directory file.
SC_TYPE_TRANSPARENT_EF Transparent elementary file.
SC_TYPE_FIXED_EF Linear fixed elementary file.
SC_TYPE_CYCLIC_EF Cyclic elementary file.
SC_TYPE_VARIABLE_EF Linear variable elementary file.
ppFileList [out, retval]
Array of BSTRs representing the list of files matching the specifier in fileType.
Return Values
The possible return values are the following.Value Meaning
S_OK Operation was completed successfully.
E_INVALIDARG Invalid parameter.
E_NOTIMPL The interface has not implemented this method.
E_OUTOFMEMORY Out of memory.
E_POINTER A bad pointer was passed in for ppFileList.
Res
For a list of all the methods defined by the ISCardFileAccess interface, see ISCardFileAccess.In addition to the COM error codes listed above, this interface may return a smart card error code if a smart card function was called to complete the request. For information on smart card error codes, see Smart Card Error Codes.Requirements
Windows NT/2000: Requires Windows NT 4.0 SP3 or later.
Windows 95/98: Requires Windows 95 OSR2.1 or later.
Header: Vendor supplied.See Also
ISCardFileAccess
[venus@linux tmp]$ ./ff ./
makefile is NOT a dir.
/home/venus/tmp/makefile
a.c is NOT a dir.
/home/venus/tmp/a.c
gg.c is NOT a dir.
/home/venus/tmp/gg.c
a is NOT a dir.
/home/venus/tmp/a
ff is NOT a dir.
/home/venus/tmp/ff
b.c is NOT a dir.
/home/venus/tmp/b.c
g.c is NOT a dir.
/home/venus/tmp/g.c
c.c is NOT a dir. 我们知道CfileFind未提供直接遍历其子目录的功能,而有时候我们却常常要遍历某一目录下的所有文件及其子目录。如我们要删除一个目录,而这个目录下又有子目录,因为Windows不允许删除非空的目录,因此我们必须能够遍历一个目录下的所有子目录,这可以通过简单的递归实现. 下面让我们从一个简单的例子开始:如何删除某一目录?(假设我们通过DeleteDirectory(LPCTSTR DirName)函数完成这一功能) 要删除一个目录,我们要完成下面几步: 1. 删除该目录下的所有文件 2. 如果该目录中还有子目录我们要递归地调用DeleteDirectory(LPCTSTR DirName)函数,以删除该子目录下的所有文件 3. 调用RemoveDirectory(LPCTSTR lpPathName)删除该目录DeleteDirectory(LPCTSTR DirName)函数的完整实现如下:
BOOL DeleteDirectory(LPCTSTR DirName)
{
CFileFind tempFind; file://声明一个CFileFind类变量,以用来搜索
char tempFileFind[200]; file://用于定义搜索格式
sprintf(tempFileFind,"%s\\*.*",DirName);
file://匹配格式为*.*,即该目录下的所有文件 BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
file://查找第一个文件
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile(); file://递归搜索其他的文件
if(!tempFind.IsDots()) file://如果不是"."目录
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory()) file://如果是目录,则递归地调用
{ file://DeleteDirectory
char tempDir[200];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(tempDir);
}
else
{ file://如果是文件则直接删除之
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(DirName)) file://删除目录
{
AfxMessageBox("删除目录失败!",MB_OK);
return FALSE;
}
return TRUE;
}
通过上面的例子,详细读者已学会了如何递归遍历某一目录下的所有文件及子目录了。实际上利用这一点可以作出非常有用的工具。下面我给大家举个小例子。 用VC编写程序的时候,VC会生成一大堆的中间文件,这些中间文件的体积十分庞大,一般比我们编写的代码要大出10倍以上。当我们想要把我们编写的源代码保留起来,以供以后查阅时,我们不得不手动删除这些中间文件,而有些时候,你想查看以前某个工程的运行结果,于是你编译该工程,经常这样做的话,如果你不删除VC生成的中间文件,你的硬盘很快就会被一大堆的中间文件塞满。于是我编写了一个VC的中间文件清理工具。其原理非常简单: 首先,让用户指定一个需要清理的目录,然后我们通过CFileFind递归地遍历该目录,查找目录名为Debug和Release的目录(这是VC默认的输出目录,如果你更改了缺省设置的话,必须手动删除之),然后调用上面我们编写的DeleteDirectory函数删除之。 警告:使用该工具时,一定要确保你的工程没有叫Debug或Release的,而且你的有用的目录名也不能是Debug或Release,否则,使用本工具会全部把它们删掉的。 另外,本工具也能统计源程序的规模(给出整个工程的行数、c文件数、h文件数和cpp文件数及总的文件数目),你可以用它方便地统计出自己源程序的规模。统计源程序规模的原理也是递归地查找某一目录,其原理和上面讲的DeleteDirestory函数是一致的。 有了该工具你再也不用手动清理自己机子上那一大堆的中间文件了,当你需要把自己的工程目录保存起来,以供以后查阅时,你就可以通过该工具彻底地清理一下中间文件,平时你再也不用去管那些令人讨厌的中间文件了。
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
--------------------------------
void FindFile(CString &strTemp)
{
WIN32_FIND_DATA fd;
//::SetCurrentDirectory(strTemp); HANDLE hFind = ::FindFirstFile("*.*", &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CString name = fd.cFileName;
if(name != _T(".") && name != _T(".."))
{
strTemp += _T("\\") + name;
::SetCurrentDirectory(strTemp);
FindFile(strTemp);
::SetCurrentDirectory(_T(".."));
TCHAR szCurDir[MAX_PATH];
::GetCurrentDirectory(sizeof(szCurDir) / sizeof (TCHAR), szCurDir);
strTemp = szCurDir;
}
}
else
{
CString strExt = GetExt(fd.cFileName);
if(m_lExt.Find(strExt))
AddToListCtrl(strTemp + _T("\\") + fd.cFileName, strExt);
} }while(::FindNextFile(hFind, &fd)); ::FindClose(hFind);
}
}