vbs怎么实现批量修改文件

发布时间:2021-09-13 22:55:55 作者:chen
来源:亿速云 阅读:135

这篇文章主要介绍“vbs怎么实现批量修改文件”,在日常操作中,相信很多人在vbs怎么实现批量修改文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vbs怎么实现批量修改文件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

示例代码(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怎么实现批量修改文件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 批量修改文件名后缀
  2. java如何批量修改文件后缀

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vbs

上一篇:什么是Socket编程

下一篇:通过sql存储过程发送邮件的方法步骤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》