[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] INI文件修改的VBS[示例]

  1. dim r(1),file(1),read(1)
  2. set fso=CreateObject("Scripting.FileSystemObject")
  3. Set RegExp = New RegExp
  4. file(0)="1.txt"
  5. file(1)="2.txt"
  6. n=2
  7. for i = 0 to 1
  8.     if not fso.fileexists(file(i)) then
  9.         n=n-1
  10.         err_msg="'"&file(i)&"' "
  11.     end if
  12. next
  13. if n=0 then
  14.     err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  15. end if
  16. if n<>2 then
  17.     msgbox err_msg&"不存在,请检查vbs代码是否修改正确!",16,"Error":wsh.quit
  18. end if
  19. for i = 0 to 1
  20.     if fso.getfile(file(i)).size=0 then
  21.         n=n-1:a=a+1
  22.         err_msg="'"&file(i)&"' "
  23.     end if
  24. next
  25. if n=0 then
  26.     err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  27. end if
  28. if a=1 then a=empty
  29. if n<>2 then
  30.     msgbox err_msg&"文件内容为空,请检查这"&a&"个"&fso.getextensionname(file(0))&"是否是需要修改的文件!",16,"Error":wsh.quit
  31. end if
  32. n=2
  33. for i = 0 to 1
  34.     read(i)=fso.opentextfile(file(i)).readall
  35. next
  36. With RegExp
  37.     .Global = -1
  38.     .multiline=-1
  39.     .pattern="^(.*)=(.*$)"
  40.     if .test(read(0)) then
  41.         for each match in .execute(read(0))
  42.             r(0)=match.submatches(0)
  43.             r(1)=match.submatches(1)
  44.             .pattern="(^"&r(0)&"=)(.*)$"
  45.             read(1)=.Replace(read(1),"$1"&r(1))
  46.         next
  47.         fso.createtextfile(file(1)).write read(1)
  48.     else
  49.         n=n-1
  50.         err_msg="'"&file(0)&"' "
  51.     end if
  52.     if not .test(read(1)) then
  53.         n=n-1
  54.         err_msg="'"&file(1)&"' "
  55.     end if
  56.     if n=0 then
  57.         err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  58.     end if
  59.     if n<>2 then
  60.         msgbox "没有在 '"&err_msg&"' 中找到相应匹配!",16,"Error":wsh.quit
  61.     end if
  62. End With
复制代码
声明:
本程序是将样本中每一行对应的数据中末尾=号后面的数据进行更替.
如果你对此代码表示不满意还可以进行修整,添加你需要的代码或者批量修改都行!使用范围不仅限INI文件.
如果你感到日常频繁使用软件或者游戏等,需要大量查找/修改感到烦恼时可以考虑用来一键替换.
1

评分人数

