雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

VC實(shí)現(xiàn)批量刪除指定文件的方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

本文所述實(shí)例主要實(shí)現(xiàn)了刪除某個(gè)盤符下指定位置的文件,可以是TXT、doc、jpeg等格式,只要選定格式后,再定義好盤符,即可一鍵刪除所有指定類型的文件。再次提示刪除前請(qǐng)確認(rèn),且刪除后不可恢復(fù)。
以下是最主要的核心代碼,其它代碼讀者可以自己添加。

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); //獲取系統(tǒng)文件圖標(biāo)
 nIcon = shInfo.iIcon;
 //設(shè)置COMBOBOXEXITEM結(jié)構(gòu)
 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); //插入數(shù)據(jù)
 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) != "\\") //判斷路徑是否以\結(jié)尾
 strtemp.Format("%s\\*.*",path);//設(shè)置通配符
 else
 strtemp.Format("%s*.*",path);//設(shè)置通配符
 CFileFind findfile;
 BOOL bfind = findfile.FindFile(strtemp);//查找文件
 while (bfind)//循環(huán)查找
 {
 bfind = findfile.FindNextFile();//查找下一個(gè)文件
 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);//獲得文件擴(kuò)展名
 DelFile(path,name);
 MessageBox("已刪除指定類型文件!");
}

這里省去了窗體部分的代碼,進(jìn)行過(guò)VC開發(fā)的朋友應(yīng)該可以看得懂的。

上一篇:c++中虛函數(shù)和純虛函數(shù)的作用與區(qū)別

欄    目:C語(yǔ)言

下一篇:VC6.0常用快捷鍵大全

本文標(biāo)題:VC實(shí)現(xiàn)批量刪除指定文件的方法

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3553.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有