来自一个变量 VBScript 的多个文本文件

Multiple textfiles from one variable VBScript

我有一些 VBScript 代码可以从属性中收集数据并将它们写入文件。

它完全可以工作,但每次输出应该只包含一行。 对于 10 个对象,10 个文件,一行。 但是有些对象有变化,这就是输出文件包含两行或更多行的原因。

你能帮我出出主意吗?

包含数据的变量称为 lGesamt

For Each lnkOberstoff In MwScriptObj.Links(fldOberstoff)
    lGesamt = ""
    Set objOberstoff = lnkOberstoff.object
    liefName = objOberstoff.Property(1482).Value(0)
    liefArtikel = objOberstoff.Property(1801).Value(0)
    saison = objOberstoff.Property(1527).Value(0)

    For Each varcolor In objOberstoff.Variations(propDefFb)
    'VarNo = MwScriptObj.VarNoFromVarId(varcolor.value)
        Farben = ""                
        Farben = varcolor.Value
        Dim Lieferant
        Lieferant = objOberstoff.Value(prpLieferant, Nothing, varcolor)
        Dim LieferantenArtikel
        LieferantenArtikel = objOberstoff.Value(prpLieferantenArtikel, Nothing, varcolor)
        Dim Saison
        Saison = objOberstoff.Value(prpSaison, Nothing, varcolor)
        Dim Thema
        For each lnkGtThema in objOberstoff.Links(fldGtThema)
            Set objGtThema = lnkGtThema.object
            Thema = objGtThema.Property(2577).Value(0)
        Next
        Dim LieferantenDessin
        LieferantenDessin = objOberstoff.Value(prpLieferantenDessin, Nothing, varcolor)
        Dim Dessin
        Dessin = objOberstoff.Value(prpDessin, Nothing, varcolor)
        Dim LieferantenFarbe
        LieferantenFarbe = objOberstoff.Value(prpLieferantenFarbe, Nothing, varcolor)
        Dim MengeGeliefert
        MengeGeliefert = objOberstoff.Value(prpMengeGeliefert, Nothing, varcolor)
        Dim SmsNutzbreite
        SmsNutzbreite = objOberstoff.Value(prpSmsNutzbreite, Nothing, varcolor)
        Dim GewichtsEinheit
        GewichtsEinheit = objOberstoff.Value(prpGewichtsEinheit, Nothing, varcolor)
        Dim GewichtBrutto
        GewichtBrutto = objOberstoff.Value(prpGewichtBrutto, Nothing, varcolor)
        Dim Materialzusammensetzung
        Materialzusammensetzung = objOberstoff.Value(prpMaterialzusammensetzung, Nothing, varcolor)
        'Beschreibung
        Dim MaterialgruppenNr
        MaterialgruppenNr = objOberstoff.Value(prpMaterialgruppenNr, Nothing, varcolor)
        Dim MaterialUntergruppenNr
        MaterialUntergruppenNr = objOberstoff.Value(prpMaterialUntergruppenNr, Nothing, varcolor)
        Dim ERPNummer
        ERPNummer = objOberstoff.Value(prpERPNummer, Nothing, varcolor)
        Dim Firma
        Firma = objOberstoff.Value(prpFirma, Nothing, varcolor)
        lGesamt = lGesamt & Lieferant & ";" & LieferantenArtikel & ";" & Saison & ";" & Thema & ";" & LieferantenDessin & ";" & Dessin & ";" & LieferantenFarbe & ";" & Farben & ";" & Mengegeliefert & ";" & SmsNutzbreite & ";" & GewichtBrutto & Gewichtseinheit & ";" & Materialzusammensetzung & ";" & MaterialgruppenNr &  "/" & MaterialUntergruppenNr &  "/" & ERPNummer &  ";" & Firma & vbNewLine
        'MsgBox lGesamt & "lGesamt"
    Next

    lOutput = lOutput & lGesamt
    MsgBox lOutput
    desPath = "P:\Musterlaschenetiketten\"
    datName = saison & "-" & MwScriptObj.Property(4758).Value(0) & "-" & MwScriptObj.Property(4003).Value(0) & ".txt"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile(desPath & datName)
    objFile.WriteLine(lOutput)
    objFile.Close
Next

这是输出文件的内容:

Ipeker Tekstil A. S.;00553 Destiny;W8;T110;88361;745;;0335;;134;83G/QM;100CV;01/40/204;
Ipeker Tekstil A. S.;00553 Destiny;W8;T110;88361;745;IJ11/T200 frei;0935;;137;83G/QM;100CV;01/40/204;

这两行应该放在不同的文件中。

大概是这样的:

  1. objOberstoff.Variations(propDefFb) returns 2 个结果,导致内部循环的 2 次迭代。
  2. 每次迭代向 lGesamt 添加一行。
  3. 内循环完成后,这两行连接到 lOutput应该 实际上会导致每个新文件包含 all 之前写入的数据,除非你在外层循环开始时重新设置变量,就像lGesamt).

要解决此问题,您需要停止附加到变量并将写入输出的代码移至内部循环。您还需要向输出文件名添加某种索引,否则内部循环的后续迭代将覆盖输出文件。

我还建议将循环内不变的定义移到循环外(比如目标文件夹和FileSystemObject实例)。

将您的代码更改为类似这样的代码,它应该会执行您想要的操作:

dst = "P:\Musterlaschenetiketten"

Set fso = CreateObject("Scripting.FileSystemObject")

For Each lnkOberstoff In MwScriptObj.Links(fldOberstoff)
    i = 1
    ...
    For Each varcolor In objOberstoff.Variations(propDefFb)
        ...
        lGesamt = Lieferant & ";" & LieferantenArtikel & ";" & Saison & ";" & _
                  Thema & ";" & LieferantenDessin & ";" & Dessin & ";" & _
                  LieferantenFarbe & ";" & Farben & ";" & Mengegeliefert & ";" & _
                  SmsNutzbreite & ";" & GewichtBrutto & Gewichtseinheit & ";" & _
                  Materialzusammensetzung & ";" & MaterialgruppenNr &  "/" & _
                  MaterialUntergruppenNr &  "/" & ERPNummer &  ";" & Firma

        fname = saison & "-" & MwScriptObj.Property(4758).Value(0) & "-" & _
                MwScriptObj.Property(4003).Value(0) & "_" & i & ".txt"

        Set objFile = fso.CreateTextFile(fso.BuildPath(dst, fname))
        objFile.WriteLine(lGesamt)
        objFile.Close
        i = i + 1
    Next
Next