vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果)
程序员文章站
2022-06-23 21:02:31
示例代码(t.vbs)如下: 复制代码 代码如下: set fso=wscript.createobject("scripting.filesystemobject") f...
示例代码(t.vbs)如下:
set fso=wscript.createobject("scripting.filesystemobject")
flrname="d:\workspace\src\" 'dir that you need deal with
count=0 'get the count of modified files
function traversal(dir)
set flr=fso.getfolder(dir)
set fs=flr.files
findstr1="[assembly: securitytransparent]" 'find string that need to be replaced
findstr2=replace(findstr1," ","") 'becasuse there is space char in findstr,so add this findstr2
replacestr="[assembly: allowpartiallytrustedcallers()]" 'destination string in replace
for each f in fs
if lcase(f.name)="assemblyinfo.cs" then
set findf=fso.opentextfile(f)
do while findf.atendofstream=false
d=f.datelastmodified 'get modified datetime of current file
alltext=findf.readall
if(instr(alltext,findstr1) > 0 ) then
s=replace(alltext,findstr1, replacestr)
count=count+1
findf.close()
'if no findstr2, delete the following elseif block
elseif(instr(alltext,findstr2) > 0) then
s=replace(alltext,findstr2, replacestr)
count=count+1
findf.close()
else
findf.close()
exit do
end if
set r=fso.opentextfile(f, 2, true)
r.write s
d2=f.datelastmodified
if d2>=d then exit do
loop
end if
next
set fs=flr.subfolders
for each f in fs
traversal(f.path)
next
end function
traversal(flrname)
'msgbox ("done! "& count & " files were modified successfully.")
wscript.echo "done! "& count & " files were modified successfully." 'value popup by "wscript.echo" can be received by .bat easily
wscript.quit count 'this variable "count" in order to get a return value by %errorlevel%
上面的操作只执行了处理文件的步骤
如果不需要提取运行结果,在批处理或主调vbs脚本里直接 运行该vbs文件就行了。
如果在bat 里调用该vbs脚本,并希望打印出提示信息
可以用下面的两种方法来实现:
1,在bat中加入如下代码
@echo off
for /f "delims=" %%a in ('cscript //nologo c:\t.vbs') do (
set scriptout=%%a)
echo result: %scriptout%
上面的 echo 打印字符串的格式自己根据需要定义,c:\t.vbs 文件目录修改为你的vbs文件目录。
2,bat 中加入
@echo off
cscript //nologo c:\t.vbs
echo -----modify %errorlevel% "assemblyinfo.cs" files successfully.-----
上面echo 文本格式根据自己的需要定义 %errorlevel% 用来获取 上面示例vbs文件结尾 count的值,同样需要修改vbs文件的目录 c:\t.vbs
复制代码 代码如下:
set fso=wscript.createobject("scripting.filesystemobject")
flrname="d:\workspace\src\" 'dir that you need deal with
count=0 'get the count of modified files
function traversal(dir)
set flr=fso.getfolder(dir)
set fs=flr.files
findstr1="[assembly: securitytransparent]" 'find string that need to be replaced
findstr2=replace(findstr1," ","") 'becasuse there is space char in findstr,so add this findstr2
replacestr="[assembly: allowpartiallytrustedcallers()]" 'destination string in replace
for each f in fs
if lcase(f.name)="assemblyinfo.cs" then
set findf=fso.opentextfile(f)
do while findf.atendofstream=false
d=f.datelastmodified 'get modified datetime of current file
alltext=findf.readall
if(instr(alltext,findstr1) > 0 ) then
s=replace(alltext,findstr1, replacestr)
count=count+1
findf.close()
'if no findstr2, delete the following elseif block
elseif(instr(alltext,findstr2) > 0) then
s=replace(alltext,findstr2, replacestr)
count=count+1
findf.close()
else
findf.close()
exit do
end if
set r=fso.opentextfile(f, 2, true)
r.write s
d2=f.datelastmodified
if d2>=d then exit do
loop
end if
next
set fs=flr.subfolders
for each f in fs
traversal(f.path)
next
end function
traversal(flrname)
'msgbox ("done! "& count & " files were modified successfully.")
wscript.echo "done! "& count & " files were modified successfully." 'value popup by "wscript.echo" can be received by .bat easily
wscript.quit count 'this variable "count" in order to get a return value by %errorlevel%
上面的操作只执行了处理文件的步骤
如果不需要提取运行结果,在批处理或主调vbs脚本里直接 运行该vbs文件就行了。
如果在bat 里调用该vbs脚本,并希望打印出提示信息
可以用下面的两种方法来实现:
1,在bat中加入如下代码
复制代码 代码如下:
@echo off
for /f "delims=" %%a in ('cscript //nologo c:\t.vbs') do (
set scriptout=%%a)
echo result: %scriptout%
上面的 echo 打印字符串的格式自己根据需要定义,c:\t.vbs 文件目录修改为你的vbs文件目录。
2,bat 中加入
复制代码 代码如下:
@echo off
cscript //nologo c:\t.vbs
echo -----modify %errorlevel% "assemblyinfo.cs" files successfully.-----
上面echo 文本格式根据自己的需要定义 %errorlevel% 用来获取 上面示例vbs文件结尾 count的值,同样需要修改vbs文件的目录 c:\t.vbs
上一篇: 合肥之战的真相到底是什么样的 所谓八百破十万之战绩是真的吗
下一篇: 微信公众平台支付开发详解