最近设备密码更换,配置文件很多,所以写了个脚本


'2012年12月28日21:31:39 配置文件读写
OnErrorResumeNext
Num = NRAND(7) 
'Randomize
'Num = Int((100000 * Rnd) + 1)
' ############################################################
'创建测试文件
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
Set testFile = fso.CreateTextFile(".\test.ini",false)
testFile.writeline "[LanDevice]"
testFile.writeline "USERNAME = admin"
testFile.writeline ""
testFile.writeline "PASSWORD = 7568243"
testFile.writeline "[LanDevice2]"
testFile.writeline "USERNAME = admin"
testFile.write "PASSWORD = 1324124"
testFile.close
' ############################################################
'调用函数
'读取
ReadINIFile=".\test.ini,LanDevice,PASSWORD"
ReadINI= Read_WriteINI (ReadINIFile) 
msgbox "ReadINI:"&ReadINI 
'------------------------------------------------------------
'写入
WriteINIFile=".\test.ini,LanDevice2,PASSWORD,"&Num 
WriteINI= Read_WriteINI (WriteINIFile) 
CreateObject("WScript.Shell").run ".\test.ini"
'CreateObject("WScript.Shell").run ".\temp.ini"
'msgbox "WriteINI"
' ############################################################
Function Read_WriteINI(Argus) 
Const ForReading = 1 
Const ForWriting = 2 
Const Forappending = 8 
Set fso = CreateObject("Scripting.FileSystemObject") 
Dim fso, strLine, j, strRead 
arg=split(Argus,",") 
if Ubound(arg)=3 then 
INIFilePath=arg(0) 
INISection=arg(1) 
ININame=arg(2) 
INIVlaue=arg(3) 
'------------------------写入方法一-------------------------
if Ubound(arg)=3 then 
'Const ForReading = 1 Const ForWriting = 2 Const Forappending = 8  !!!
Set IniFile = fso.OpenTextFile(INIFilePath, 1) 
OldAll = IniFile.ReadAll 
IniFile.Close 
NEWALL = Replace(OldAll,strRpl, ININame&" = "&INIVlaue) '如果不同节点下,有相同的行,会同时替换
'NEWALL = Replace(OldAll,strRpl, ININame&"="&INIVlaue)   '=号两侧无空格
Set IniFile = fso.OpenTextFile(INIFilePath, ForWriting) 
IniFile.Write NEWALL 
IniFile.Close 
end if 
''------------------------写入方法二-------------------------
'           '虽然不会替换错误,但是太麻烦了
'Set tempFile = fso.OpenTextFile(".\temp.ini", 2,true)
'Set IniFile = fso.OpenTextFile(INIFilePath, 1)
'Do Until IniFile.atendofstream
'strEachLine = IniFile.ReadLine
''      tempFile.writeLine strEachLine
'   If  Trim(strEachLine) = "[" &INISection & "]" Then 
'       tempFile.writeLine strEachLine
'      i="find2"
'    'msgbox "找到节名"
'   elseif i="find2" and strLine <> "" then
'       m=split(strLine,"=")
'       if  ININame=trim(m(0)) then    '找到Name
'                       i="stopSplit"  
'        tempFile.writeLine ININame&" = "&INIVlaue
'           strRead = trim(m(1))
'           strRpl = strLine
'       else
'           tempFile.writeLine strEachLine          
'       end if 
'   else
'       tempFile.writeLine strEachLine
'   end if 
' loop
' IniFile.Close
'fso.getfile(".\test.ini" ).delete 
'fso.Movefile ".\temp.ini",".\test.ini" 
' tempFile.Close
' fso.getfile(".\temp.ini" ).delete 
'-----------------------------------------------------------
else  
INIFilePath=arg(0) 
INISection=arg(1) 
ININame=arg(2) 
Set IniFile = fso.opentextfile(INIFilePath, 1) 
DoUntil IniFile.atendofstream 
    strLine = IniFile.readline 
If  Trim(strLine) = "[" &INISection & "]"Then
      i="find"
'msgbox "找到小节名"
    elseif i="find" and strLine <> "" then   '查找Section后的Name,空行loop
'-------------------------读取方法一--------------------------
     if  InStr(strLine, "[")>0 and Trim(strLine) <> "[" &INISection & "]"then  
     exit do              '下一节点退出
     else  
        j=split(strLine,"=")         
        if  ININame=trim(j(0)) then   '查找参数
            strRead = trim(j(1))        '读值
'           msgbox strRead
            strRpl = strLine 
            exit do  
        end if  
     end if  
'-------------------------读取方法二------------------------     
'            j = InStr(strLine, "=")             '度娘
'            If j > 0 Then                               '小节名后的文本行存在
'                If InStr(Left(strLine, j), ININame) > 0 Then                       '从"="左边字符串找到键名
'                    strRead = Trim(Right(strLine, Len(strLine) - InStr(strLine, "="))) '读取等号后的部分
'                    'msgbox strRead
'                    strRpl = strLine
'                    Exit Do
'                End If
'            End If
'-------------------------------------------------------
EndIf
Loop
IniFile.Close 
Read_WriteINI = strRead 
end if 
Set fso = Nothing
EndFunction
Function NRAND(n) '生成n位随机数字
For i=1 to n 
    Randomize 
    NO = int(9*Rnd+1) 
    NRAND = NRAND&NO 
Next
EndFunction