VBS批量重命名文件并且操作前备份原有文件
程序员文章站
2022-06-10 07:58:13
核心函数
'==========================================================================
'
'...
核心函数
'========================================================================== ' ' 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函数
上一篇: 淘宝怎么购买冰墩墩?淘宝购买冰墩墩教程
下一篇: VBS怎么获取指定目录下的文件列表