Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2358|回复: 5

[已解决]求大神给个代码

[复制链接]
发表于 2013-11-17 14:27 | 显示全部楼层 |阅读模式
有很多个文件夹,需要把每个文件夹的图片的名字改成对应的文件夹名字,图片名字没有规律
比如A文件夹里的图片名字改为A.jpg
B文件夹里的图片名字改为B.jpg
感谢大神

最佳答案
2013-11-17 15:25
只有一个就用这个。
  1. Sub Main()
  2.     Call ListDirs(ThisWorkbook.Path)
  3. End Sub

  4. Sub ListDirs(ByVal Path As String)
  5.     Dim StrFileName$
  6.     Dim arrPath(1 To 1000)
  7.     Dim sPath$
  8.     Dim i&, j&

  9.     i = 1: j = 1

  10.     If Not Path Like "*" Then Path = Path & Application.PathSeparator
  11.     arrPath(i) = Path

  12.     On Error Resume Next

  13.     sPath = arrPath(j)

  14.     Do While Len(sPath)
  15.         StrFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
  16.         Do While Len(StrFileName)

  17.             If Not (StrFileName = "." Or StrFileName = "..") Then
  18.                 If (GetAttr(sPath & StrFileName) And vbDirectory) = 16 Then
  19.                     '避免读取错误
  20.                     If Err.Number <> 0 Then Err.Clear: GoTo End1If
  21.                     i = i + 1
  22.                     arrPath(i) = sPath & StrFileName & Application.PathSeparator
  23.                 Else

  24.                     If LCase(StrFileName) Like "*.jpg" Then
  25.                         Call Rename(sPath, StrFileName)
  26.                     End If
  27.                 End If
  28.             End If
  29. End1If:
  30.             StrFileName = Dir
  31.         Loop

  32.         j = j + 1
  33.         If j > i Then Exit Do
  34.         sPath = arrPath(j)
  35.     Loop
  36. End Sub

  37. Sub Rename(strpath As String, StrFileName As String)
  38.     Dim strFolder$
  39.     Dim arr
  40.     Dim strNewName$
  41.     arr = Split(strpath, "")
  42.     strFolder = arr(UBound(arr) - 1)
  43.     strNewName = strpath & strFolder & Mid(StrFileName, InStrRev(StrFileName, "."))
  44.     Name strpath & StrFileName As strNewName
  45. End Sub
复制代码

新建文件夹 (4).rar

14.56 KB, 下载次数: 8

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-11-17 14:47 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2013-11-17 14:50 | 显示全部楼层
每个文件夹只有一个图片,没有其他文件
回复

使用道具 举报

发表于 2013-11-17 15:23 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-11-17 15:26 编辑

一个文件夹中有多个jpg文件就按 目录名-序号.jpg 方式命名
  1. Sub Main()
  2.     Call ListDirs(ThisWorkbook.Path)
  3. End Sub

  4. Sub ListDirs(ByVal Path As String)
  5.     Dim StrFileName$
  6.     Dim arrPath(1 To 1000)
  7.     Dim sPath$
  8.     Dim i&, j&, k As Integer

  9.     i = 1: j = 1: k = 0

  10.     If Not Path Like "*" Then Path = Path & Application.PathSeparator
  11.     arrPath(i) = Path

  12.     On Error Resume Next

  13.     sPath = arrPath(j)

  14.     Do While Len(sPath)
  15.         StrFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
  16.         Do While Len(StrFileName)

  17.             If Not (StrFileName = "." Or StrFileName = "..") Then
  18.                 If (GetAttr(sPath & StrFileName) And vbDirectory) = 16 Then
  19.                     '避免读取错误
  20.                     If Err.Number <> 0 Then Err.Clear: GoTo End1If
  21.                     i = i + 1
  22.                     arrPath(i) = sPath & StrFileName & Application.PathSeparator
  23.                 Else

  24.                     If LCase(StrFileName) Like "*.jpg" Then
  25.                         k = k + 1
  26.                         Debug.Print sPath, StrFileName, k
  27.                         Call Rename(sPath, StrFileName, k)
  28.                     End If
  29.                 End If
  30.             End If
  31. End1If:
  32.             StrFileName = Dir
  33.         Loop

  34.         j = j + 1
  35.         k = 0
  36.         If j > i Then Exit Do
  37.         sPath = arrPath(j)
  38.     Loop
  39. End Sub

  40. Sub Rename(strpath As String, StrFileName As String, k As Integer)
  41.     Dim strFolder$
  42.     Dim arr
  43.     Dim strNewName$
  44.     arr = Split(strpath, "")
  45.     strFolder = arr(UBound(arr) - 1)
  46.     strNewName = strpath & strFolder & "-" & k & Mid(StrFileName, InStrRev(StrFileName, "."))
  47.     Name strpath & StrFileName As strNewName
  48. End Sub
复制代码

评分

参与人数 1 +6 收起 理由
yyyydddd8888 + 6 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2013-11-17 15:25 | 显示全部楼层    本楼为最佳答案   
只有一个就用这个。
  1. Sub Main()
  2.     Call ListDirs(ThisWorkbook.Path)
  3. End Sub

  4. Sub ListDirs(ByVal Path As String)
  5.     Dim StrFileName$
  6.     Dim arrPath(1 To 1000)
  7.     Dim sPath$
  8.     Dim i&, j&

  9.     i = 1: j = 1

  10.     If Not Path Like "*" Then Path = Path & Application.PathSeparator
  11.     arrPath(i) = Path

  12.     On Error Resume Next

  13.     sPath = arrPath(j)

  14.     Do While Len(sPath)
  15.         StrFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
  16.         Do While Len(StrFileName)

  17.             If Not (StrFileName = "." Or StrFileName = "..") Then
  18.                 If (GetAttr(sPath & StrFileName) And vbDirectory) = 16 Then
  19.                     '避免读取错误
  20.                     If Err.Number <> 0 Then Err.Clear: GoTo End1If
  21.                     i = i + 1
  22.                     arrPath(i) = sPath & StrFileName & Application.PathSeparator
  23.                 Else

  24.                     If LCase(StrFileName) Like "*.jpg" Then
  25.                         Call Rename(sPath, StrFileName)
  26.                     End If
  27.                 End If
  28.             End If
  29. End1If:
  30.             StrFileName = Dir
  31.         Loop

  32.         j = j + 1
  33.         If j > i Then Exit Do
  34.         sPath = arrPath(j)
  35.     Loop
  36. End Sub

  37. Sub Rename(strpath As String, StrFileName As String)
  38.     Dim strFolder$
  39.     Dim arr
  40.     Dim strNewName$
  41.     arr = Split(strpath, "")
  42.     strFolder = arr(UBound(arr) - 1)
  43.     strNewName = strpath & strFolder & Mid(StrFileName, InStrRev(StrFileName, "."))
  44.     Name strpath & StrFileName As strNewName
  45. End Sub
复制代码

评分

参与人数 1 +6 收起 理由
yyyydddd8888 + 6 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-11-17 15:31 | 显示全部楼层
hwc2ycy 发表于 2013-11-17 15:25
只有一个就用这个。

太感谢了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 13:01 , Processed in 0.842940 second(s), 16 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表