|
楼主 |
发表于 2022-4-25 01:14
|
显示全部楼层
- Private dicPath As Object, dicChar As Object
- Private vSubPath As Variant, sMainPath As String
- Sub 删除文件夹及文件()
- Dim sPath As String, nI As Long, nJ As Long, sNewName As String, nPath As Long
-
- Randomize '初始化随机种子
- Set dicChar = CreateObject("Scripting.Dictionary") '初始化记录允许用于文件命名的字符的字典
- sPath = "~!@#$%^&()_+-=`;',.[]{}1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" '允许用于文件命名的字符
- For nI = 1 To Len(sPath)
- dicChar(Mid(sPath, nI, 1)) = 0 '建立允许用于文件命名的字符的字典
- Next
-
- sMainPath = [A3].Value '要删除的主文件夹
- If sMainPath = "" Then Exit Sub '未输入要删除的主文件夹
- If Right(sMainPath, 1) = "" Then sMainPath = Left(sMainPath, Len(sMainPath) - 1) '清理主输入的文件夹名最后一个斜杠
- If Dir(sMainPath, vbDirectory) = "" Then '检查主文件夹是否存在
- MsgBox "输入的主文件夹不存在!“"
- Exit Sub
- End If
-
- nI = Cells(Rows.Count, 1).End(xlUp).Row '获取A列最后一个单元格的行号
- If nI = 5 Then '说明只有一个指定子文件夹
- ReDim vSubPath(1 To 1) '建立单一子文件夹数组
- vSubPath(1) = [A5].Value
- ElseIf nI > 4 Then '说明有多个指定子文件夹
- vSubPath = Application.WorksheetFunction.Transpose([A5].Resize(nI - 4).Value) '获取所有的子文件夹
- End If
-
- Set dicPath = CreateObject("Scripting.Dictionary") '初始化记录用于循环检索子文件夹
- If IsArray(vSubPath) Then '如果子文件夹变量是数组,说明有指定删除的文件夹
- For nI = LBound(vSubPath) To UBound(vSubPath)
- If Dir(sMainPath & "" & vSubPath(nI), vbDirectory) <> "" Then '检查子文件夹存在
- dicPath(sMainPath & "" & vSubPath(nI)) = vSubPath(nI) '字典记录需要查子文件夹的文件夹全称为关键字,子文件夹名为项目值
- End If
- Next
- Else '如果子文件夹变量不是数组,说明要对整个主文件夹进行删除
- vSubPath = Split(sMainPath, "") '将原主文件夹用\拆出各级文件夹的名称
- vSubPath = vSubPath(UBound(vSubPath)) '获取主文件夹名
- sMainPath = Left(sMainPath, Len(sMainPath) - 1 - Len(vSubPath)) '获取主文件的上级文件夹全名
- dicPath(sMainPath & "" & vSubPath) = vSubPath '字典记录需要查子文件夹的文件夹全称为关键字,子文件夹名为项目值
- End If
-
- Do While nPath < dicPath.Count '字典关键字序号小于字典总数
- sPath = dicPath.Keys()(nPath) '获取字典第nPath个需要检索的文件夹名
- SearchDir sPath '检索该文件夹内的文件及子文件夹
- nPath = nPath + 1
- Loop
-
- Do While dicPath.Count > 0 '文件夹字典总数大于0,说明还有文件夹没有删除
- sMainPath = dicPath.Keys()(dicPath.Count - 1) '从最后一个文件夹开始删除,获取删除的文件夹全称
- sPath = dicPath(sMainPath) '获取删除的子文件夹名
- dicPath.Remove sMainPath '清除字典内文件夹名
- sMainPath = Left(sMainPath, Len(sMainPath) - Len(sPath)) '获取子文件夹上级文件夹全称
- sNewName = GetFileName '获取改名用的新名
- Name sMainPath & sPath As sMainPath & sNewName '将子文件夹进行改名
- RmDir sMainPath & sNewName '删除文件夹
- Loop
- MsgBox "删除完毕!"
- End Sub
- Sub SearchDir(ByVal sPath As String)
- '对指定文件夹进行检索文件及子文件夹
- Dim sFile As Variant, nI As Long, sNewName As String
-
- sPath = sPath & ""
- sFile = Dir(sPath & "*.*", vbDirectory + vbSystem + vbHidden) '检索文件夹内所有文件夹及文件
- Do While sFile <> ""
- If sFile <> "." And sFile <> ".." Then
- If (GetAttr(sPath & sFile) And vbDirectory) = vbDirectory Then
- If (GetAttr(sPath & sFile) And vbHidden) = vbHidden Then '如果是系统隐藏文件夹
- SetAttr sPath & sFile, vbNormal '设置文件为正常文件夹
- End If
- dicPath(sPath & sFile) = sFile '记录到文件夹字典
- Else
- If (GetAttr(sPath & sFile) And (vbSystem + vbHidden)) = vbSystem + vbHidden Then '如果是系统隐藏文件
- SetAttr sPath & sFile, vbNormal '设置文件为正常文件
- sNewName = sFile
- Else
- sNewName = GetFileName '获取改名用的新名
- If (GetAttr(sPath & sFile) And vbHidden) = vbHidden Then '如果是隐藏文件
- SetAttr sPath & sFile, vbNormal '设置文件为正常文件
- End If
- Name sPath & sFile As sPath & sNewName '文件改名
- End If
- Kill sPath & sNewName '删除文件
- End If
- End If
- sFile = Dir '检索下一个文件夹或文件
- Loop
- End Sub
- Private Function GetFileName() As String
- '建立随机文件名
- Dim nI As Long, nLen As Long, sFile As String
-
- nLen = Int(30 * Rnd) + 20 '获取一个大于大于20,小于50的长度
- For nI = 1 To nLen
- sFile = sFile & dicChar.Keys()(Int((dicChar.Count - 1) * Rnd)) '随机取一个允许字符加到文件名
- Next
- GetFileName = sFile
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|