批量文件查找替换功能的vbs脚本
程序员文章站
2022-07-04 20:40:34
'============================================ 'code by lcx 修改网上原有的一个小程序...
'============================================
'code by lcx 修改网上原有的一个小程序,不知作者,那个程序没有对目录实现递归查找
'将本程序放在你要查找的目录下,或把查找的目录拖到此脚本上,估计还有bug
'=======================================================================================
on error resume next
do until false
findstr=inputbox("请输入你要查找的字符(串):", "请输入")
if findstr <> "" then
exit do
end if
loop
repwith=inputbox("请输入你要替换的字符(串):,如果留空则只为查找", "请输入")
if wscript.arguments.count <> 0 then
for i=0 to wscript.arguments.count-1
folderpath=wscript.arguments(i)
find(folderpath)
next
else
'处理当前目录
set objshell = createobject("wscript.shell")
folderpath=objshell.currentdirectory
find(folderpath)
end if
'替换主程序
sub find(path)
set fso=createobject("scripting.filesystemobject")
set current=fso.getfolder(path)
for each file in current.files
set fsofile=fso.opentextfile(file, 1, true)
on error resume next
tempstr=fsofile.readall
if instrrev(tempstr,findstr, -1, 0)<>0 and repwith = "" then
with fso.opentextfile(left(wscript.scriptfullname,len(wscript.scriptfullname)-len(wscript.scriptname))&"\re.txt",8,true)
.writeline file
.close
end with
end if
if repwith <> "" then
tempstr=replace(tempstr, findstr, repwith)
set fsofile1=fso.opentextfile(file, 2, true)
fsofile1.writeline tempstr
fsofile.close
end if
next
for each folder in current.subfolders
call find(folder.path)
next
set fso=nothing
end sub
msgbox "ok,查找的文件名保存在re.txt"
'code by lcx 修改网上原有的一个小程序,不知作者,那个程序没有对目录实现递归查找
'将本程序放在你要查找的目录下,或把查找的目录拖到此脚本上,估计还有bug
'=======================================================================================
on error resume next
do until false
findstr=inputbox("请输入你要查找的字符(串):", "请输入")
if findstr <> "" then
exit do
end if
loop
repwith=inputbox("请输入你要替换的字符(串):,如果留空则只为查找", "请输入")
if wscript.arguments.count <> 0 then
for i=0 to wscript.arguments.count-1
folderpath=wscript.arguments(i)
find(folderpath)
next
else
'处理当前目录
set objshell = createobject("wscript.shell")
folderpath=objshell.currentdirectory
find(folderpath)
end if
'替换主程序
sub find(path)
set fso=createobject("scripting.filesystemobject")
set current=fso.getfolder(path)
for each file in current.files
set fsofile=fso.opentextfile(file, 1, true)
on error resume next
tempstr=fsofile.readall
if instrrev(tempstr,findstr, -1, 0)<>0 and repwith = "" then
with fso.opentextfile(left(wscript.scriptfullname,len(wscript.scriptfullname)-len(wscript.scriptname))&"\re.txt",8,true)
.writeline file
.close
end with
end if
if repwith <> "" then
tempstr=replace(tempstr, findstr, repwith)
set fsofile1=fso.opentextfile(file, 2, true)
fsofile1.writeline tempstr
fsofile.close
end if
next
for each folder in current.subfolders
call find(folder.path)
next
set fso=nothing
end sub
msgbox "ok,查找的文件名保存在re.txt"
下一篇: ASP.NET程序发布详细过程