我想用vbs查找指定文件夹下的所有文本文档,并替换里面的内容,但是...
代码如下:path1="E:\1\*.txt"ReplaceStrPath1,"-",""FunctionReplaceStr(FilePath,FindStr,wRep...
代码如下:
path1 = "E:\1\*.txt"
ReplaceStr Path1,"-",""
Function ReplaceStr(FilePath,FindStr,wReplaceStr)
set fso = CreateObject("scripting.filesystemobject")
set red = fso.opentextfile(FilePath,1)
str = replace(red.readall,FindStr,wReplaceStr)
red.close
set wit = fso.opentextfile(FilePath,2)
wit.writeline str
wit.close
end function
双击后提示出错,提示错误的文件名!
请各位大神指点指点!! 展开
path1 = "E:\1\*.txt"
ReplaceStr Path1,"-",""
Function ReplaceStr(FilePath,FindStr,wReplaceStr)
set fso = CreateObject("scripting.filesystemobject")
set red = fso.opentextfile(FilePath,1)
str = replace(red.readall,FindStr,wReplaceStr)
red.close
set wit = fso.opentextfile(FilePath,2)
wit.writeline str
wit.close
end function
双击后提示出错,提示错误的文件名!
请各位大神指点指点!! 展开
展开全部
给你写个简单的
public function ReplaceStr(FilePath,ExtensioNname,Find,WreplaceStr)
on error resume next '绕过读写错误
dim fso,read,list
set fso = createobject("scripting.filesystemobject")
for each list in fso.getfolder(filepath).files '遍历文件夹
if fso.getextensionname(list)=ExtensioNname then '判断文件是否是txt,若是则执行
read=fso.opentextfile(list).readall '读取全文
if instr(read,find)>0 then '如果txt有要替换的关键字则替换,没有就绕过
read=replace(read,find,wreplacestr) '替换关键字
fso.createtextfile(list.path).write read '写入文本
end if
end if
next
set fso=nothing '释放内存,这里注意,我没有重新设定新对象,尽量不要用以免你忘记加上close导致代码在读写时发生错误,若一定要用的时候才用,我一般是这样的
end function
replacestr "e:\1","txt",123,empty '将e:\1文件夹下的所有txt类型文件中的123初始化(即删除)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询