Problem with VBS and XML

Asked

Viewed 68 times

0

So people... all right? I have a little problem here, I have a VBS script that served me well, he read the XML files and renamed them to the vendor name.

Now I need it to rename the XML to the document issuance date(dhEmi), which in case is a DACTE. I tried to do here but it doesn’t work... Could you help me? Follow below the codes:

Excerpt from the XML:

<?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>
                <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)

    Set ElemList = objParser.getElementsByTagName("chNFe")
    FilePath = ElemList.Item(0).getAttribute("filePath")

    oldFileName = objFile.path

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

    newFileName = DiretorioDestino + dhEmi + ".xml"

    fso.MoveFile oldFileName, newFileName
End If
Next

Thank you!

1 answer

0


It seems that it is not working as it is trying to save a file that contains : in the middle of the name.

<dhEmi>2018-07-17T15:37:34-03:00</dhEmi>

You can change the code to something similar to this: (you can use another character)

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

newFileName = DiretorioDestino + dhEmi + ".xml"
  • That’s exactly what it was! Thank you very much!!!!!!! ---

Browser other questions tagged

You are not signed in. Login or sign up in order to post.