遍历文件夹/创建目录

本文介绍了一个用于递归搜索指定目录及其所有子目录的C/C++函数,并展示了如何创建目录路径的方法。该函数能够遍历文件系统并收集所有文件的信息,同时排除特定的目录。此外,还提供了一段代码实现创建目录的功能,包括处理UNC路径。

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

SearchSubdir(LPCSTR lpszFolderPath)
{
 char strname[MAX_PATH] = ""; //file name
 char strsearch[MAX_PATH] = ""; //search name
 char strdir[MAX_PATH] = ""; //dir path
 char strfile[MAX_PATH] = ""; //file path

 if (GetFileAttributesA(lpszFolderPath) & FILE_ATTRIBUTE_DIRECTORY)
 {
        strcpy(strsearch, lpszFolderPath);
        HANDLE hFile;
  WIN32_FIND_DATAA wfd;
  strcat(strsearch, "//*.*");
  
  hFile = FindFirstFileA(strsearch, &wfd);
  if (INVALID_HANDLE_VALUE == hFile)
   return;
        do
        {
   if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
   {
    sprintf(strname, wfd.cFileName);
    //排除掉两个文件夹
    if(lstrcmpA(wfd.cFileName, ".") == 0 || lstrcmpA(wfd.cFileName,"..") == 0)  
     continue;  
    
    strcpy(strdir, lpszFolderPath);
    strcat(strdir, "//");
    strcat(strdir, strname);
    SearchSubdir(strdir);
   }
   else
   {  
    memset(strfile, NULL, MAX_PATH);
    strcat(strfile, lpszFolderPath);
    strcat(strfile, "//");
    strcat(strfile, wfd.cFileName);
    
    SetFileAttributesA(strfile, FILE_ATTRIBUTE_NORMAL); 
    m_pClientInfo->fileInfo.fileList.push_back(strfile);
   }  
   
        } while (FindNextFileA(hFile, &wfd));
 }
}

 

 

CreateDirectory(IN LPCTSTR lpszDirName)
{
 LPTSTR p, pszDirCopy;
    DWORD dwAttributes;

    // Make a copy of the string for editing.

    __try
    {
        pszDirCopy = (LPTSTR)malloc(sizeof(TCHAR) * (lstrlen(lpszDirName) + 1));

        if(pszDirCopy == NULL)
            return FALSE;

        lstrcpy(pszDirCopy, lpszDirName);

        p = pszDirCopy;

        //  If the second character in the path is "/", then this is a UNC
        //  path, and we should skip forward until we reach the 2nd / in the path.

        if((*p == TEXT('//')) && (*(p+1) == TEXT('//')))
        {
            p++;            // Skip over the first / in the name.
            p++;            // Skip over the second / in the name.

            //  Skip until we hit the first "/" (//Server/).

            while(*p && *p != TEXT('//'))
            {
                p = CharNext(p);
            }

            // Advance over it.

            if(*p)
            {
                p++;
            }

            //  Skip until we hit the second "/" (//Server/Share/).

            while(*p && *p != TEXT('//'))
            {
                p = CharNext(p);
            }

            // Advance over it also.

            if(*p)
            {
                p++;
            }

        }
        else if(*(p+1) == TEXT(':')) // Not a UNC.  See if it's <drive>:
        {
            p++;
            p++;

            // If it exists, skip over the root specifier

            if(*p && (*p == TEXT('//')))
            {
                p++;
            }
        }

  while(*p)
        {
            if(*p == TEXT('//'))
            {
                *p = TEXT('/0');
                dwAttributes = GetFileAttributes(pszDirCopy);

                // Nothing exists with this name.  Try to make the directory name and error if unable to.
                if(dwAttributes == 0xffffffff)
                {
                    if(!::CreateDirectory(pszDirCopy, NULL))
                    {
                        if(GetLastError() != ERROR_ALREADY_EXISTS)
                        {
                            free(pszDirCopy);
                            return FALSE;
                        }
                    }
                }
                else
                {
                    if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
                    {
                        // Something exists with this name, but it's not a directory... Error
                        free(pszDirCopy);
                        return FALSE;
                    }
                }
 
                *p = TEXT('//');
            }

            p = CharNext(p);
        }

  dwAttributes = GetFileAttributes(pszDirCopy);
  if(dwAttributes == 0xffffffff)
  {
   if(!::CreateDirectory(pszDirCopy, NULL))
   {
    if(GetLastError() != ERROR_ALREADY_EXISTS)
    {
     free(pszDirCopy);
     return FALSE;
    }
   }
        }

    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
        // SetLastError(GetExceptionCode());
        free(pszDirCopy);
        return FALSE;
    }

    free(pszDirCopy);
    return TRUE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值