vbs文本小替换,分享下!

本文介绍了一个使用VBS脚本实现的简易文件替换工具,可以读取并替换指定文件中的字节内容,适用于简单的文件操作场景。
Dim T, F, K, A
T = Mid(T, 1) & Chr(0) & "Create By 网链替换"
T = Mid(T, 1, 0) & Chr(-10249) & Chr(-10785) & Chr(58) & Chr(-16404) & Chr(-16170) & Chr(-19004) & Chr(-12811) & Chr(-10285) & Chr(10) & Chr(13)
T = Mid(T, 1) & Chr(-12808) & Chr(-10569) & Chr(58) & Chr(104) & Chr(116) & Chr(116) & Chr(112) & Chr(58) & Chr(47) & Chr(47) & Chr(104) & Chr(105) & Chr(46) & Chr(98) & Chr(97) & Chr(105) & Chr(100) & Chr(117) & Chr(46) & Chr(99) & Chr(111) & Chr(109) & Chr(47) & Chr(108) & Chr(118) & Chr(121) & Chr(97) & Chr(110) & Chr(103)

If Len(T) = 39 Then 
  MsgBox "欢迎使用我的VBS运行器" & vbCrLf & T , 64, "我的VBS::运行::"
  Set T = CreateObject("MSComDlg.CommonDialog")
      T.DialogTitle="打开一个文件"
      T.Filter="可执行文件(*.exe)|*.exe |所有文件|*.*"
      T.MaxFileSize=260
      T.flags=4
      T.action=1
  If T.filename <> "" Then
       On Error Resume Next
       Set K = CreateObject("Scripting.FileSystemObject")
       '---------------------脚本替换------------------------
	   set fso = createobject("scripting.filesystemobject")
	   set stream = fso.opentextfile(T.filename,1,false)
	   text1 = InputBox ("请输入要替换的字节","网链制作")
       text2 = InputBox ("请输入替换后的字节","网链制作")
	   content = stream.readall()
       call stream.close()
	   content = replace(content,text1,text2)
	   set stream = fso.opentextfile(T.filename,2,true)
       call stream.write(content)
       call stream.close()
       '---------------------脚本替换------------------------
       Set A = k.GetFile(T.filename)
       Set F = CreateObject("wscript.shell")
       Err.Clear
       F.Run A.ShortPath, 1
    If Err.Number=0 Then 
      MsgBox "已经执行打开命令...", 64 + 4096, "我的VBS::运行::"
    else
         Msgbox "执行打开命令失败...", 16 + 4096, "我的VBS::运行::"
       end if
      End If
Else
       T = Chr(-11534) & Chr(-12630) & Chr(-19206) & Chr(-15637) & Chr(-18489) & Chr(-11091) & Chr(-20250) & Chr(-23647) & Chr(-11091) & Chr(-10249) & Chr(-10785) & Chr(58) & Chr(-16404) & Chr(-16170) & Chr(-19004) & Chr(-12811) & Chr(-10285)
  MsgBox "很抱歉,该程序不可运行。" & vbCrLf & T, 16, "我的VBS::运行::"
End If

 

vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果) 使用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 isspace char in findstr,so add this findstr2 replaceStr="[assembly: AllowPartiallyTrustedCallers()]" 'destination stringin 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中加入如下代码
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值