VBScript的新手并尝试执行嵌套的For Each循环

时间:2018-04-18 17:29:43

标签: foreach vbscript

我昨天拿起了VBScript,我正在尝试接收文件,复制文件,更改文件中的一些文本并输出一个新名称的新文件。该文件正在复制并保存在正确的位置,但我不确定我的代码在哪里出错。 “过滤器”文本在所有文件中都在变化,但它只是更改为过滤器(14)。

非常感谢任何帮助,指导或指导。我已经把我目前的代码放在了下面:

  Const ForReading = 1
Const ForWriting = 2

Dim outputFile(14)
outputFile(0) = "MMCLAgenda_BehavioralHealth.txt"
outputFile(1) ="MMCLAgenda_CaseandCareSupport.txt"
outputFile(2) ="MMCLAgenda_CaseManagement.txt"
outputFile(3) ="MMCLAgenda_ChangeOrder.txt"
outputFile(4) ="MMCLAgenda_DM_BehavioralHealth.txt"
outputFile(5) ="MMCLAgenda_DM_Overarching.txt"
outputFile(6) ="MMCLAgenda_DM_PhysicalHealth.txt"
outputFile(7) ="MMCLAgenda_DM_Supervisors.txt"
outputFile(8) ="MMCLAgenda_DM_SupportStaff.txt"
outputFile(9) ="MMCLAgenda_DM_Workgroups.txt"
outputFile(10) ="MMCLAgenda_InpatientUM.txt"
outputFile(11) ="MMCLAgenda_LeadershipCommittee.txt"
outputFile(12) ="MMCLAgenda_OutpatientUM.txt"
outputFile(13) ="MMCLAgenda_PolicyAndProcedureGroup.txt"
outputFile(14) ="MMCLAgenda_UMCommittee.txt"

Dim filter(14)
filter(0) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Behavior Health' : 'Behavioral Health'</Formula>"
filter(1) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Medical Management Call Center'</Formula>"
filter(2) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Case Management'</Formula>"
filter(3) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Change Orders'</Formula>"
filter(4) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Disease Management/Behavior Health/Anxiety' : 'DM Behavioral Health - Anxiety and Depression'</Formula>"
filter(5) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Disease Management/Overarching'</Formula>"
filter(6) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name =  'Disease Management/Clinical Staff'</Formula>"
filter(7) = "<Formula>@Created &gt; @Date(2015;12;31) &amp; COL_Name = 'Disease Management/Supervisors'</Formula>"
filter(8) = "<Formula>@Created &gt; @Date(2015;12;31) &amp; COL_Name = 'Disease Management/Support Staff'</Formula>"
filter(9) = "<Formula>@Created &gt; @Date(2015;12;31)</Formula>"
filter(10) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Transitional Care'</Formula>"
filter(11) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Leadership Committee'</Formula>"
filter(12) = "<Formula>@Created &gt; @Date(2015;12;31) &amp; COL_Name = 'Utilization Management'</Formula>"
filter(13) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'Policy &amp; Procedures Workgroup'</Formula>"
filter(14) = "<Formula>@Created &gt; @Date(2015;12;31)&amp; COL_Name = 'TEST'</Formula>"

Dim destination(14)
destination(0) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/BH"
destination(1) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/CCS"
destination(2) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/CM"
destination(3) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/CO"
destination(4) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMBH"
destination(5) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMO"
destination(6) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMPH"
destination(7) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMS"
destination(8) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMSS"
destination(9) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/DMW"
destination(10) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/IUM"
destination(11) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/LC"
destination(12) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/OUM"
destination(13) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/PPG"
destination(14) = "https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL/UMC"

fileLocation = "C:\Users\HN142083\Desktop\MigrationJobAutomation\Jobs\"



'**********************************************************************************************************************************
'***for each file in outputFiles -> take FileIn, FileOut = outputFiles(i) -> change strNewText and change destination -> go next***
'**********************************************************************************************************************************

For each x in outputFile

    FileIn = "C:\Users\HN142083\Desktop\MigrationJobAutomation\Jobs\MMCLAgenda_BehavioralHealth.txt"
    FileOut = fileLocation+x

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(FileIn, ForReading)

    strText = objFile.ReadAll
    strTextDestination = strText
    objFile.Close


        For each item in filter

            strNewText = Replace(strText, "<Formula>@Created &gt; @Date(2015; 12; 31)</Formula>", item)
        Next

        For each record in destination
            strTextDestination = Replace(strTextDestination, "<Web>https://testcnet.centene.com/sites/LNTestMigration/Issac/MMCL</Web>", record)
        Next

    Set objFile = objFSO.OpenTextFile(FileOut, ForWriting)
    objFile.WriteLine strNewText
    objFile.Close


Next

1 个答案:

答案 0 :(得分:0)

因为这个循环

For each item in filter
    strNewText = Replace(strText, "<Formula>@Created &gt; @Date(2015; 12; 31)</Formula>", item)
Next
针对strText的每个项目执行

,您将其设置为文件x的内容。在filter循环的第一次迭代中,使用strNewTextstrText变量设置为filter(0)的替换版本。然后使用strText将其设置为filter(1)的替换版本,依此类推,直到filter(14)之后循环完成并且不再设置strNewText

strText永远不会被更改,因此循环只会strNewText仅使用filter(14)提供等效替换。

如果需要进一步解释,请告诉我?