加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VBS修改替换文件内容

(2012-04-14 15:49:30)
分类: 操作系统

'VBS替换普通文本内容

@echo off
echo set fso=createobject("scripting.filesystemobject")>Zkx.vbs
echo set file=fso.opentextfile("application.properties") >>Zkx.vbs
echo s=file.readall >>Zkx.vbs
echo file.close >>Zkx.vbs
echo s=replace(s,"DJUSER","TEST") >>Zkx.vbs
echo set file=fso.createtextfile("1.txt")>>Zkx.vbs
echo file.write s >>Zkx.vbs
echo file.close >>Zkx.vbs
start Zkx.vbs
ping 127.1 -n 2 >nul
del Zkx.vbs

'VBS替换UTF-8文本内容

function ansi2utf8(strFile,s1,s2,newname)
Set ADOStrm = CreateObject("ADODB.Stream")
ADOStrm.Type = 2
ADOStrm.Mode = 3
ADOStrm.CharSet = s1
ADOStrm.Open
ADOStrm.LoadFromFile strFile
s = ADOStrm.ReadText
ADOStrm.Position = 0
ADOStrm.CharSet = s2
ADOStrm.WriteText s
ADOStrm.SetEOS
ADOStrm.SaveToFile newname, 2
ADOStrm.Close
end function

function strReplace(pFile,pStr,reStr)
set fso=CreateObject("Scripting.FileSystemObject")
set ws=CreateObject("wscript.shell")
filename=pFile
ansi2utf8 filename,"utf-8","gbk","temp.tmp"
set fr=fso.opentextfile("temp.tmp",1,-1)
set fw=fso.createtextfile("temp2.tmp",2)
Set regEx = New RegExp
do
data=fr.readline
regEx.Pattern = pStr
data = regEx.Replace(data, reStr)
fw.writeline data
loop until fr.atendofstream
fw.close:fr.close
ansi2utf8 "temp2.tmp","gbk","utf-8",filename
fso.deletefile "temp.tmp",true
fso.deletefile "temp2.tmp",true
end function

strReplace "strPath","strOld","strNew"

 

'VBS读写UTF-8函数

Function ReadFile(FileUrl, CharSet)
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
ReadFile = Str
End Function


Function WriteToFile (FileUrl, Str, CharSet)
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
stm.Close
Set stm = Nothing
End Function

'VBS查找替换当前目录下文件中内容

On Error Resume next
Do Until False
        Findstr=InputBox("请输入你要查找的字符(串):", "请输入")
        If Findstr <> "" Then
                Exit do
        End If
Loop

repwith=InputBox("请输入你要替换的字符(串):,如果留空则只为查找", "请输入")
    

If Wscript.Arguments.Count <> 0 Then
        For i=0 To WScript.Arguments.Count-1
                FolderPath=WScript.Arguments(i)
                Find(FolderPath)
        Next
Else
        '处理当前目录
        Set objShell = CreateObject("WScript.Shell")
        FolderPath=objShell.CurrentDirectory
        Find(FolderPath)
End If

'替换主程序
Sub Find(path)
        set FSO=CreateObject("Scripting.FileSystemObject")
        set current=FSO.GetFolder(path)
        For Each File In current.Files
            set FSOFile=FSO.OpenTextFile(File, 1, true)
         On Error Resume next
            TempStr=FSOFile.Readall

         If  InstrRev(TempStr,Findstr, -1, 0)<>0 And repwith = "" Then
          with FSO.opentextFile(left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))&"\Result.txt",8,true)
             .writeline File
          .close
          end with
         End If
     
        If repwith <> "" Then
        TempStr=replace(TempStr, Findstr, repwith)
        set FSOFile1=FSO.OpenTextFile(File, 2, true)
            FSOFile1.WriteLine TempStr
        FSOFile.close
        End if         
        Next
    for each Folder in current.subFolders
     Call Find(Folder.path)
    next

set FSO=nothing
End Sub

msgbox "OK,查找的文件名保存在Result.txt"

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有