VC to achieve batch delete specified file method

  • 2020-04-02 02:28:28
  • OfStack

The example described in this paper mainly realizes the deletion of a disk character under the specified location of the file, can be TXT, doc, jpeg format, as long as the format is selected, and then define a good disk character, can be a key to delete all specified types of files. Please confirm before the prompt is deleted again, and it cannot be recovered after the deletion.
The following is the main core code that readers can add to.


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); //List the drive
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; //Define file information
 int nIcon;
 SHGetFileInfo(pchDrive, 0, &shInfo, sizeof(shInfo),
  SHGFI_ICON|SHGFI_SMALLICON); //Gets the system file icon
 nIcon = shInfo.iIcon;
 //Set the COMBOBOXEXITEM structure
 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); //Insert data
 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) != "\") //Determines if the path ends with 
 strtemp.Format("%s\*.*",path);//Set the wildcard character
 else
 strtemp.Format("%s*.*",path);//Set the wildcard character
 CFileFind findfile;
 BOOL bfind = findfile.FindFile(strtemp);//Find files
 while (bfind)//Loop through
 {
 bfind = findfile.FindNextFile();//Find the next file
 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())//If it's a directory
 {
  DelFile(findfile.GetFilePath(),name);//Recursive search
 }
 }
}
void CDeleteDlg::OnButdelete() 
{
 // TODO: Add your control notification handler code here
 CString path,name;
 m_ComboEx.GetWindowText(path);
 m_ExName.GetWindowText(name);//Gets the file extension
 DelFile(path,name);
 MessageBox(" The specified type file has been deleted! ");
}

Here saved the form part of the code, the VC development of friends should be able to understand.


Related articles: