如何删除字符并忽略分隔符?

时间:2013-11-26 11:35:23

标签: vbscript

我有一个包含联系人的csv文件。这些联系人应通过vb-Script导入Exchange-Server。问题是源文件包含被解释为分隔符的符号:

Germany,Seehger,Warehouse,Seehger,"Schmitt, Michael",,Michael,,,,,michael.schmitt@seehger.de,,,,61462,Rosbach,,Frankfurter Strasse 243,,Warehouse
,,,,"Backup, Network",,Network,,,,,NBackup@xyz.com,,,,,Backup,,,,

第一行是“施密特,迈克尔”。在第二行,它是“备份,网络”。如何删除引号并确保不将逗号解释为分隔符。

我的第一种方法是使用正则表达式在数据导入之前清理数据,这是不可能的。

这是我的剧本:

Dim objFSO, objQuelldatei, objOu, objADKontakt, objKontakt
Dim strQuelldatei, strKontaktOU, strZeileTemp, strtemp
Dim aUserdaten

Const ForReading = 1   

strKontaktOU = "LDAP://ou=KontakteTest,dc=exp,dc=xyz,dc=de"

strQuelldatei = "C:\test.csv"

Set objOu = GetObject(strKontaktOU)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objQuelldatei = objFSO.OpenTextFile(strQuelldatei, ForReading)
    Do Until objQuelldatei.AtEndOfStream 
        strZeileTemp = objQuelldatei.Readline 

            If InStr(strZeileTemp, Chr(34) >
               'Werte per Komma trennen und in eindimensionales Array schreiben
               aUserdaten = Split(strZeileTemp, ",") 

               emailExists = False
                   If Not emailExists Then

                   strtemp = aUserdaten(4)
                   wscript.echo strtemp

                   Set objKontakt = objOu.Create("contact", "CN=" &strtemp)

                   objKontakt.put "co" , aUserdaten(0)
                   objKontakt.put "company" , aUserdaten(1)
                   objKontakt.put "department" , aUserdaten(2)
                   objKontakt.put "description" , aUserdaten(3)
                   objKontakt.put "displayName" , aUserdaten(4)
                   objKontakt.put "facsimileTelephoneNumber" , aUserdaten(5)
                   objKontakt.put "givenName" , aUserdaten(6)
                   objKontakt.put "homePhone" , aUserdaten(7)
                   objKontakt.put "info" , aUserdaten(8)
                   objKontakt.put "initials" , aUserdaten(9)
                   objKontakt.put "ipPhone" , aUserdaten(10)
                   objKontakt.put "mail" , aUserdaten(11)
                   objKontakt.put "mobile" , aUserdaten(12)
                   objKontakt.put "pager" , aUserdaten(13)
                   objKontakt.put "physicalDeliveryOfficeName" , aUserdaten(14)
                   objKontakt.put "postalCode" , aUserdaten(15)
                   objKontakt.put "sn" , aUserdaten(16)
                   objKontakt.put "st" , aUserdaten(17)
                   objKontakt.put "streetAddress" , aUserdaten(18)
                   objKontakt.put "telephoneNumber" , aUserdaten(19)
                   objKontakt.put "title" , aUserdaten(20)
                   objKontakt.SetInfo
            End If                 
    Loop 
objQuelldatei.Close 

1 个答案:

答案 0 :(得分:4)

不要将split等与csv文件一起使用。使用ADODB。这是一个例子:

Option Explicit

Const PATH = "C:\folder\where\csv\sits"
Const FILE = "filename.csv"

Dim cn,rs
Dim val

Set cn = CreateObject("ADODB.Connection")

'if the first line is column names such as UserName, Phone etc, set HDR=YES
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & PATH & ";" & _
          "Extended Properties=""text;HDR=NO;FMT=Delimited"""

Set rs = cn.Execute("Select * From [" & FILE & "]")

While Not rs.EOF
  'if HDR=YES then get columns like this:
  val = rs.Fields.Item("UserName").Value 'where UserName is a header
  'if HDR=NO then get columns like this:
  val = rs.Fields.Item("F2").Value 'gets the second column
  If Not isNull(val) Then
    'do something with the value
    WScript.Echo val
  End If
  rs.MoveNext
Wend 

cn.close
Set cn = Nothing
Set rs = Nothing
WScript.Quit

希望这会指出你正确的方向并防止'分界头痛'