• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            c++ file and directory 刪除,移動,目錄瀏覽對話框,找某目錄下的所有文件(包括子目錄)

            Delete folders, subfolders and files easily

            <PRE>void RecursiveDelete(CString szPath)
            {
                CFileFind ff;
                CString path 
            = szPath;
                
                
            if(path.Right(1!= "\\")
                    path 
            += "\\";

                path 
            += "*.*";

                BOOL res 
            = ff.FindFile(path);

                
            while(res)
                
            {
                    res 
            = ff.FindNextFile();
                    
            if (!ff.IsDots() && !ff.IsDirectory())
                        DeleteFile(ff.GetFilePath());
                    
            else if (ff.IsDirectory())
                    
            {
                        path 
            = ff.GetFilePath();
                        RecursiveDelete(path);
                        RemoveDirectory(path);
                    }

                }

            }
            </PRE>

            The CreateDir function creates folders and subfolders thereby completing the whole path passed to it. If the folder already exists, it is left unaffected, but if it doesn't exist, it is created. The CreateDirectory WIN32 API lets us create a directory, but it works only if the parent directory already exists. This function overcomes this limitation.

            <PRE>void CreateDir(char* Path)
            {
             
            char DirName[256];
             
            char* p = Path;
             
            char* q = DirName; 
             
            while(*p)
             
            {
               
            if (('\\' == *p) || ('/' == *p))
               
            {
                 
            if (':' != *(p-1))
                 
            {
                    CreateDirectory(DirName, NULL);
                 }

               }

               
            *q++ = *p++;
               
            *= '\0';
             }

             CreateDirectory(DirName, NULL);
            }
            </PRE>

            The DeleteAllFiles function deletes all the files (not folders) present in the specified path:

            <PRE>void DeleteAllFiles(char* folderPath)
            {
             
            char fileFound[256];
             WIN32_FIND_DATA info;
             HANDLE hp; 
             sprintf(fileFound, 
            "%s\\*.*", folderPath);
             hp 
            = FindFirstFile(fileFound, &info);
             
            do
                
            {
                   sprintf(fileFound,
            "%s\\%s", folderPath, info.cFileName);
                   DeleteFile(fileFound);
             
                }
            while(FindNextFile(hp, &info)); 
             FindClose(hp);
            }
            </PRE>


            The EmptyDirectory function deletes all the contents from a specified directory. The RemoveDirectory WIN32 API deletes an existing empty directory, but it doesn't work if the directory isn't empty. This function overcomes this limitation:

            <PRE>void EmptyDirectory(char* folderPath)
            {
             
            char fileFound[256];
             WIN32_FIND_DATA info;
             HANDLE hp; 
             sprintf(fileFound, 
            "%s\\*.*", folderPath);
             hp 
            = FindFirstFile(fileFound, &info);
             
            do
                
            {
                    
            if (!((strcmp(info.cFileName, ".")==0)||
                          (strcmp(info.cFileName, 
            "..")==0)))
                    
            {
                      
            if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
                                                 FILE_ATTRIBUTE_DIRECTORY)
                      
            {
                          
            string subFolder = folderPath;
                          subFolder.append(
            "\\");
                          subFolder.append(info.cFileName);
                          EmptyDirectory((
            char*)subFolder.c_str());
                          RemoveDirectory(subFolder.c_str());
                      }

                      
            else
                      
            {
                          sprintf(fileFound,
            "%s\\%s", folderPath, info.cFileName);
                          BOOL retVal 
            = DeleteFile(fileFound);
                      }

                    }

             
                }
            while(FindNextFile(hp, &info)); 
             FindClose(hp);
            }
            </PRE>

            瀏覽目錄dialog:
            void CTestBrowseDlg::OnBrowse() 
            {
                CString str;
                BROWSEINFO bi;
                
            char name[MAX_PATH];
                ZeroMemory(
            &bi,sizeof(BROWSEINFO));
                bi.hwndOwner
            =GetSafeHwnd();
                bi.pszDisplayName
            =name;
                bi.lpszTitle
            ="Select folder";
                bi.ulFlags
            =BIF_USENEWUI;
                LPITEMIDLIST idl
            =SHBrowseForFolder(&bi);
                
            if(idl==NULL)
                    
            return;
                SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
                str.ReleaseBuffer();
                m_Path
            =str;
                
            if(str.GetAt(str.GetLength()-1)!='\\')
                    m_Path
            +="\\";
                UpdateData(FALSE);
            }

            得到某目錄下的所有文件:
            void RecursiveDelete(CString szPath)
            {
                CFileFind ff;
                CString path 
            = szPath;

                
            if(path.Right(1!= "\\")
                    path 
            += "\\";

                path 
            += "*.*";

                BOOL res 
            = ff.FindFile(path);

                
            while(res)
                
            {
                    res 
            = ff.FindNextFile();
                    
            if ( !ff.IsDots()&&ff.IsDirectory())
                    
            {
                        
                        path 
            = ff.GetFilePath();
                        RecursiveDelete(path);
                    }

                    
            else if (!ff.IsDirectory()&&!ff.IsDots())
                    
            {
                        CString ss ; ss
            = ff.GetFileName();
                        printf(
            "%s\n",ss);
                        
                    }

                }

                ff.Close();
            }

            posted on 2006-01-12 14:52 夢在天涯 閱讀(5803) 評論(4)  編輯 收藏 引用 所屬分類: CPlusPlusMFC/QT

            評論

            # re: c++ file and directory 2006-01-12 15:12 小明

            一點(diǎn)小問題:

            因?yàn)镈eleteFile不能Delete掉只讀文件,所以最好在DeleteFile之前,使用SetFileAttributes(file,FILE_ATTRIBUTE_NORMAL);  回復(fù)  更多評論   

            # re: c++ file and directory 2006-01-12 16:39 夢在天涯

            也可以用searchpath()來找,是嗎?可惜就是沒有找到例子!
            那位有的話,貢獻(xiàn)一下啊,謝謝!  回復(fù)  更多評論   

            # re: c++ file and directory 2006-02-09 16:49 夢在天涯

            -------------------------------------------------------------------
            、獲得當(dāng)前應(yīng)用程序路徑
            #include < windows.h >
            #include < string.h >

            HINSTANCE hInst;
            char szBuf[256];
            char *p;

            //拿到全部路徑
            GetModuleFileName(hInst,szBuf,sizeof(szBuf));

            //分離路徑和文件名。
            p = szBuf;
            while(strchr(p,'\\')) {
            p = strchr(p,'\\');
            p++;
            }
            *p = '\0';
            //路徑在szBuf理了。  回復(fù)  更多評論   

            # re: c++ file and directory 2006-02-20 12:01 blue_bean

            用SHFileOperation,可以刪除只讀文件。


            SHFILEOPSTRUCT op;
            memset(&op, 0, sizeof(op));
            op.pFrom = "c:\\text.ini";
            op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
            op.wFunc = FO_DELETE;
            if (SHFileOperation(&op) != 0)
            {
            // delete error
            }   回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            97久久婷婷五月综合色d啪蜜芽 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 亚洲中文字幕久久精品无码喷水| 久久久久国产精品人妻| 久久av无码专区亚洲av桃花岛| 色婷婷综合久久久久中文一区二区| 1000部精品久久久久久久久| 国产精品99久久精品| 欧美大战日韩91综合一区婷婷久久青草 | 精品久久久久久国产91| 久久无码人妻精品一区二区三区| 久久久亚洲AV波多野结衣 | 色欲久久久天天天综合网| 久久亚洲精品视频| 色青青草原桃花久久综合| 国产精品久久成人影院| 久久成人小视频| 久久久久人妻一区二区三区vr| 久久久这里有精品中文字幕| 亚洲AV无码久久精品色欲| 久久久久99精品成人片三人毛片 | 久久精品中文字幕久久| 2021最新久久久视精品爱| 韩国无遮挡三级久久| 久久人人爽人人爽人人AV东京热 | 久久91精品综合国产首页| 日韩精品久久久久久免费| 亚洲精品WWW久久久久久| 久久精品国产秦先生| 久久w5ww成w人免费| 欧美va久久久噜噜噜久久| 亚洲精品国精品久久99热| 久久精品无码一区二区app| 久久国产高清字幕中文| 99久久中文字幕| 久久精品亚洲一区二区三区浴池| 色天使久久综合网天天| 无码国内精品久久综合88 | 青草国产精品久久久久久| 奇米影视7777久久精品| 久久天天躁狠狠躁夜夜躁2O2O|