2010-08-16 41 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仍然可用,那麼您可以嘗試一下,但請自行承擔責任。黑客已經訪問過我的網站一次。