2010-08-16 58 views
4

我想将某些地址存储在文本文件中,然后根据组成员资格读取文件的特定部分。我已经完成了所有的组员资格,所以我不需要任何帮助。Vbscript - 读取特定部分的ini或文本文件

但我不确定我是否应该使用纯文本文件或INI文件?
事情是,邮政地址是在两三行,我需要换行。
我尝试使用纯文本文件,但我无法正确地获得换行符。

那么INI文件会更好?

ini文件看起来是这样的:

 
[London] 
Address 1 
Postbox 3245 
58348 London 

[Copenhagen] 
Address 2 
Postbox 2455 
5478347 Copenhagen 

我不能肯定这是否可能在INI文件不过,也许我需要命名每一行也是如此。或者,我可以使用纯文本文件并搜索单词[伦敦],然后读取每一行,直到出现换行符为止。然后将所有这些行存储在我将传递的变量中?

你们如何解决这个问题?

回答

1

我可能会使用CSV文件,而不是各行地方将代表一个国家。

Country,Address1,Address2,Address3,Address4 
London,Address 1,Postbox 3245,58348 London 
Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen 

如果你可以很容易地识别您的数据,那么你很可能有更多的描述性的列名(即Street1,STREET2,镇,邮政编码等)。

这种文件格式也很容易阅读,因为你只有一次读取输入文件的一行,并使用类似

aAddress = split(sLine, ",") 

为了更方便地工作,你可以使用字典把它分解对象,并使用国为一个键和阵列作为值

'sLine should be read from input file' 
sLine = "Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen" 

'Create dictionary for addresses' 
Set dic = CreateObject("Scripting.Dictionary") 

'Split line into array' 
aAddressParts = Split(sLine, ",") 

'Remove the first element of the array' 
sValues = Mid(sLine, InStr(sLine, ",")+1) 
aValues = Split(sValues, ",") 

'Add new entry into dictionary' 
dic.Add aAddressParts(0), aValues 

'Usage' 
MsgBox "Address for Copenhagen: " & vbNewLine & _ 
    Join(dic("Copenhagen"), "," & vbNewLine) 

谢谢, 马切伊

+0

好主意:)关于这一切的所有想法都是客户必须能够以可读格式轻松编辑数据。 CSV可以用Excel打开。伟大的提示! :) – 2010-08-22 21:08:05

1

您可以将地址存储在一行中并使用特殊字符(例如下划线)来指示换行符。阅读地址时,只需用换行符替换特殊字符即可。

[伦敦]
地址= “邮筒3245_58348 伦敦”

[哥本哈根]
地址= “邮筒 2455_5478347哥本哈根”

这使您可以存储地址更行或无邮箱行。根据我的经验,信息,如“我们的地址总是恰好有两条线,第一个是总是一个邮箱”是非常不正确常...

+0

尝试'替换(ReadIni,“_”,vbCrLf)',而没有'vbCrLf'周围的任何引号。 – Treb 2010-08-16 12:39:15

2

我已经写了一个小的VBScript类第在手柄“真实”的ini文件,写有这样的格式:

 
[section_name] 
key1 = value1 
key2 = value2 

为类的代码是:

