vbs 批量修改文件,bat 批處理文件調(diào)用執(zhí)行vbs,并在cmd窗口打印返回值(vbs運(yùn)行結(jié)果)
更新時(shí)間:2011年09月16日 19:36:55 作者:
使用vbs 腳本對(duì)工作目錄下的字符串進(jìn)行替換并統(tǒng)計(jì)被修改的文件數(shù)主調(diào)的批處理方法將捕捉該vbs腳本運(yùn)行后的結(jié)果并打印在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%
上面的操作只執(zhí)行了處理文件的步驟
如果不需要提取運(yùn)行結(jié)果,在批處理或主調(diào)vbs腳本里直接 運(yùn)行該vbs文件就行了。
如果在bat 里調(diào)用該vbs腳本,并希望打印出提示信息
可以用下面的兩種方法來(lái)實(shí)現(xiàn):
1,在bat中加入如下代碼
@echo off
for /f "delims=" %%a in ('cscript //nologo c:\t.vbs') do (
set ScriptOut=%%a)
echo Result: %ScriptOut%
上面的 echo 打印字符串的格式自己根據(jù)需要定義,c:\t.vbs 文件目錄修改為你的vbs文件目錄。
2,bat 中加入
@echo off
cscript //nologo c:\t.vbs
echo -----Modify %ErrorLevel% "AssemblyInfo.cs" files successfully.-----
上面echo 文本格式根據(jù)自己的需要定義 %ErrorLevel% 用來(lái)獲取 上面示例vbs文件結(jié)尾 count的值,同樣需要修改vbs文件的目錄 c:\t.vbs
復(fù)制代碼 代碼如下:
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%
上面的操作只執(zhí)行了處理文件的步驟
如果不需要提取運(yùn)行結(jié)果,在批處理或主調(diào)vbs腳本里直接 運(yùn)行該vbs文件就行了。
如果在bat 里調(diào)用該vbs腳本,并希望打印出提示信息
可以用下面的兩種方法來(lái)實(shí)現(xiàn):
1,在bat中加入如下代碼
復(fù)制代碼 代碼如下:
@echo off
for /f "delims=" %%a in ('cscript //nologo c:\t.vbs') do (
set ScriptOut=%%a)
echo Result: %ScriptOut%
上面的 echo 打印字符串的格式自己根據(jù)需要定義,c:\t.vbs 文件目錄修改為你的vbs文件目錄。
2,bat 中加入
復(fù)制代碼 代碼如下:
@echo off
cscript //nologo c:\t.vbs
echo -----Modify %ErrorLevel% "AssemblyInfo.cs" files successfully.-----
上面echo 文本格式根據(jù)自己的需要定義 %ErrorLevel% 用來(lái)獲取 上面示例vbs文件結(jié)尾 count的值,同樣需要修改vbs文件的目錄 c:\t.vbs
相關(guān)文章
VBS教程:函數(shù)-TypeName 函數(shù)
VBS教程:函數(shù)-TypeName 函數(shù)...2006-11-11ADOX.Catalog中文幫助詳細(xì)說(shuō)明chm文檔
這個(gè)是用來(lái)操作ACCESS數(shù)據(jù)庫(kù)的東西,遍歷表,遍歷表的所有字段及字段的屬性都會(huì)用到這個(gè)2008-06-06vbs打開(kāi)ie兩種方法 在VBScript中啟動(dòng)IE瀏覽器的實(shí)現(xiàn)代碼
用VBS打開(kāi)網(wǎng)頁(yè),減少I(mǎi)E廣告, 不錯(cuò)的東東,是在網(wǎng)上收集的,你也可以實(shí)現(xiàn)網(wǎng)頁(yè)的自動(dòng)登錄等2013-01-01用vbs來(lái)探測(cè)端口的代碼 不用注冊(cè)winsock
寫(xiě)這東東主要是在木馬的cmdshell中用,探測(cè)目標(biāo)ip的常用端口是否開(kāi)放。像用nc在shell中返回信息不方便,其它掃描軟件什么的tcp syn之類(lèi)的掃描怕動(dòng)靜太大2011-05-05VBS和頁(yè)面中響應(yīng)COM的事件的方法和代碼
VBS和頁(yè)面中響應(yīng)COM的事件的方法和代碼...2007-03-03vbscript include的辦法實(shí)現(xiàn)代碼
vbscript本身沒(méi)有include這樣包含其他vbs文件的支持,在asp或html頁(yè)面里可以用<script ...這樣來(lái)包含一個(gè)vbs文件,如果是直接運(yùn)行的vbs腳本,就只有用其他辦法來(lái)代替這個(gè)功能了。2009-02-02