• 售前

  • 售后

热门帖子
入门百科

VBS批量重定名文件而且操作前备份原有文件

[复制链接]
六翼天使494 显示全部楼层 发表于 2021-8-19 18:56:30 |阅读模式 打印 上一主题 下一主题
焦点函数
  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函数

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作