经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP/ADO/VBScript » 查看文章
VBS批量重命名文件并且操作前备份原有文件
来源:jb51  时间:2019/9/29 8:38:40  对本文有异议

核心函数

  1. '==========================================================================
  2. '
  3. ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
  4. '
  5. ' NAME:
  6. '
  7. ' AUTHOR: Microsoft , Microsoft
  8. ' DATE : 2014/7/9
  9. '
  10. ' COMMENT: '批量修改文件夹下对应的所有文件名
  11. '
  12. '==========================================================================
  13. '选择我的电脑作为根目录,来选择目录
  14. Const MY_COMPUTER = &H11&
  15. Const WINDOW_HANDLE = 0
  16. Const OPTIONS = 0
  17. Set objShell = CreateObject("Shell.Application")
  18. Set objFolder = objShell.Namespace(MY_COMPUTER)
  19. Set objFolderItem = objFolder.Self
  20. strPath = objFolderItem.Path
  21. Set objShell = CreateObject("Shell.Application")
  22. Set objFolder = objShell.BrowseForFolder _
  23. (WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath)
  24. If objFolder Is Nothing Then
  25. Wscript.Quit
  26. End If
  27. Set objFolderItem = objFolder.Self
  28. objPath = objFolderItem.Path
  29. 'MsgBox objFolderItem.name
  30. '===================================================================
  31. '选择指定盘符下的目录
  32. ' Const WINDOW_HANDLE = 0
  33. ' Const OPTIONS = 0
  34. '
  35. ' Set objShell = CreateObject("Shell.Application")
  36. ' Set objFolder = objShell.BrowseForFolder _
  37. ' (WINDOW_HANDLE, "Select a folder:", OPTIONS, "C:\")
  38. '
  39. ' If objFolder Is Nothing Then
  40. ' Wscript.Quit
  41. ' End If
  42. '
  43. ' Set objFolderItem = objFolder.Self
  44. ' objPath = objFolderItem.Path
  45. '
  46. ' MsgBox objPath
  47. '=========================================================================
  48. '定义变量
  49. dim file_path,prefix_name,suffix_name,repeat_name,repeat_edit
  50. Dim OneLine,TwoLine,ThreeLine,FourLine,FiveLine
  51. i=0
  52. test = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
  53. 'Wscript.echo test
  54. filepath=test&"\config.ini"
  55. 'WScript.Echo filepath
  56. ' file_path = "C:\Users\Administrator\Desktop\1\music"'目标文件夹的路径
  57. dst_file_path="C:\"&objFolderItem.name&"_bak"
  58. file_path=objPath
  59. '-----得到文件夹路径,且打开配置文件
  60. Set fso = CreateObject("Scripting.FileSystemObject")
  61. Set folder = fso.getfolder(file_path)
  62. Set fs = folder.files
  63. Set file=fso.OpenTextFile(filepath,1)
  64. '----------------在操作前,备份一下原有的文件
  65. fso.CopyFolder file_path,dst_file_path,True
  66. '----------------------------------
  67. '取出第一行中的两个数
  68. OneLine=file.ReadLine
  69. OneLineStr=Split(OneLine,"=")
  70. OneLineCount=UBound(split(OneLine,"="))
  71. For i1=0 To OneLineCount
  72. 'WScript.Echo OneLineStr(i1)
  73. Next
  74. '-------------------------------------
  75. '取出第二行中的两个数
  76. TwoLine=file.ReadLine
  77. TwoLineStr=Split(TwoLine,"=")
  78. TwoLineCount=UBound(split(TwoLine,"="))
  79. For i2=0 To TwoLineCount
  80. 'WScript.Echo TwoLineStr(i2)
  81. Next
  82. '-------------------------------------------
  83. '取出第三行中的两个数
  84. ThreeLine=file.ReadLine
  85. ThreeLineStr=Split(ThreeLine,"=")
  86. ThreeLineCount=UBound(split(ThreeLine,"="))
  87. For i3=0 To ThreeLineCount
  88. 'WScript.Echo ThreeLineStr(i3)
  89. Next
  90. '-------------------------------------------
  91. '取出第四行中的两个数
  92. FourLine=file.ReadLine
  93. FourLineStr=Split(FourLine,"=")
  94. FourLineCount=UBound(split(FourLine,"="))
  95. For i4=0 To FourLineCount
  96. 'WScript.Echo FourLineStr(i4)
  97. Next
  98. '-----------------------------------------
  99. '取出第五行中的两个数
  100. FiveLine=file.ReadLine
  101. FiveLineStr=Split(FiveLine,"=")
  102. FiveLineCount=Ubound(split(FiveLine,"="))
  103. For i5=0 To FiveLineCount
  104. 'WScript.Echo FiveLineStr(i5)
  105. Next
  106. '---------------------------------------------
  107. '调用过程
  108. 'Function_Main()
  109. Function Function_Main()
  110. If OneLineStr(1)="true" Then
  111. Function_Prefix_Name()
  112. Elseif OneLineStr(1)="false" Then
  113. Function_Suffix_Name()
  114. Elseif OneLineStr(1)="number" Then
  115. Function_Number_Value()
  116. Elseif OneLineStr(1)="array" Then
  117. Function_MyArrayReName()
  118. Elseif OneLineStr(1)="" Then
  119. WScript.Quit
  120. End If
  121. End Function
  122. '-----------------------------------------
  123. '在原有名称前增加前缀
  124. Function Function_Prefix_Name()
  125. For Each file in fs
  126. File.Name=TwoLineStr(1)&File.Name
  127. Next
  128. End Function
  129. '--------------------------------------
  130. '在原有名称前增加后缀
  131. Function Function_Suffix_Name()
  132. For Each file in fs
  133. Name=Mid(file.name,1,instrrev(file.name,".")-1) '取到.号前面的文件名
  134. Format=Mid(file.name,instrrev(file.name,".")) '取到.号后面的后缀格式
  135. file.Name=Name&ThreeLineStr(1)&Format
  136. Next
  137. End Function
  138. '--------------------------------------------
  139. '在原有名称前增加有序自增数字
  140. Function Function_Number_Value()
  141. For Each file In fs
  142. FourLineStr(1)=FourLineStr(1)+1
  143. file.name=FourLineStr(1)&file.name
  144. Next
  145. End Function
  146. 'Function_Suffix_Name()
  147. '--------------------------------------------------
  148. '批量更改文件名称
  149. Function Function_MyArrayReName()
  150. Const BeforAlarm="发生犯人暴狱,请注意观察"
  151. Const AfterAlarm="发生犯人暴狱,各小组按预案处置"
  152. Dim MyArray(12)
  153. n=1
  154. y=0
  155. For i=0 To 12
  156. If i=11 Then
  157. MyArray(i)="监门哨"
  158. Elseif i=12 Then
  159. MyArray(i)="自卫哨"
  160. Else
  161. MyArray(i)=n&"号哨"
  162. n=n+1
  163. End If
  164. ' WScript.Echo MyArray(i)
  165. Next
  166. For Each file In fs
  167. Format=Mid(file.name,instrrev(file.name,"."))
  168. 'MsgBox Format
  169. 'MsgBox MyArray(y)
  170. If FiveLineStr(1)="before" Then
  171. file.name=MyArray(y)&BeforAlarm&Format
  172. Elseif FiveLineStr(1)="after" Then
  173. file.name=MyArray(y)&AfterAlarm&Format
  174. Else
  175. MsgBox "请先设置是确认前还是确认后!",,"提示"
  176. WScript.Quit
  177. End If
  178. y=y+1
  179. 'WScript.Echo file.name
  180. Next
  181. End Function
  182. '=======================================================================
  183. ' If prefix_name <> "" then'批量加前缀
  184. ' For each f in fs
  185. ' f.name = prefix_name&f.name
  186. ' Next
  187. ' End If
  188. '
  189. ' if suffix_name <> "" then'批量加后缀
  190. ' For each f in fs
  191. ' name = Mid(f.name,1,InstrRev(f.name,".")-1)
  192. ' format = Mid(f.name,InstrRev(f.name,"."))
  193. ' f.name = name & suffix_name & format
  194. ' Next
  195. ' end If
  196. '
  197. ' if repeat_name <> "" then'批量删除相同字符
  198. ' For each f in fs
  199. ' On Error Resume Next
  200. ' f.name = Replace(f.name,repeat_name,repeat_edit)
  201. ' Next
  202. ' end If
  203. ' '-----文件操作结束
  204. '
  205. ' set fso = nothing'释放内存
  206. '
  207. ' MsgBox("完成!")

需用用到配置文件

  1. config.ini文件内容:
  2. statue=
  3. prefix_name=[320kbp]
  4. suffix_name=[结束]
  5. i=20140100
  6. array=

参数配置使用方法:

statue=true时为增加前缀
statue=false时为增加后缀
statue=number 时为增加有序自增数字。
statue=array 为调用数组函数
statue=空值时为空,不作处理,退出脚本操作。
array=before时,设置为确认前。
array=after时,设置为确认后。
array=空时,弹出提示信息,退出脚本操作。

好了这篇文章就介绍到这了,主要用到了FileSystemObject与mid函数

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号