Board logo

标题: [原创] VBS实现文件夹/文件补0重命名 [打印本页]

作者: 老刘1号    时间: 2019-4-23 19:10     标题: VBS实现文件夹/文件补0重命名

隔壁发去重复,我也来水一贴,
会给目录下的所有文件名/文件夹名中出现的第一个数字补0。
用法:存vbs,要处理的文件/文件夹的父目录拖上去。
文件补0重命名.vbs
  1. Rem Code by OldLiu
  2. Option Explicit
  3. Dim fso,Files
  4. Set fso = CreateObject("Scripting.FileSystemObject")
  5. If wsh.Arguments.Count <> 1 Then wsh.Quit
  6. Set Files = fso.GetFolder(wsh.Arguments(0)).Files
  7. Dim regex
  8. Set regex = New RegExp
  9. regex.Global = False
  10. regex.Pattern = "\D*(\d+)"
  11. Rem 得到最大索引
  12. Dim File,lngMaxIndex,strMatch
  13. lngMaxIndex = 0
  14. For Each File In Files
  15. For Each strMatch In regex.Execute(File.Name)
  16. If CLng(strMatch.Submatches(0)) > lngMaxIndex Then
  17. lngMaxIndex = CLng(strMatch.Submatches(0))
  18. End If
  19. Next
  20. Next
  21. If lngMaxIndex = 0 Then wsh.Quit 1
  22. Rem 计算最大补0数目
  23. Dim bytMaxZeroAdd
  24. bytMaxZeroAdd = Len(CStr(lngMaxIndex))
  25. Rem 重命名处理
  26. regex.Pattern = "(\D*)(\d+)(.*)"
  27. For Each File In Files
  28. For Each strMatch In regex.Execute(File.Name)
  29. File.Move _
  30. File.ParentFolder&"\"& _
  31. strMatch.Submatches(0) & _
  32. String(bytMaxZeroAdd - Len(CStr(CLng(strMatch.Submatches(1)))),"0") & _
  33. CStr(CLng(strMatch.Submatches(1))) & _
  34. strMatch.Submatches(2)
  35. Next
  36. Next
复制代码
文件夹补0重命名.vbs
  1. Rem Code by OldLiu
  2. Option Explicit
  3. Dim fso,Folders
  4. Set fso = CreateObject("Scripting.FileSystemObject")
  5. If wsh.Arguments.Count <> 1 Then wsh.Quit
  6. Set Folders = fso.GetFolder(wsh.Arguments(0)).SubFolders
  7. Dim regex
  8. Set regex = New RegExp
  9. regex.Global = False
  10. regex.Pattern = "\D*(\d+)"
  11. Rem 得到最大索引
  12. Dim Folder,lngMaxIndex,strMatch
  13. lngMaxIndex = 0
  14. For Each Folder In Folders
  15. For Each strMatch In regex.Execute(Folder.Name)
  16. If CLng(strMatch.Submatches(0)) > lngMaxIndex Then
  17. lngMaxIndex = CLng(strMatch.Submatches(0))
  18. End If
  19. Next
  20. Next
  21. If lngMaxIndex = 0 Then wsh.Quit 1
  22. Rem 计算最大补0数目
  23. Dim bytMaxZeroAdd
  24. bytMaxZeroAdd = Len(CStr(lngMaxIndex))
  25. Rem 重命名处理
  26. regex.Pattern = "(\D*)(\d+)(.*)"
  27. For Each Folder In Folders
  28. For Each strMatch In regex.Execute(Folder.Name)
  29. Folder.Move _
  30. Folder.ParentFolder&"\"& _
  31. strMatch.Submatches(0) & _
  32. String(bytMaxZeroAdd - Len(CStr(CLng(strMatch.Submatches(1)))),"0") & _
  33. CStr(CLng(strMatch.Submatches(1))) & _
  34. strMatch.Submatches(2)
  35. Next
  36. Next
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2