Class IniFileObject 
    Private m_Data 

    Private Sub Class_Initialize 
     Set m_Data = Server.CreateObject("Scripting.Dictionary") 
    End Sub 

    Private Sub Class_Terminate 
     Dim key 
     If IsObject(m_Data) Then 
      For Each key In m_Data 
       m_Data(key).RemoveAll 
       Set m_Data(key) = Nothing 
      Next 
      m_Data.RemoveAll 
      Set m_Data = Nothing 
     End If 
    End Sub 

    Public Function Init(sFilePath) 
     Dim arrLines, sLine, x 
     Dim sCurSection, oSectionDict 
     Set Init = Me 
     arrLines = GetFileLines(sFilePath) 
     If Not(IsArray(arrLines)) Then Exit Function 
     sCurSection = "" 
     For x = 0 To UBound(arrLines) 
      sLine = Trim(arrLines(x)) 
      If Len(sLine)>0 Then 
       If Left(sLine, 1)="[" Then 
        If Not(HandleSectionLine(sLine, sCurSection)) Then Exit Function 
       Else 
        If Len(sCurSection)=0 Then 
         Err.Raise 1005, "IniFileObject init", "Found value outside any section (" & Server.HTMLEncode(sLine) & ")" 
         Exit Function 
        End If 

        Set oSectionDict = m_Data(sCurSection) 
        If Not(ParseOneLine(sLine, oSectionDict)) Then Exit Function 
        Set m_Data(sCurSection) = oSectionDict 
       End If 
      End If 
     Next 
    End Function 

    Public Property Get ReadValue(section, key) 
     Dim oSectionDict 
     ReadValue = "" 
     If m_Data.Exists(section) Then 
      Set oSectionDict = m_Data(section) 
      If oSectionDict.Exists(key) Then ReadValue = oSectionDict(key) 
     End If 
    End Property 

    Private Function ParseOneLine(ByVal sLine, ByRef oSectionDict) 
     Dim arrTemp, sErrorMsg, sKey 
     sErrorMsg = "" 
     ParseOneLine = True 
     If Left(sLine, 2)="//" Or Left(sLine, 1)="'" Or Left(sLine, 1)="{" Then Exit Function 
     arrTemp = Split(sLine, "=") 
     If UBound(arrTemp)=1 Then 
      sKey = Trim(arrTemp(0)) 
      If (Len(sKey)>0) And (Len(arrTemp(1))>0) Then 
       If Not(oSectionDict.Exists(sKey)) Then 
        oSectionDict.Add sKey, Trim(arrTemp(1)) 
       Else 
        sErrorMsg = "Key already exists" 
       End If 
      Else 
       sErrorMsg = "Empty key or value" 
      End If 
     Else 
      sErrorMsg = "Missing or too much '=' characters" 
     End If 
     Erase arrTemp 
     If Len(sErrorMsg)>0 Then 
      ParseOneLine = False 
      Err.Raise 1006, "IniFileObject Init", "Failed to parse single line (" & Server.HTMLEncode(sLine) & "): " & sErrorMsg 
     End If 
    End Function 

    Private Function HandleSectionLine(ByVal sLine, ByRef sCurSection) 
     HandleSectionLine = False 
     If (Len(sLine)<3) Or (Right(sLine, 1)<>"]") Then 
      Err.Raise 1002, "IniFileObject init", "Invalid line found: " & Server.HTMLEncode(sLine) 
      Exit Function 
     End If 

     sCurSection = Mid(sLine, 2, Len(sLine) - 2) 
     If m_Data.Exists(sCurSection) Then 
      Err.Raise 1003, "IniFileObject init", "Section exists more than once: " & Server.HTMLEncode(sCurSection) 
      Exit Function 
     End If 

     m_Data.Add sCurSection, Server.CreateObject("Scripting.Dictionary") 
     HandleSectionLine = True 
    End Function 

    Private Function GetFileLines(sFilePath) 
     Dim objFSO, oFile 
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
     If Not(objFSO.FileExists(sFilePath)) Then 
      Set objFSO = Nothing 
      Err.Raise 1001, "IniFileObject init", "file path '" & Server.HTMLEncode(sFilePath) & "' does not exist, check permissions" 
      Exit Function 
     End If 
     Set oFile = objFSO.OpenTextFile(sFilePath) 
     GetFileLines = Split(oFile.ReadAll, VBCrLf) 
     oFile.Close 
     Set oFile = Nothing 
     Set objFSO = Nothing 
    End Function 
End Class 

用例:

Dim filePath, ini 
filePath = Server.MapPath("config.ini") 
Set ini = New IniFileObject.Init(filePath) 
Response.Write("Value for 'Key001': " & ini.ReadValue("MySection", "Key001") & "<br />") 
Set ini = Nothing 

代码抛出的各种错误时文件不存在或包含无效行,错误非常明显。这是可能的“抑制”的错误,并通过使用这些代码消耗时不显示错误页面:

On Error Resume Next 
    Set ini = New IniFileObject.Init(filePath) 
    If Err.Number<>0 Then 
     Response.Write("Error reading ini file") 
    End If 
On Error Goto 0 
If IsObject(ini) Then 
    Response.Write("Value for 'IP001': " & ini.ReadValue("IPaddress", "IP001") & "<br />") 
    Set ini = Nothing 
End If 
0

我用一个小的可执行文件,启动本机API为:GetPrivateProfileStringWritePrivateProfileString

可执行文件叫这样的:

Set sh = CreateObject("WScript.Shell") 
Set exec = sh.Exec("ini.exe get %APPDATA%\sth\file.ini ""Section name"" key") 
sFirma1 = exec.StdOut.ReadLine 
Call sh.Run("ini.exe set %APPDATA%\sth\file.ini ""Section name"" key set_value", 0) 

Running command line silently with VbScript and getting output?见。

这是可执行的代码:

#include <stdio.h> 
#include <windows.h> 

void usage() 
{ 
    puts("ini <get>/<set> <file> <section> <key> <value>"); 
    exit(1); 
} 

int main(int cArg, char **aszArg) 
{ 
    int iFile = 2; 
    int iSection = 3; 
    int iKey = 4; 
    int iValue = 5; 
    if (cArg < 5) usage(); 
    if (strcmp(aszArg[1], "get") != 0 && strcmp(aszArg[1], "set") != 0) usage(); 
    if (strcmp(aszArg[1], "set") == 0 && cArg < iValue + 1) usage(); 
    if (strcmp(aszArg[1], "set") == 0) { 
    if (!WritePrivateProfileString(aszArg[iSection], aszArg[iKey], 
            aszArg[iValue], aszArg[iFile])) 
     puts("Failure in WriteProfileString."); 
    } else { 
    char buf[1000]; 
    buf[0] = 0; 
    GetPrivateProfileString(
     aszArg[iSection], aszArg[iKey], "", buf, 999, aszArg[iFile]); 
    puts(buf); 
    } 
    return 0; 
} 

你需要使用一个Windows的C编译器来编译它。我是用gcc做的,但是ms的免费编译器也可以工作。如果带有32位可执行文件的this page仍然可用,那么您可以尝试一下,但请自行承担责任。黑客已经访问过我的网站一次。