当前位置: 移动技术网 > IT编程>脚本编程>VBScript > 利用vbscript脚本修改文件内容,此适用于自动化的操作中

利用vbscript脚本修改文件内容,此适用于自动化的操作中

2017年12月12日  | 移动技术网IT编程  | 我要评论

阿尔玛蓝,免费女装代理,射鸡英雄传

利用vbscript脚本修改文件内容,此适用于自动化的操作中

'新建一个replace.vbs脚本,脚本内容如下,程序运行时输入三个参数:查找内容,替换内容,文件 

复制代码 代码如下:
dim filename, find, replacewith, filecontents, dfilecontents  
find = wscript.arguments(0)  
replacewith = wscript.arguments(1)  
filename = wscript.arguments(2)  

'读取文件  
filecontents = getfile(filename)  

'用“替换内容”替换文件中所有“查找内容”  
dfilecontents = replace(filecontents, find, replacewith, 1, -1, 1)  

'比较源文件和替换后的文件  
if dfilecontents <> filecontents then  
'保存替换后的文件  
writefile filename, dfilecontents  

wscript.echo "replace done."  
if len(replacewith) <> len(find) then  
'计算替换总数  
wscript.echo _  
( (len(dfilecontents) - len(filecontents)) / (len(replacewith)-len(find)) ) & _  
" replacements."  
end if  
else  
wscript.echo "searched string not in the source file"  
end if  

'读取文件  
function getfile(filename)  
if filename<>"" then  
dim fs, filestream  
set fs = createobject("scripting.filesystemobject")  
on error resume next  
set filestream = fs.opentextfile(filename)  
getfile = filestream.readall  
end if  
end function  

'写文件  
function writefile(filename, contents)  
dim outstream, fs  

on error resume next  
set fs = createobject("scripting.filesystemobject")  
set outstream = fs.opentextfile(filename, 2, true)  
outstream.write contents  
end function

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网