转一个~
《Read & Write INI Files》
http://www.robvanderwoude.com/vbstech_files_ini.php#SampleScript
  1. 'Read & Write INI Files
  2. 'http://www.robvanderwoude.com/vbstech_files_ini.php#SampleScript
  3. WriteIni "test.ini", "TEST1", "My1stKey", "My1stValue"
  4. WriteIni "test.ini", "TEST2", "My1stKey", "My1stValue"
  5. WScript.Echo ReadIni( "test.ini", "TEST1", "My1stKey" )
  6. WriteIni "test.ini", "TEST1", "My1stKey", "My2ndValue"
  7. WScript.Echo ReadIni( "test.ini", "TEST1", "My1stKey" )
  8. 'DeleteINI
  9. 'To delete a key in an INI file, use WriteINI with a value "<DELETE_THIS_VALUE>".
  10. Function ReadIni( myFilePath, mySection, myKey )
  11.     ' This function returns a value read from an INI file
  12.     '
  13.     ' Arguments:
  14.     ' myFilePath  [string]  the (path and) file name of the INI file
  15.     ' mySection   [string]  the section in the INI file to be searched
  16.     ' myKey       [string]  the key whose value is to be returned
  17.     '
  18.     ' Returns:
  19.     ' the [string] value for the specified key in the specified section
  20.     '
  21.     ' CAVEAT:     Will return a space if key exists but value is blank
  22.     '
  23.     ' Written by Keith Lacelle
  24.     ' Modified by Denis St-Pierre and Rob van der Woude
  25.     Const ForReading   = 1
  26.     Const ForWriting   = 2
  27.     Const ForAppending = 8
  28.     Dim intEqualPos
  29.     Dim objFSO, objIniFile
  30.     Dim strFilePath, strKey, strLeftString, strLine, strSection
  31.     Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  32.     ReadIni     = ""
  33.     strFilePath = Trim( myFilePath )
  34.     strSection  = Trim( mySection )
  35.     strKey      = Trim( myKey )
  36.     If objFSO.FileExists( strFilePath ) Then
  37.         Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
  38.         Do While objIniFile.AtEndOfStream = False
  39.             strLine = Trim( objIniFile.ReadLine )
  40.             ' Check if section is found in the current line
  41.             If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  42.                 strLine = Trim( objIniFile.ReadLine )
  43.                 ' Parse lines until the next section is reached
  44.                 Do While Left( strLine, 1 ) <> "["
  45.                     ' Find position of equal sign in the line
  46.                     intEqualPos = InStr( 1, strLine, "=", 1 )
  47.                     If intEqualPos > 0 Then
  48.                         strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  49.                         ' Check if item is found in the current line
  50.                         If LCase( strLeftString ) = LCase( strKey ) Then
  51.                             ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
  52.                             ' In case the item exists but value is blank
  53.                             If ReadIni = "" Then
  54.                                 ReadIni = " "
  55.                             End If
  56.                             ' Abort loop when item is found
  57.                             Exit Do
  58.                         End If
  59.                     End If
  60.                     ' Abort if the end of the INI file is reached
  61.                     If objIniFile.AtEndOfStream Then Exit Do
  62.                     ' Continue with next line
  63.                     strLine = Trim( objIniFile.ReadLine )
  64.                 Loop
  65.             Exit Do
  66.             End If
  67.         Loop
  68.         objIniFile.Close
  69.     Else
  70.         WScript.Echo strFilePath & " doesn't exists. Exiting..."
  71.         Wscript.Quit 1
  72.     End If
  73. End Function
  74. Sub WriteIni( myFilePath, mySection, myKey, myValue )
  75.     ' This subroutine writes a value to an INI file
  76.     '
  77.     ' Arguments:
  78.     ' myFilePath  [string]  the (path and) file name of the INI file
  79.     ' mySection   [string]  the section in the INI file to be searched
  80.     ' myKey       [string]  the key whose value is to be written
  81.     ' myValue     [string]  the value to be written (myKey will be
  82.     '                       deleted if myValue is <DELETE_THIS_VALUE>)
  83.     '
  84.     ' Returns:
  85.     ' N/A
  86.     '
  87.     ' CAVEAT:     WriteIni function needs ReadIni function to run
  88.     '
  89.     ' Written by Keith Lacelle
  90.     ' Modified by Denis St-Pierre, Johan Pol and Rob van der Woude
  91.     Const ForReading   = 1
  92.     Const ForWriting   = 2
  93.     Const ForAppending = 8
  94.     Dim blnInSection, blnKeyExists, blnSectionExists, blnWritten
  95.     Dim intEqualPos
  96.     Dim objFSO, objNewIni, objOrgIni, wshShell
  97.     Dim strFilePath, strFolderPath, strKey, strLeftString
  98.     Dim strLine, strSection, strTempDir, strTempFile, strValue
  99.     strFilePath = Trim( myFilePath )
  100.     strSection  = Trim( mySection )
  101.     strKey      = Trim( myKey )
  102.     strValue    = Trim( myValue )
  103.     Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  104.     Set wshShell = CreateObject( "WScript.Shell" )
  105.     strTempDir  = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
  106.     strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )
  107.     Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )
  108.     Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )
  109.     blnInSection     = False
  110.     blnSectionExists = False
  111.     ' Check if the specified key already exists
  112.     blnKeyExists     = ( ReadIni( strFilePath, strSection, strKey ) <> "" )
  113.     blnWritten       = False
  114.     ' Check if path to INI file exists, quit if not
  115.     'strFolderPath = Mid( strFilePath, 1, InStrRev( strFilePath, "\" ) )
  116.     'If Not objFSO.FolderExists ( strFolderPath ) Then
  117.     '    WScript.Echo "Error: WriteIni failed, folder path (" _
  118.     '               & strFolderPath & ") to ini file " _
  119.     '               & strFilePath & " not found!"
  120.     '    Set objOrgIni = Nothing
  121.     '    Set objNewIni = Nothing
  122.     '    Set objFSO    = Nothing
  123.     '    WScript.Quit 1
  124.     'End If
  125.     While objOrgIni.AtEndOfStream = False
  126.         strLine = Trim( objOrgIni.ReadLine )
  127.         If blnWritten = False Then
  128.             If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  129.                 blnSectionExists = True
  130.                 blnInSection = True
  131.             ElseIf InStr( strLine, "[" ) = 1 Then
  132.                 blnInSection = False
  133.             End If
  134.         End If
  135.         If blnInSection Then
  136.             If blnKeyExists Then
  137.                 intEqualPos = InStr( 1, strLine, "=", vbTextCompare )
  138.                 If intEqualPos > 0 Then
  139.                     strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  140.                     If LCase( strLeftString ) = LCase( strKey ) Then
  141.                         ' Only write the key if the value isn't empty
  142.                         ' Modification by Johan Pol
  143.                         If strValue <> "<DELETE_THIS_VALUE>" Then
  144.                             objNewIni.WriteLine strKey & "=" & strValue
  145.                         End If
  146.                         blnWritten   = True
  147.                         blnInSection = False
  148.                     End If
  149.                 End If
  150.                 If Not blnWritten Then
  151.                     objNewIni.WriteLine strLine
  152.                 End If
  153.             Else
  154.                 objNewIni.WriteLine strLine
  155.                     ' Only write the key if the value isn't empty
  156.                     ' Modification by Johan Pol
  157.                     If strValue <> "<DELETE_THIS_VALUE>" Then
  158.                         objNewIni.WriteLine strKey & "=" & strValue
  159.                     End If
  160.                 blnWritten   = True
  161.                 blnInSection = False
  162.             End If
  163.         Else
  164.             objNewIni.WriteLine strLine
  165.         End If
  166.     Wend
  167.     If blnSectionExists = False Then ' section doesn't exist
  168.         objNewIni.WriteLine
  169.         objNewIni.WriteLine "[" & strSection & "]"
  170.             ' Only write the key if the value isn't empty
  171.             ' Modification by Johan Pol
  172.             If strValue <> "<DELETE_THIS_VALUE>" Then
  173.                 objNewIni.WriteLine strKey & "=" & strValue
  174.             End If
  175.     End If
  176.     objOrgIni.Close
  177.     objNewIni.Close
  178.     ' Delete old INI file
  179.     objFSO.DeleteFile strFilePath, True
  180.     ' Rename new INI file
  181.     objFSO.MoveFile strTempFile, strFilePath
  182.     Set objOrgIni = Nothing
  183.     Set objNewIni = Nothing
  184.     Set objFSO    = Nothing
  185.     Set wshShell  = Nothing
  186. End Sub
