PROBLEM with parent TAG in VBS

0
Are people ... all right? I have a problem here very similar to one I had yesterday, I have a VBS script that caters me well, it basically reads the XML, takes the date inside the XML and renames the file with that date ...

Now I need it to rename the XML with CNPJ followed by the date, but the problem is that the CNPJ tag is not unique, there are multiple CNPJs in the xml ... I need only the CNPJ inside the tag, I managed to make it work in a way here, but there are some XML that I get that the order changes, so as it takes the first CNPJ tag that it has, sometimes it gets the CNPJ from another tag other than the. can you help me? Here are the codes:

XML snippet:

<?xml version = "1.0" encoding = "ISO-8859-1"?>
<cteProc versao="3.00" xmlns="http://www.portalfiscal.inf.br/cte">
    <CTe xmlns="http://www.portalfiscal.inf.br/cte">
        <infCte versao="3.00" Id="CTe33180702012862001050570060001000081465016982">
            <ide>
                <cUF>33</cUF>
                <cCT>sad1698</cCT>
                <CFOP>6asd351</CFOP>
                <natOp>PREST. SERV DE TRANSP. PARA EXECUÇÃO SERV. DA MESMA NATUREZA</natOp>
                <mod>57</mod>
                <serie>6</serie>
                <nCT>10s0008</nCT>
                <dhEmi>2018-07-17T15:37:34-03:00</dhEmi>
                <tpImp>1</tpImp>
                <tpEmis>1</tpEmis>
                <cDV>2</cDV>
                <tpAmb>1</tpAmb>
                <tpCTe>0</tpCTe>
                <procEmi>0</procEmi>
                <verProc>Triangulus_2.0.28.48</verProc>
                <cMunEnv>3304557</cMunEnv>
                <xMunEnv>RIO DE JANEIRO</xMunEnv>
                <UFEnv>RJ</UFEnv>
                <modal>02</modal>
                <tpServ>0</tpServ>
                <cMunIni>3304557</cMunIni>
                <xMunIni>RIO DE JANEIRO</xMunIni>
                <UFIni>RJ</UFIni>
                <cMunFim>3518800</cMunFim>
                <xMunFim>GUARULHOS</xMunFim>
                <UFFim>SP</UFFim>
                <retira>0</retira>
                <xDetRetira>AEROPORTO</xDetRetira>
                <indIEToma>1</indIEToma>
                <toma3>
                    <toma>3</toma>
                </toma3>
            </ide>
            <compl>
                <xCaracSer>PRÓXIMO DIA</xCaracSer>
                <xEmi>Tauany Peçanha Ayre</xEmi>
                <fluxo>
                    <xOrig>SDU</xOrig>
                    <xDest>GRU</xDest>
                </fluxo>
                <destCalc>3518800</destCalc>
                <xObs>ICMS CONFORME RESOLU??O DO SENADO FEDERAL 95/96</xObs>
                <ObsCont xCampo="TIPO DE PAGAMENTO">
                    <xTexto>CONTA CORRENTE - 638877SAO - 19.89</xTexto>
                </ObsCont>
                <ObsCont xCampo="CONTA CORRENTE">
                    <xTexto>638877SAO</xTexto>
                </ObsCont>
                <ObsCont xCampo="FINANCIERO">
                    <xTexto>Lei da transparência 12.741/12, o percentual aproximado dos tributos incidentes sobre o preço do serviço são: Federal: 15,96% Estadual: 4,0%</xTexto>
                </ObsCont>
            </compl>
            <emit>
                <CNPJ>02012862001050</CNPJ>   <---- PRECISO QUE SEJA ESSE CNPJ EM ESPECIFICO
                <IE>84328820</IE>
                <xNome>TAM LINHAS AEREAS SA SDU</xNome>
                <enderEmit>
                    <xLgr>PC SENADOR SALGADO FILHO</xLgr>
                    <nro>0</nro>
                    <xCpl>AEROPORTO SANTOS DUMONT</xCpl>
                    <xBairro>AEROPORTO SANTOS DUMONT</xBairro>
                    <cMun>3304557</cMun>
                    <xMun>RIO DE JANEIRO</xMun>
                    <CEP>20021340</CEP>

VBS file:

DiretorioobjFiles = "C:\DACTE\"
DiretorioDestino = "C:\DACTE\DATA\"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(DiretorioobjFiles)
Wscript.Echo objFolder.Path

Set colFiles = objFolder.Files


For Each objFile in colFiles

On Error Resume Next
If Right(objFile.FileName, 4) = ".xml" Then

    Set objParser = CreateObject("Microsoft.XMLDOM")
    objParser.Load (objFile.path)

    oldFileName = objFile.path

    Set ElemList = objParser.getElementsByTagName("emit").getElementsByTagName("CNPJ")
    CNPJ = ElemList.Item(0).Text

    Set ElemList = objParser.getElementsByTagName("dhEmi")
    dhEmi = Replace(ElemList.Item(0).Text, ":", "_")

    newFileName = DiretorioDestino + CNPJ + "_" + dhEmi + ".xml"

    fso.MoveFile oldFileName, newFileName
End If
Next
    
asked by anonymous 09.08.2018 / 22:26

1 answer

1

I have been able to resolve people, if anyone needs something similar, follow the code:

DiretorioobjFiles = "C:\DACTE\"
DiretorioDestino = "C:\DACTE\DATA\"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(DiretorioobjFiles)
Wscript.Echo objFolder.Path

Set colFiles = objFolder.Files


For Each objFile in colFiles

On Error Resume Next
If Right(objFile.FileName, 4) = ".xml" Then

    Set objParser = CreateObject("Microsoft.XMLDOM")
    objParser.Load (objFile.path)

    oldFileName = objFile.path

    Set ElemList = objParser.getElementsByTagName("./emit/CNPJ")
    CNPJ = ElemList.Item(0).Text

    Set ElemList = objParser.getElementsByTagName("dhEmi")
    dhEmi = Replace(ElemList.Item(0).Text, ":", "_")

    newFileName = DiretorioDestino + CNPJ + "_" + dhEmi + ".xml"

    fso.MoveFile oldFileName, newFileName
End If
Next
    
09.08.2018 / 23:55