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

[问题求助] 【已解决】vbs怎么直接执行包含vbs代码的ini文件

本帖最后由 raozhao2008 于 2015-11-9 15:46 编辑

如题:vbs怎么直接执行包含vbs代码的ini文件?ini文件,顾名思义:它是个配置文件,里面全是用vbs格式设置的变量
现在我想用主vbs程序直接执行这个.ini,将变量传递给主程序(不知这样是否可行?),
请问各位大侠该怎么做?
  1. ’123.ini
  2. a=as
  3. av=1
  4. b=f
  5. bv=1
  6. df=dfc
  7. dfv=1
复制代码

  1. execute("a=123:b=345")
  2. msgbox a+b
复制代码

TOP

回复 2# pcl_test


   我想直接执行.ini文件,行吗

TOP

回复 3# raozhao2008

read and convert

TOP

本帖最后由 B魔方大人 于 2015-11-7 20:20 编辑

ini文件改成自己,比如d:\a.ini
  1. set f=creatobject("scripting.filesystemobject").opentextfile("ini文件",1)
  2. do until f.atendofstream
  3. redim preserve st(k)
  4. st(k)=f.readline:loop:f.close
  5. s=join(st,":")
  6. EXEcute(s)
  7. s="":f=""
复制代码

TOP

本帖最后由 yu2n 于 2015-11-8 23:15 编辑
ini文件
http://baike.baidu.com/view/1296365.htm

文件扩展名
配置文件.ini
请注意:我们所讨论的是项目中的配置文件,它是整个项目共用的。所以它要有一个项目使用的文件名,其后缀是.ini。例如:端口配置.ini

格式
INI文件由节、键、值组成。


[section]

参数(键=值)
name=value

注解
注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。
; comment text
; INI文件的数据格式的例子(配置文件的内容)
[Section1 Name]
KeyName1=value1
KeyName2=value2
...
[Section2 Name]
KeyName21=value21
KeyName22=value22
其中:
[Section1 Name]用来表示一个段落。
因为INI文件可能是项目中共用的,所以使用[Section Name]段名来区分不同用途的参数区。例如:[Section1 Name]表示传感器灵敏度参数区;[Section2 Name]表示测量通道参数区等等。
KeyName1=value1 用来表示一个参数名和值。
比如:
7033=50
7034=51
其中:
7033表示某传感器名,50表示它的灵敏度值。
7034表示另一只传感器名,51表示它的灵敏度值。

实例
; exp ini file
[port]
portname=COM4
port=4

一个连“节”都没有的INI文件,也能叫INI文件?(“节”不可失!)

VBS 读写 INI 文件实例 1
  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
复制代码
VBS 读写 INI 文件实例 2
  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

返回列表