|
本帖最后由 hwc2ycy 于 2013-11-17 15:26 编辑
一个文件夹中有多个jpg文件就按 目录名-序号.jpg 方式命名- Sub Main()
- Call ListDirs(ThisWorkbook.Path)
- End Sub
- Sub ListDirs(ByVal Path As String)
- Dim StrFileName$
- Dim arrPath(1 To 1000)
- Dim sPath$
- Dim i&, j&, k As Integer
- i = 1: j = 1: k = 0
- If Not Path Like "*" Then Path = Path & Application.PathSeparator
- arrPath(i) = Path
- On Error Resume Next
- sPath = arrPath(j)
- Do While Len(sPath)
- StrFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
- Do While Len(StrFileName)
- If Not (StrFileName = "." Or StrFileName = "..") Then
- If (GetAttr(sPath & StrFileName) And vbDirectory) = 16 Then
- '避免读取错误
- If Err.Number <> 0 Then Err.Clear: GoTo End1If
- i = i + 1
- arrPath(i) = sPath & StrFileName & Application.PathSeparator
- Else
- If LCase(StrFileName) Like "*.jpg" Then
- k = k + 1
- Debug.Print sPath, StrFileName, k
- Call Rename(sPath, StrFileName, k)
- End If
- End If
- End If
- End1If:
- StrFileName = Dir
- Loop
- j = j + 1
- k = 0
- If j > i Then Exit Do
- sPath = arrPath(j)
- Loop
- End Sub
- Sub Rename(strpath As String, StrFileName As String, k As Integer)
- Dim strFolder$
- Dim arr
- Dim strNewName$
- arr = Split(strpath, "")
- strFolder = arr(UBound(arr) - 1)
- strNewName = strpath & strFolder & "-" & k & Mid(StrFileName, InStrRev(StrFileName, "."))
- Name strpath & StrFileName As strNewName
- End Sub
复制代码 |
评分
-
查看全部评分
|