VC完成批量刪除指定文件的辦法。本站提示廣大學習愛好者:(VC完成批量刪除指定文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是VC完成批量刪除指定文件的辦法正文
本文所述實例重要完成了刪除某個盤符下指定地位的文件,可所以TXT、doc、jpeg等格局,只需選定格局後,再界說好盤符,便可一鍵刪除一切指定類型的文件。再次提醒刪除前請確認,且刪除後弗成恢復。
以下是最重要的焦點代碼,其它代碼讀者可以本身添加。
SHFILEINFO shInfo; memset(&shInfo,0,sizeof(SHFILEINFO)); HIMAGELIST hImage = (HIMAGELIST)SHGetFileInfo("C:\\",0,&shInfo, sizeof( SHFILEINFO ), SHGFI_SYSICONINDEX | SHGFI_SMALLICON ); m_ImageList.Attach(hImage); m_ComboEx.SetImageList(&m_ImageList); m_ComboEx.ResetContent(); char pchDrives[128] = {0}; char* pchDrive; GetLogicalDriveStrings(sizeof(pchDrives), pchDrives); //羅列盤符 pchDrive = pchDrives; int nItem = 0; while(*pchDrive) { COMBOBOXEXITEM cbi; CString csText; cbi.mask = CBEIF_IMAGE|CBEIF_INDENT|CBEIF_OVERLAY| CBEIF_SELECTEDIMAGE|CBEIF_TEXT; SHFILEINFO shInfo; //界說文件信息 int nIcon; SHGetFileInfo(pchDrive, 0, &shInfo, sizeof(shInfo), SHGFI_ICON|SHGFI_SMALLICON); //獲得體系文件圖標 nIcon = shInfo.iIcon; //設置COMBOBOXEXITEM構造 cbi.iItem = nItem; cbi.pszText = pchDrive; cbi.cchTextMax = strlen(pchDrive); cbi.iImage = nIcon; cbi.iSelectedImage = nIcon; cbi.iOverlay = 0; cbi.iIndent = (0 & 0x03); m_ComboEx.InsertItem(&cbi); //拔出數據 nItem++; pchDrive += strlen(pchDrive) + 1; } return TRUE; // return TRUE unless you set the focus to a control } void CDeleteDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } void CDeleteDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } HCURSOR CDeleteDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDeleteDlg::DelFile(CString path,CString name) { CString strtemp; if (path.Right(1) != "\\") //斷定途徑能否以\開頭 strtemp.Format("%s\\*.*",path);//設置通配符 else strtemp.Format("%s*.*",path);//設置通配符 CFileFind findfile; BOOL bfind = findfile.FindFile(strtemp);//查找文件 while (bfind)//輪回查找 { bfind = findfile.FindNextFile();//查找下一個文件 if(!findfile.IsDots() && !findfile.IsDirectory()) { CString str = findfile.GetFileName(); int index = str.ReverseFind('.'); if(str.Right(str.GetLength()-index) == name) { DeleteFile(findfile.GetFilePath()); } } else if (findfile.IsDots()) { continue; } else if (findfile.IsDirectory())//假如是目次 { DelFile(findfile.GetFilePath(),name);//遞歸查找 } } } void CDeleteDlg::OnButdelete() { // TODO: Add your control notification handler code here CString path,name; m_ComboEx.GetWindowText(path); m_ExName.GetWindowText(name);//取得文件擴大名 DelFile(path,name); MessageBox("已刪除指定類型文件!"); }
這裡省去了窗體部門的代碼,停止過VC開辟的同伙應當可以看得懂的。