当前位置: 代码网 > it编程>前端脚本>其它脚本 > VBS批量重命名文件并且操作前备份原有文件

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

2024年05月18日 其它脚本 我要评论
vbs 版批量重命名rem 读取配置文件 dim config config = "conf.txt" set fso=createobject("scripting.filesystemobjec

vbs 版批量重命名

rem 读取配置文件
 dim config 
 config = "conf.txt"
 set fso=createobject("scripting.filesystemobject")  
 set a=createobject("scripting.dictionary")  
 set file=fso.opentextfile(config)  
 do while file.atendofstream<>true  
  m=m+1  
  a.add m,file.readline  
 src =  a(m)
 renamesubpage src
loop  
file.close  
set fso =nothing 
msgbox  "操作已完成" ,4096,"文件重命名"

sub renamesubpage(strurl)
 dim path
 for i=19 to 100
  path = replace(strurl , ".html", "_"& i & ".html")
  if fso.fileexists(path) =true then 
   target = path & ".tmp"
   fso.movefile path , target
  else 
   ' do nothing 
  end if 
 next 
end sub 

注释: conf.txt 文件内容如下:
d:\a\b\c.html
d:\d\e\f.html

其它代码

核心函数

'==========================================================================
'
' vbscript source file -- created with sapien technologies primalscript 4.0
'
' name: 
'
' author: microsoft , microsoft
' date : 2014/7/9
'
' comment: '批量修改文件夹下对应的所有文件名
'
'==========================================================================
'选择我的电脑作为根目录,来选择目录
const my_computer = &h11&
const window_handle = 0
const options = 0
set objshell = createobject("shell.application")
set objfolder = objshell.namespace(my_computer)
set objfolderitem = objfolder.self
strpath = objfolderitem.path
set objshell = createobject("shell.application")
set objfolder = objshell.browseforfolder _
(window_handle, "select a folder:", options, strpath)
if objfolder is nothing then
wscript.quit
end if
set objfolderitem = objfolder.self
objpath = objfolderitem.path
'msgbox objfolderitem.name
'===================================================================
'选择指定盘符下的目录
' const window_handle = 0
' const options = 0
' 
' set objshell = createobject("shell.application")
' set objfolder = objshell.browseforfolder _
' (window_handle, "select a folder:", options, "c:\")
' 
' if objfolder is nothing then
' wscript.quit
' end if
' 
' set objfolderitem = objfolder.self
' objpath = objfolderitem.path
' 
' msgbox objpath
'=========================================================================
'定义变量
dim file_path,prefix_name,suffix_name,repeat_name,repeat_edit
dim oneline,twoline,threeline,fourline,fiveline
i=0
test = createobject("scripting.filesystemobject").getfile(wscript.scriptfullname).parentfolder.path
'wscript.echo test
filepath=test&"\config.ini"
'wscript.echo filepath
' file_path = "c:\users\administrator\desktop\1\music"'目标文件夹的路径
dst_file_path="c:\"&objfolderitem.name&"_bak"
file_path=objpath
'-----得到文件夹路径,且打开配置文件
set fso = createobject("scripting.filesystemobject") 
set folder = fso.getfolder(file_path) 
set fs = folder.files
set file=fso.opentextfile(filepath,1)
'----------------在操作前,备份一下原有的文件
fso.copyfolder file_path,dst_file_path,true
'----------------------------------
'取出第一行中的两个数
oneline=file.readline
onelinestr=split(oneline,"=")
onelinecount=ubound(split(oneline,"="))
for i1=0 to onelinecount
'wscript.echo onelinestr(i1)
next 
'-------------------------------------
'取出第二行中的两个数
twoline=file.readline
twolinestr=split(twoline,"=")
twolinecount=ubound(split(twoline,"="))
for i2=0 to twolinecount
'wscript.echo twolinestr(i2)
next 
'-------------------------------------------
'取出第三行中的两个数
threeline=file.readline
threelinestr=split(threeline,"=")
threelinecount=ubound(split(threeline,"="))
for i3=0 to threelinecount
'wscript.echo threelinestr(i3)
next 
'-------------------------------------------
'取出第四行中的两个数
fourline=file.readline
fourlinestr=split(fourline,"=")
fourlinecount=ubound(split(fourline,"="))
for i4=0 to fourlinecount
'wscript.echo fourlinestr(i4)
next
'-----------------------------------------
'取出第五行中的两个数
fiveline=file.readline
fivelinestr=split(fiveline,"=")
fivelinecount=ubound(split(fiveline,"="))
for i5=0 to fivelinecount
'wscript.echo fivelinestr(i5)
next 
'---------------------------------------------
'调用过程
'function_main()
function function_main()
if onelinestr(1)="true" then
function_prefix_name()
elseif onelinestr(1)="false" then
function_suffix_name()
elseif onelinestr(1)="number" then	
function_number_value()
elseif onelinestr(1)="array" then	
function_myarrayrename()
elseif onelinestr(1)="" then
wscript.quit
end if
end function 
'-----------------------------------------
'在原有名称前增加前缀
function function_prefix_name()
for each file in fs
file.name=twolinestr(1)&file.name
next
end function
'--------------------------------------
'在原有名称前增加后缀
function function_suffix_name()
for each file in fs
name=mid(file.name,1,instrrev(file.name,".")-1) '取到.号前面的文件名
format=mid(file.name,instrrev(file.name,".")) '取到.号后面的后缀格式
file.name=name&threelinestr(1)&format
next
end function
'--------------------------------------------
'在原有名称前增加有序自增数字
function function_number_value()
for each file in fs
fourlinestr(1)=fourlinestr(1)+1
file.name=fourlinestr(1)&file.name
next
end function 
'function_suffix_name()
'--------------------------------------------------
'批量更改文件名称
function function_myarrayrename()
const beforalarm="发生犯人暴狱,请注意观察"
const afteralarm="发生犯人暴狱,各小组按预案处置"
dim myarray(12)
n=1
y=0
for i=0 to 12
if i=11 then 
myarray(i)="监门哨"
elseif i=12 then 
myarray(i)="自卫哨"
else 
myarray(i)=n&"号哨"
n=n+1
end if 
' wscript.echo myarray(i)
next 
for each file in fs
format=mid(file.name,instrrev(file.name,".")) 
'msgbox format
'msgbox myarray(y)
if fivelinestr(1)="before" then 
file.name=myarray(y)&beforalarm&format
elseif fivelinestr(1)="after" then 
file.name=myarray(y)&afteralarm&format
else 
msgbox "请先设置是确认前还是确认后!",,"提示"
wscript.quit
end if 
y=y+1
'wscript.echo file.name
next
end function
 
'=======================================================================
' if prefix_name <> "" then'批量加前缀
' for each f in fs
' f.name = prefix_name&f.name
' next
' end if
' 
' if suffix_name <> "" then'批量加后缀
' for each f in fs
' name = mid(f.name,1,instrrev(f.name,".")-1)
' format = mid(f.name,instrrev(f.name,"."))
' f.name = name & suffix_name & format
' next
' end if
' 
' if repeat_name <> "" then'批量删除相同字符
' for each f in fs
' on error resume next 
' f.name = replace(f.name,repeat_name,repeat_edit)
' next
' end if
' '-----文件操作结束
' 
' set fso = nothing'释放内存
' 
' msgbox("完成!")

需用用到配置文件

config.ini文件内容:
statue=
prefix_name=[320kbp]
suffix_name=[结束]
i=20140100
array=

参数配置使用方法:

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

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com