复制代码
再转一个~
《Read and write windows INI files in VBSscriptMOTOBIT.COM》
http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/
  1. 'Read and write windows INI files in VBSscriptMOTOBIT.COM
  2. 'http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/
  3. WriteINIString "Mail", "MAPI", "1", "win.ini" '写入INI
  4. WScript.echo GetINIString("Mail", "MAPI", "-", "win.ini") '读取INI
  5. Sub WriteINIStringVirtual(Section, KeyName, Value, FileName)
  6.   WriteINIString Section, KeyName, Value, FileName
  7. End Sub
  8. Function GetINIStringVirtual(Section, KeyName, Default, FileName)
  9.   GetINIStringVirtual = GetINIString(Section, KeyName, Default, FileName)
  10. End Function
  11. 'Work with INI files In VBS (ASP/WSH)
  12. 'v1.00
  13. '2003 Antonin Foller, PSTRUH Software, http://www.motobit.com
  14. 'Function GetINIString(Section, KeyName, Default, FileName)
  15. 'Sub WriteINIString(Section, KeyName, Value, FileName)
  16. Sub WriteINIString(Section, KeyName, Value, FileName)
  17.   Dim INIContents, PosSection, PosEndSection
  18.   
  19.   'Get contents of the INI file As a string
  20.   INIContents = GetFile(FileName)
  21.   'Find section
  22.   PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
  23.   If PosSection>0 Then
  24.     'Section exists. Find end of section
  25.     PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
  26.     '?Is this last section?
  27.     If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
  28.    
  29.     'Separate section contents
  30.     Dim OldsContents, NewsContents, Line
  31.     Dim sKeyName, Found
  32.     OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
  33.     OldsContents = split(OldsContents, vbCrLf)
  34.     'Temp variable To find a Key
  35.     sKeyName = LCase(KeyName & "=")
  36.     'Enumerate section lines
  37.     For Each Line In OldsContents
  38.       If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
  39.         Line = KeyName & "=" & Value
  40.         Found = True
  41.       End If
  42.       NewsContents = NewsContents & Line & vbCrLf
  43.     Next
  44.     If isempty(Found) Then
  45.       'key Not found - add it at the end of section
  46.       NewsContents = NewsContents & KeyName & "=" & Value
  47.     Else
  48.       'remove last vbCrLf - the vbCrLf is at PosEndSection
  49.       NewsContents = Left(NewsContents, Len(NewsContents) - 2)
  50.     End If
  51.     'Combine pre-section, new section And post-section data.
  52.     INIContents = Left(INIContents, PosSection-1) & _
  53.       NewsContents & Mid(INIContents, PosEndSection)
  54.   else'if PosSection>0 Then
  55.     'Section Not found. Add section data at the end of file contents.
  56.     If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
  57.       INIContents = INIContents & vbCrLf
  58.     End If
  59.     INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
  60.       KeyName & "=" & Value
  61.   end if'if PosSection>0 Then
  62.   WriteFile FileName, INIContents
  63. End Sub
  64. Function GetINIString(Section, KeyName, Default, FileName)
  65.   Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
  66.   
  67.   'Get contents of the INI file As a string
  68.   INIContents = GetFile(FileName)
  69.   'Find section
  70.   PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
  71.   If PosSection>0 Then
  72.     'Section exists. Find end of section
  73.     PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
  74.     '?Is this last section?
  75.     If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
  76.    
  77.     'Separate section contents
  78.     sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
  79.     If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
  80.       Found = True
  81.       'Separate value of a key.
  82.       Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
  83.     End If
  84.   End If
  85.   If isempty(Found) Then Value = Default
  86.   GetINIString = Value
  87. End Function
  88. 'Separates one field between sStart And sEnd
  89. Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
  90.   Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
  91.   If PosB > 0 Then
  92.     PosB = PosB + Len(sStart)
  93.     Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
  94.     If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
  95.     If PosE = 0 Then PosE = Len(sFrom) + 1
  96.     SeparateField = Mid(sFrom, PosB, PosE - PosB)
  97.   End If
  98. End Function
  99. 'File functions
  100. Function GetFile(ByVal FileName)
  101.   Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
  102.   'Go To windows folder If full path Not specified.
  103.   'If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
  104.   '  FileName = FS.GetSpecialFolder(0) & "\" & FileName
  105.   'End If
  106.   On Error Resume Next
  107.   GetFile = FS.OpenTextFile(FileName).ReadAll
  108. End Function
  109. Function WriteFile(ByVal FileName, ByVal Contents)
  110.   
  111.   Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
  112.   'On Error Resume Next
  113.   'Go To windows folder If full path Not specified.
  114.   'If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
  115.   '  FileName = FS.GetSpecialFolder(0) & "\" & FileName
  116.   'End If
  117.   Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
  118.   OutStream.Write Contents
  119. End Function
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 Nsqs 于 2017-3-22 12:21 编辑

回复 2# codegay

我的方法是一次读入再正则一次写入,不需要按行读,大致的模板,不需要复杂的算法.后期也方便修改

TOP

去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

返回列表