跳转到主要内容

在工作中,我们需要清理一些在程序过程中产生的垃圾或者无需保留文件,但是因为不知情或者经常忘记的缘故,这些文件便积累起来,分布在各个目录中,难于查找和删除。特别是对于CAD作图的人来说,有时候形成一些.dwl, .plt, .err, .log, .ac$, .tmp等之类的文件,长期以来,既不美观,又浪费空间,的确有必要清理一下。而且下面这个程序可以帮你达到目的。网上有这样类似的程序,但可能比较庞杂,不会像这样针对特别目的。

程序截图:

vb-cadcleaner

CADCleaner

很久之前我就写了这个程序,开始时是用VLISP程序写的,后来我在学习VB的过程中,又把程序再次写了一遍,最后我用VC++重写。所以这个程序有三种版本的。这里我先发布VB语言的。

关于这个程序的源码我就不发布了,附件仅仅包含程序。这里贴出一段关键代码,就是用API 查找特定文件的代码:

'**********************************************************************************************************************
'查找函数
Public Function FindFilesAPI(path As String, SearchStr As Variant, FileCount As Long, DirCount As Long) As Double
    Dim FileName    As String           '文件名
    Dim Extension   As String           '文件后缀
    Dim DirName     As String           '子目录名
    Dim dirNames()  As String           '目录数组
    Dim nDir        As Long             '当前路径的目录数
    Dim i           As Long             '循环计数器变量
    Dim hSearch     As Long             '搜索句柄变量
    Dim WFD         As WIN32_FIND_DATA
    Dim Cont        As Long
 
    If Right(path, 1) <> chr(92) Then path = path & chr(92)
    '搜索子目录
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While Cont
            FileName = StripNulls(WFD.cFileName)
            If (FileName <> ".") And (FileName <> "..") Then
                '遍历目录并累计文件总数
                If GetFileAttributes(path & FileName) And FILE_ATTRIBUTE_DIRECTORY Then
                    dirNames(nDir) = FileName
                    DirCount = DirCount + 1
                    nDir = nDir + 1
                    ReDim Preserve dirNames(nDir)
                Else
                    If isCheckAll Or UserForm1.ZERO.Value And WFD.nFileSizeLow = 0 Then
                        If isCheckAll Then
                            FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                        End If
                        FileCount = FileCount + 1
                        UserForm1.List1.AddItem path & FileName
                    Else
                        Extension = Mid(FileName, InStrRev(FileName, ".") + 1)
                        If CheckType(Extension, SearchStr) Then
                            FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                            FileCount = FileCount + 1
                            UserForm1.List1.AddItem path & FileName
                            SendMessage LHwnd, WM_VSCROLL, SB_BOTTOM, 0&
                        End If
                    End If
                End If
            End If
            Cont = FindNextFile(hSearch, WFD)         '获取下一个文件或者文件夹
        Loop
        Cont = FindClose(hSearch)
    End If
    '如果子目录存在则遍历之
    If nDir > 0 And UserForm1.INC.Value Then
        For i = 0 To nDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & chr(92), SearchStr, FileCount, DirCount)
        Next i
    End If
End Function

VC版本的:

int CCADCleanerDlg::Search(CString PathName)
{
	CString FileName;
	HANDLE hScan=NULL;
	WIN32_FIND_DATAW FindFileData;
	if (PathName.Right(1) != (chr(92)))
	{
		hScan = FindFirstFile(PathName +_T("*"),&FindFileData);
	}
	else
	{
		hScan = FindFirstFile(PathName +_T("*"),&FindFileData);
	}
 
	if ( hScan != INVALID_HANDLE_VALUE)
	{
		BOOL cont = TRUE;
		while (cont)
		{
			FileName =  FindFileData.cFileName;
			if (FileName!=_T(".") && FileName!=_T(".."))
			{
				if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					DirCount++;
					Search(PathName+FileName+chr(92));
				}
				else
				{
					if (Check(FileName,FindFileData.nFileSizeLow,FindFileData.nFileSizeHigh))
					{
						//m_strInfo = (PathName+FileName);
						m_list.AddString(PathName+FileName);
						//m_Info.SetWindowText(m_strInfo);
						//m_list.SendMessage(LB_ADDSTRING,NULL,(LPARAM)(FileName.GetBuffer()));
						//SendMessage(m_listWnd,LB_ADDSTRING,NULL,(LPARAM)(FileName.GetBuffer()));
						//m_list.SendMessage(WM_VSCROLL,SB_BOTTOM,NULL);
						FileCount++;
						if (FindFileData.nFileSizeHigh)
						{
							TotalSize += (FindFileData.nFileSizeLow + FindFileData.nFileSizeHigh * FOUR_GB);
						}
						else
						{
							TotalSize += FindFileData.nFileSizeLow;
						}
					}
				}
			}	
			cont = FindNextFile(hScan,&FindFileData);
		}
		FindClose(hScan);
		return TRUE;
	}
	else
	{
		return ERROR;
	}
}

截图:
cadcleaner(VC)
CADCleaner(VC)
当然是用C语言编写的快些了。