• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    VBS批量重命名文件并且操作前备份原有文件

    发布者: 皮3591 | 发布时间: 2025-8-14 04:05| 查看数: 10| 评论数: 0|帖子模式

    VBS 版批量重命名
    1. rem 读取配置文件
    2. Dim config
    3. config = "conf.txt"
    4. set fso=createobject("scripting.filesystemobject")  
    5. set a=createobject("scripting.dictionary")  
    6. set file=fso.opentextfile(config)  
    7. do while file.atendofstream<>true  
    8.   m=m+1  
    9.   a.add m,file.readline  
    10. src =  a(m)
    11. RenameSubPage src
    12. loop  
    13. file.close  
    14. Set fso =Nothing
    15. msgbox  "操作已完成" ,4096,"文件重命名"

    16. Sub RenameSubPage(strURL)
    17. Dim path
    18. For i=19 To 100
    19.   path = Replace(strURL , ".html", "_"& i & ".html")
    20.   If fso.fileexists(path) =True Then
    21.    target = path & ".tmp"
    22.    fso.movefile path , target
    23.   Else
    24.    ' do nothing
    25.   End If
    26. Next
    27. End Sub
    复制代码
    注释: conf.txt 文件内容如下:
    D:\a\b\c.html
    D:\d\e\f.html
    其它代码

    核心函数
    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函数

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

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