vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果)
2011-09-16 19:30
741 查看
使用vbs 脚本对工作目录下的字符串进行替换并统计被修改的文件数
主调的批处理方法将捕捉该vbs脚本运行后的结果并打印在cmd窗口。
示例代码(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
主调的批处理方法将捕捉该vbs脚本运行后的结果并打印在cmd窗口。
示例代码(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
相关文章推荐
- vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果)
- 【VBscript Shell】通过vbs文件调用bat文件达到隐藏cmd窗口后台运行的效果
- 执行BAT 批处理文件 CMD窗口一闪而过
- java调用cmd并执行批处理(bat)文件
- Java执行bat批处理文件,并关闭cmd窗口
- java 执行bat批处理文件 并关闭cmd窗口
- 在java调用exe,com可执行文件和bat,cmd批处理文件
- java 执行bat批处理文件 并关闭cmd窗口
- BAT批处理文件同时打多个CMD窗口执行其他BAT命令
- C#调用批处理(bat)文件,并获取执行批处理(bat)文件后返回值
- java 执行bat批处理文件 并关闭cmd窗口
- java调用sqlplus执行批量脚本获得输出结果和调用.bat文件
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值
- 不让bat文件运行命令结束后cmd窗口自动关闭
- 使用批处理文件同时打多个CMD窗口且执行多条命令
- 使用批处理文件(*.bat)同时打多个cmd窗口
- Android adb logcat cmd 中文乱码 ----- bat批处理文件不能执行 -- 问题的解决办法
- Dos批处理 Start命令的参数 - bat运行完批处理文件自动关闭窗口
- c#执行bat批处理文件,并通过线程将结果显示在控件中
- 使用批处理文件.bat执行可运行的Jar文件