VBS批量重命名文件并且操作前备份原有文件
核心函数
'========================================================================== ' 'VBScriptSourceFile--CreatedwithSAPIENTechnologiesPrimalScript4.0 ' 'NAME: ' 'AUTHOR:Microsoft,Microsoft 'DATE:2014/7/9 ' 'COMMENT:'批量修改文件夹下对应的所有文件名 ' '========================================================================== '选择我的电脑作为根目录,来选择目录 ConstMY_COMPUTER=&H11& ConstWINDOW_HANDLE=0 ConstOPTIONS=0 SetobjShell=CreateObject("Shell.Application") SetobjFolder=objShell.Namespace(MY_COMPUTER) SetobjFolderItem=objFolder.Self strPath=objFolderItem.Path SetobjShell=CreateObject("Shell.Application") SetobjFolder=objShell.BrowseForFolder_ (WINDOW_HANDLE,"Selectafolder:",OPTIONS,strPath) IfobjFolderIsNothingThen Wscript.Quit EndIf SetobjFolderItem=objFolder.Self objPath=objFolderItem.Path 'MsgBoxobjFolderItem.name '=================================================================== '选择指定盘符下的目录 'ConstWINDOW_HANDLE=0 'ConstOPTIONS=0 ' 'SetobjShell=CreateObject("Shell.Application") 'SetobjFolder=objShell.BrowseForFolder_ '(WINDOW_HANDLE,"Selectafolder:",OPTIONS,"C:\") ' 'IfobjFolderIsNothingThen 'Wscript.Quit 'EndIf ' 'SetobjFolderItem=objFolder.Self 'objPath=objFolderItem.Path ' 'MsgBoxobjPath '========================================================================= '定义变量 dimfile_path,prefix_name,suffix_name,repeat_name,repeat_edit DimOneLine,TwoLine,ThreeLine,FourLine,FiveLine i=0 test=createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path 'Wscript.echotest filepath=test&"\config.ini" 'WScript.Echofilepath 'file_path="C:\Users\Administrator\Desktop\1\music"'目标文件夹的路径 dst_file_path="C:\"&objFolderItem.name&"_bak" file_path=objPath '-----得到文件夹路径,且打开配置文件 Setfso=CreateObject("Scripting.FileSystemObject") Setfolder=fso.getfolder(file_path) Setfs=folder.files Setfile=fso.OpenTextFile(filepath,1) '----------------在操作前,备份一下原有的文件 fso.CopyFolderfile_path,dst_file_path,True '---------------------------------- '取出第一行中的两个数 OneLine=file.ReadLine OneLineStr=Split(OneLine,"=") OneLineCount=UBound(split(OneLine,"=")) Fori1=0ToOneLineCount 'WScript.EchoOneLineStr(i1) Next '------------------------------------- '取出第二行中的两个数 TwoLine=file.ReadLine TwoLineStr=Split(TwoLine,"=") TwoLineCount=UBound(split(TwoLine,"=")) Fori2=0ToTwoLineCount 'WScript.EchoTwoLineStr(i2) Next '------------------------------------------- '取出第三行中的两个数 ThreeLine=file.ReadLine ThreeLineStr=Split(ThreeLine,"=") ThreeLineCount=UBound(split(ThreeLine,"=")) Fori3=0ToThreeLineCount 'WScript.EchoThreeLineStr(i3) Next '------------------------------------------- '取出第四行中的两个数 FourLine=file.ReadLine FourLineStr=Split(FourLine,"=") FourLineCount=UBound(split(FourLine,"=")) Fori4=0ToFourLineCount 'WScript.EchoFourLineStr(i4) Next '----------------------------------------- '取出第五行中的两个数 FiveLine=file.ReadLine FiveLineStr=Split(FiveLine,"=") FiveLineCount=Ubound(split(FiveLine,"=")) Fori5=0ToFiveLineCount 'WScript.EchoFiveLineStr(i5) Next '--------------------------------------------- '调用过程 'Function_Main() FunctionFunction_Main() IfOneLineStr(1)="true"Then Function_Prefix_Name() ElseifOneLineStr(1)="false"Then Function_Suffix_Name() ElseifOneLineStr(1)="number"Then Function_Number_Value() ElseifOneLineStr(1)="array"Then Function_MyArrayReName() ElseifOneLineStr(1)=""Then WScript.Quit EndIf EndFunction '----------------------------------------- '在原有名称前增加前缀 FunctionFunction_Prefix_Name() ForEachfileinfs File.Name=TwoLineStr(1)&File.Name Next EndFunction '-------------------------------------- '在原有名称前增加后缀 FunctionFunction_Suffix_Name() ForEachfileinfs Name=Mid(file.name,1,instrrev(file.name,".")-1)'取到.号前面的文件名 Format=Mid(file.name,instrrev(file.name,"."))'取到.号后面的后缀格式 file.Name=Name&ThreeLineStr(1)&Format Next EndFunction '-------------------------------------------- '在原有名称前增加有序自增数字 FunctionFunction_Number_Value() ForEachfileInfs FourLineStr(1)=FourLineStr(1)+1 file.name=FourLineStr(1)&file.name Next EndFunction 'Function_Suffix_Name() '-------------------------------------------------- '批量更改文件名称 FunctionFunction_MyArrayReName() ConstBeforAlarm="发生犯人暴狱,请注意观察" ConstAfterAlarm="发生犯人暴狱,各小组按预案处置" DimMyArray(12) n=1 y=0 Fori=0To12 Ifi=11Then MyArray(i)="监门哨" Elseifi=12Then MyArray(i)="自卫哨" Else MyArray(i)=n&"号哨" n=n+1 EndIf 'WScript.EchoMyArray(i) Next ForEachfileInfs Format=Mid(file.name,instrrev(file.name,".")) 'MsgBoxFormat 'MsgBoxMyArray(y) IfFiveLineStr(1)="before"Then file.name=MyArray(y)&BeforAlarm&Format ElseifFiveLineStr(1)="after"Then file.name=MyArray(y)&AfterAlarm&Format Else MsgBox"请先设置是确认前还是确认后!",,"提示" WScript.Quit EndIf y=y+1 'WScript.Echofile.name Next EndFunction '======================================================================= 'Ifprefix_name<>""then'批量加前缀 'Foreachfinfs 'f.name=prefix_name&f.name 'Next 'EndIf ' 'ifsuffix_name<>""then'批量加后缀 'Foreachfinfs 'name=Mid(f.name,1,InstrRev(f.name,".")-1) 'format=Mid(f.name,InstrRev(f.name,".")) 'f.name=name&suffix_name&format 'Next 'endIf ' 'ifrepeat_name<>""then'批量删除相同字符 'Foreachfinfs 'OnErrorResumeNext 'f.name=Replace(f.name,repeat_name,repeat_edit) 'Next 'endIf ''-----文件操作结束 ' 'setfso=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函数