C/C++

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C/C++ >

C++_VC实现批量删除指定文件的方法,本文所述实例主要实现了删除

来源:网络整理  作者:  发布时间:2020-12-20 14:48
VC实现批量删除指定文件的方法,本文所述实例主要实现了删除某个盘符下指定位置的文件,可以是TXT、doc、jpeg等格式...
以下是最主要的核心代码,其它代码读者可以自己添加。

本文所述实例主要实现了删除某个盘符下指定位置的文件,可以是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开发的朋友应该可以看得懂的。

相关热词: 方法 C++ 实例

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/cjj/6141.shtml

最新文章
只需要在调用Ctrl+B编译后 只需要在调用Ctrl+B编译后

时间:2021-01-13

OpenGL超级宝典visual studio OpenGL超级宝典visual studio

时间:2021-01-04

Directx11 教程(2) 基本的wi Directx11 教程(2) 基本的wi

时间:2021-01-04

LeetCode11ContainerWithMostWate LeetCode11ContainerWithMostWate

时间:2021-01-04

C语言简单IT之家速成 C语言简单IT之家速成

时间:2020-12-27

三分钟了解Activity工作流 三分钟了解Activity工作流

时间:2020-12-27

编译器是如何实现32位整型 编译器是如何实现32位整型

时间:2020-12-27

C++中lower_bound函数和upper C++中lower_bound函数和upper

时间:2020-12-27

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C++_VC实现批量删除指定文件的方法,本文所述实例主要实现了删除

2020-12-20 编辑:

以下是最主要的核心代码,其它代码读者可以自己添加。

本文所述实例主要实现了删除某个盘符下指定位置的文件,可以是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开发的朋友应该可以看得懂的。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/cjj/6141.shtml

相关文章

风云图片

推荐阅读

返回C/C++频道首页