2017-12-29 49 views
1

特定の属性Niv=に基づいてすべての.xmlファイルの名前を変更するバッチファイルを生成しようとしています。しかし、私はそのような変更を行うことはできませんでした。特定のXMLノード値を使用して複数のXMLファイルの名前を一括して変更する方法は?

ノードはnoIdentificacionというノードで動作するようにサポートしていましたが、下に追加されたファイルにNiv=が必要です。 XMLファイルの

@ECHO OFF 
SETLOCAL EnableExtensions DisableDelayedExpansion 
REM Change the current directory: your choice 
pushd "C:\Users\Javier Preciado\Desktop\sistemas\" 
REM Get a static list of `xml` files 
for /f "delims=" %%Z in ('dir /b *.xml') do (
    REM clear a sentinel flag 
    set "_Niv=" 
    REM Search for a particular text string in a file 
    for /f "delims=" %%A in ('find /i "Niv=" ^< "%%Z"') do (
     REM The FOR command is mostly used to process a set of one or more files, 
     REM  but you can also process a set of one or more text strings: 
     for %%G in (%%A) do (
      REM debugging output 
      echo debug %%G 
      REM found the particular text string in the line? 
      if /I "%%~G"=="Niv" (
       set "_Niv=Niv" REM set the sentinel flag 
      ) else (
       if defined _noIdentificacion (
        REM debugging output 
        echo found Niv in file "%%~Z": "%%~G" 
        REM check as to whether a file isn't renamed already 
        if EXIST "%%~Z" if /I "%%~Z" NEQ "%%~G.xml" (
         REM remove ECHO from next line no sooner than debugged 
         ren "%%~Z" "%%~G.xml" 
        ) 
       ) 
       REM clear the sentinel flag 
       set "_Niv=" 
      ) 
     ) 
    ) 
) 
REM Change directory back to the path/folder most recently stored by the PUSHD command. 
popd 

内容:

<?xml version="1.0" encoding="UTF-8" ?><cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ventavehiculos="http://www.sat.gob.mx/ventavehiculos" xmlns:terceros="http://www.sat.gob.mx/terceros" xmlns:Toyota="http://www.pegasotecnologia.com/secfd/Schemas" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd http://www.sat.gob.mx/ventavehiculos http://www.sat.gob.mx/sitio_internet/cfd/ventavehiculos/ventavehiculos11.xsd http://www.sat.gob.mx/terceros http://www.sat.gob.mx/sitio_internet/cfd/terceros/terceros11.xsd http://www.pegasotecnologia.com/secfd/Schemas http://www.pegasotecnologia.com/secfd/Schemas/AddendaEmisorToyota.xsd" Version="3.3" Serie="A" Folio="000814547" Fecha="2017-12-01T15:27:55" Moneda="MXN" TipoCambio="1" SubTotal="292556.52" Total="338928.57" FormaPago="99" CondicionesDePago="Inmediato" TipoDeComprobante="I" MetodoPago="PPD" LugarExpedicion="45110" NoCertificado="00001000000402850319" Certificado="MIIGUjCCBDqgAwIBAgIUMDAwMDEwMDAwMDA0MDI4NTAzMTkwDQYJKoZIhvcNAQELBQAwggGyMTgwNgYDVQQDDC9BLkMuIGRlbCBTZXJ2aWNpbyBkZSBBZG1pbmlzdHJhY2nDs24gVHJpYnV0YXJpYTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3RyYWNpw7NuIFRyaWJ1dGFyaWExODA2BgNVBAsML0FkbWluaXN0cmFjacOzbiBkZSBTZWd1cmlkYWQgZGUgbGEgSW5mb3JtYWNpw7NuMR8wHQYJKoZIhvcNAQkBFhBhY29kc0BzYXQuZ29iLm14MSYwJAYDVQQJDB1Bdi4gSGlkYWxnbyA3NywgQ29sLiBHdWVycmVybzEOMAwGA1UEEQwFMDYzMDAxCzAJBgNVBAYTAk1YMRkwFwYDVQQIDBBEaXN0cml0byBGZWRlcmFsMRQwEgYDVQQHDAtDdWF1aHTDqW1vYzEVMBMGA1UELRMMU0FUOTcwNzAxTk4zMV0wWwYJKoZIhvcNAQkCDE5SZXNwb25zYWJsZTogQWRtaW5pc3RyYWNpw7NuIENlbnRyYWwgZGUgU2VydmljaW9zIFRyaWJ1dGFyaW9zIGFsIENvbnRyaWJ1eWVudGUwHhcNMTYwNjE3MTUwNzM4WhcNMjAwNjE3MTUwNzM4WjCB8jEzMDEGA1UEAxMqVE9ZT1RBIE1PVE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IabiIKElqcLXYwYIPUrsMCSggSTS+TtCJOWfDI7f2xE2aCCvfBi4Ag0UVeitpklFzmAkYMLfDZkT4w2IlpK4Q5KwN3L5E7KRmIv5e3uP8EAGN6z6dIAM27rVcHbWzirzScO1/R/WooPUuQ1UXkjNh1jflAQQSjZ0KR4KrE/194bKTbPEKFutk86i3wX3Qrv7H6iOuh7gXk6Pa87MFAPnzJ5M4ftoGnIYrRa28kvSfgXD5erm0cMTgsoBDrz8BnaQCWqjVhR8nS6Yudlfo8GS6NySE+3LzC4vK+kVhJSK0dXy5cgAyvHTUea0GddJieQUZquJKmD7ns++ZEHcbZN2rKyc8WNmbb2u+ZjmioY=" Sello="LV1beEcSeRBLjmIrZV6wwQ5TcYQkYeuekAKtAmgRsGD5EezvevH4gv3+93E8Wk9CxBJNVRYLHLlG3uxR7rXawpBTbgPJFCPSt3a0q+Rb3OnBdu8BAdu/lNi4lGojrgzcbthZnPD0u9WoJOiDFI4494wUDR4dF2WJr+8QYYxSfXtMQV065dOrDDtmfJektPIS0hq1JAkknHpThExjX8s0UuV2+MVTciu+DVXoa4G5wp0ldLCWLSzsR5iXulATCEgodrynw5/kvLKqZkI2GAy5oCO2GA0REHFosIlISuNBhD08fcEOwju+NVHKGdjXZ89YpkCXMVxhrL0CQVHZ1dxuNQ=="><cfdi:Emisor Rfc="TMS010508RX0" Nombre="TOYOTA MOTOR SALES DE MEXICO S DE RL DE CV" RegimenFiscal="601" /><cfdi:Receptor Rfc="OAU021125H84" Nombre="OZ AUTOMOTRIZ S DE RL DE CV" UsoCFDI="P01" /><cfdi:Conceptos><cfdi:Concepto ClaveProdServ="25101500" NoIdentificacion="56012018" Cantidad="1.00" ClaveUnidad="EA" Unidad="EA" Descripcion="Hiace Panel Van Super Long" ValorUnitario="283717.25" Importe="283717.25"><cfdi:Impuestos><cfdi:Traslados><cfdi:Traslado Base="283717.25" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="45394.76" /></cfdi:Traslados></cfdi:Impuestos><cfdi:InformacionAduanera NumeroPedimento="17 51 3788 7004005" /><cfdi:ComplementoConcepto><ventavehiculos:VentaVehiculos version="1.1" ClaveVehicular="1520202" Niv="JTFPX22PXJ0080089"><ventavehiculos:InformacionAduanera numero="1701842" fecha="2017-11-28" aduana="Lazaro Cardenas" /></ventavehiculos:VentaVehiculos></cfdi:ComplementoConcepto></cfdi:Concepto><cfdi:Concepto ClaveProdServ="01010101" NoIdentificacion="560120181" Cantidad="1.00" ClaveUnidad="EA" Unidad="EA" Descripcion="Manejo, procesamiento y entreg" ValorUnitario="6108.07" Importe="6108.07"><cfdi:Impuestos><cfdi:Traslados><cfdi:Traslado Base="6108.07" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="977.29" /></cfdi:Traslados></cfdi:Impuestos></cfdi:Concepto><cfdi:Concepto ClaveProdServ="01010101" NoIdentificacion="5601201823" Cantidad="1.00" ClaveUnidad="EA" Unidad="EA" Descripcion="Otros cargos DTM" ValorUnitario="2320.00" Importe="2320.00"><cfdi:Impuestos><cfdi:Traslados><cfdi:Traslado Base="2320.00" Impuesto="002" TipoFactor="Exento" /></cfdi:Traslados></cfdi:Impuestos><cfdi:ComplementoConcepto><terceros:PorCuentadeTerceros version="1.1" rfc="DTM020315Q47" nombre="Distribuidores Toyota México, A.C."><terceros:Impuestos><terceros:Traslados><terceros:Traslado impuesto="IVA" tasa="0" importe="0.00" /></terceros:Traslados></terceros:Impuestos></terceros:PorCuentadeTerceros></cfdi:ComplementoConcepto></cfdi:Concepto><cfdi:Concepto ClaveProdServ="01010101" NoIdentificacion="5601201824" Cantidad="1.00" ClaveUnidad="EA" Unidad="EA" Descripcion="Otros cargos DTM" ValorUnitario="371.20" Importe="371.20"><cfdi:Impuestos><cfdi:Traslados><cfdi:Traslado Base="371.20" Impuesto="002" TipoFactor="Exento" /></cfdi:Traslados></cfdi:Impuestos><cfdi:ComplementoConcepto><terceros:PorCuentadeTerceros version="1.1" rfc="DCO050303BG1" nombre="DTMAC Comercializadora, S.A. de C.V."><terceros:Impuestos><terceros:Traslados><terceros:Traslado impuesto="IVA" tasa="0" importe="0.00" /></terceros:Traslados></terceros:Impuestos></terceros:PorCuentadeTerceros></cfdi:ComplementoConcepto></cfdi:Concepto><cfdi:Concepto ClaveProdServ="01010101" NoIdentificacion="5601201825" Cantidad="1.00" ClaveUnidad="EA" Unidad="EA" Descripcion="Otros cargos DTM" ValorUnitario="40.00" Importe="40.00"><cfdi:Impuestos><cfdi:Traslados><cfdi:Traslado Base="40.00" Impuesto="002" TipoFactor="Exento" /></cfdi:Traslados></cfdi:Impuestos><cfdi:ComplementoConcepto><terceros:PorCuentadeTerceros version="1.1" rfc="FME930525J6A" nombre="Fundacion Merced, A.C."><terceros:Impuestos><terceros:Traslados><terceros:Traslado impuesto="IVA" tasa="0" importe="0.00" /></terceros:Traslados></terceros:Impuestos></terceros:PorCuentadeTerceros></cfdi:ComplementoConcepto></cfdi:Concepto></cfdi:Conceptos><cfdi:Impuestos TotalImpuestosTrasladados="46372.05"><cfdi:Traslados><cfdi:Traslado Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="46372.05" /></cfdi:Traslados></cfdi:Impuestos><cfdi:Complemento><tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/timbrefiscaldigital/TimbreFiscalDigitalv11.xsd" Version="1.1" UUID="E3A64D3C-1F89-4620-B005-A021B109FB23" FechaTimbrado="2017-12-01T15:27:55" RfcProvCertif="SST060807KU0" SelloCFD="LV1beEcSeRBLjmIrZV6wwQ5TcYQkYeuekAKtAmgRsGD5EezvevH4gv3+93E8Wk9CxBJNVRYLHLlG3uxR7rXawpBTbgPJFCPSt3a0q+Rb3OnBdu8BAdu/lNi4lGojrgzcbthZnPD0u9WoJOiDFI4494wUDR4dF2WJr+8QYYxSfXtMQV065dOrDDtmfJektPIS0hq1JAkknHpThExjX8s0UuV2+MVTciu+DVXoa4G5wp0ldLCWLSzsR5iXulATCEgodrynw5/kvLKqZkI2GAy5oCO2GA0REHFosIlISuNBhD08fcEOwju+NVHKGdjXZ89YpkCXMVxhrL0CQVHZ1dxuNQ==" NoCertificadoSAT="00001000000405179095" SelloSAT="DsTXSzE+CTI+c6cobTFhLee2nsQVC+l0Fnjy2+5OTGTUpTWUj7FjpSw/tgHsiSNijTVKuiGuQFanSj2AmZImM6PXaPe06cEpt11PQ6ABOzR2PZpJ0Eanf3/YwpOu4/fyH3euZS/dtLIfZpFOrwAZlhJ7zQCLS7cv2q5dnLRRz9o1PD0yTv/7ECm3mPE30eWXKjsmg46HckzY+mxDWFWmpmhfQGR/PhimThQBq03+WO78ycresk67q5jM1rvmGzNPnh9UIKAEAH4NSsdrCt0jgkff2vOeYCVmsuhBf/y4L9FXid/vAdGS7xMgaBMEP9e1AnEBW1IppbUCaKMZe2jX4g==" /></cfdi:Complemento><cfdi:Addenda><Toyota:AddendaEmisor><Toyota:Toyota><Toyota:Datos email="[email protected]" distFd="57011" fechaEmbarque="2017-11-28" facturaTms="1701842" claveVehicular="1520202" color="058 ICEBERG/FR13 FR13" usado="usado" numMotor="2TR -9145818" otrosCargos="6108.07" netoPagar="338928.57" /></Toyota:Toyota></Toyota:AddendaEmisor></cfdi:Addenda></cfdi:Comprobante> 
+0

ノードの位置を使用して名前を変更するとはどういう意味ですか? XMLコンテンツは常に単一行ですか?自分の目標を達成するためにどのような努力をしましたか?ネイティブXML機能を持たないスクリプトソリューションを選択する理由は何ですか? – Compo

答えて

0

それだけでコマンドやアプリケーションを実行するために設計されたWindowsコマンドインタプリタでXMLファイルを処理するために非常に悪い考えです。 cmd.exeには、XML解析機能または少なくとも拡張された文字列処理機能はありません。

XMLファイルには、バッチファイル内のXMLファイルから処理ラインが非常に困難にし、Windowsのコマンド・インタプリタのためのすべての特別な意味を持っている<>="がたくさん含まれています。

XMLファイルは、使用されているユーザーアカウントの地域および言語設定に応じて、OEMコードページで文字エンコードごとに1バイトを使用するWindowsコマンドインタープリタによってデフォルトで使用されない文字エンコードであるUTF-8を使用して、 。これは、処理する値に非ASCII文字が含まれていると問題になる可能性があります。この場合、属性Nivの値には常にASCII文字のみが含まれているように見えます。

改行文字のないXMLファイルでは、この場合のファイルサイズは1行の長さであり、コマンドラインの長さと環境の最大長それは

  1. Environment Variables(のMicrosoft Developer Networkの記事)
  2. Command prompt (Cmd. exe) command-line string limitation(マイクロソフトのサポート記事)
  3. What is the maximum length of an environment variable?(レイMSDNのブログの記事で、たとえば読み取ることができるよう変数はほんの数KiBのに限定されていますモンド陳レイモンド・チェン)
  4. What is the command line length limit?(MSDNのブログ記事)

これらの制限は、属性Nivの値に基づいて、タスクの名前を変更し、このファイルを実行するための問題があります。以下のバッチファイルは、バッチファイルJREPL.BAT、Dave Benham)を使用して、コマンドラインの長さ制限を回避します。このバッチファイルは、ZIPファイルからバッチファイルのディレクトリ(または上記のバッチファイルの行番号10を変更している他のディレクトリ)に抽出する必要があります。もちろん、How can you find and replace text in a file using the Windows command-line environment?に記載されている他のソリューションを使用して、改行文字のないXMLファイルの一時コピーに各>の後にCR + LFを挿入することもできます。

これは、他のスクリプト言語ではうまくいきましたが、この名前変更タスクのコメント付きバッチファイルです。

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 
set "MessageOutput=" 

rem Define name of replace tool jrepl.bat with full path. It is expected by 
rem the code below that this batch file is stored in same directory as the 
rem batch file. It is of course also possible to have this tool stored in 
rem a different directory in which case %~dp0 must be replaced by path of 
rem this directory. 
set "ReplaceTool=%~dp0jrepl.bat" 

rem Change the current directory to the directory specified below. 
pushd "%USERPROFILE%\Desktop\sistemas" 

rem Get and process a static list of XML files. 
for /F "delims=" %%I in ('dir *.xml /A-D /B 2^>nul') do call :ProcessXmlFile "%%I" 

rem JREPL.BAT v7.9 leaves behind JREPL\XBYTES.HEX in folder for temporary files. 
rd /Q /S "%TEMP%\JREPL" 2>nul 

rem Restore initial current directory. 
popd 

rem Pause script execution if an error message was output because a file 
rem could not be renamed because of an already existing file with same name. 
if defined MessageOutput echo/ & pause 

rem Restore initial environment which means also discarding all environment 
rem variables defined in this batch file and restore the initial values of 
rem all other environment variables. 
endlocal 

rem Exit processing of this batch file. 
goto :EOF 


rem Subroutine ProcessXmlFile reads first line containing case-sensitive 
rem attribute Niv with a value having at least one character in double 
rem quotes. Nothing is done if the XML file of which name is passed as 
rem first argument to the subroutine does not contain such an attribute. 

rem Both commands of the FOR loop are not executed if the line output by 
rem FINDSTR is too long for being assigned to environment variable XmlLine. 
rem In this case it can be expected that the XML file to process contains 
rem no newline characters. For that reason a copy of the not pretty formatted 
rem XML file is made with JREPL batch file with file extension TMP instead of 
rem XML with inserting after every right angle bracket the Windows text file 
rem line ending characters carriage return (\r) and line-feed (\n). Then the 
rem temporary file is searched by FINDSTR returning now hopefully a much 
rem shorter line which can be processed by the Windows command interpreter. 
rem The temporary file is deleted finally in any case. 

rem From the XML line containing attribute Niv everything from begin of 
rem the line to Niv and including Niv itself is removed from the line. 
rem So the remaining XML line starts with an equal sign and the value 
rem of attribute Niv enclosed in double quotes. Needed is only the 
rem attribute value inside the double quotes. 

rem After having the attribute value it is first checked if the file 
rem has already the right file name in which case the subroutine can 
rem be exited immediately and processing continues in main FOR loop. 

rem Next is checked if no *.xml file with this value as file name exists 
rem already in current directory. The current XML file is renamed to Niv 
rem attribute value if this precondition is true. Otherwise an information 
rem is output and later batch file execution is halted so that the user 
rem can read this message after finishing processing of all XML files. 

:ProcessXmlFile 
echo %1 
%SystemRoot%\System32\findstr.exe /R " Niv=\"..*\"" %1 >nul 2>&1 
if errorlevel 1 goto :EOF 
set "XmlFile=%~1" 

:GetXmlLine 
for /F "delims=" %%J in ('%SystemRoot%\System32\findstr.exe /R " Niv=\"..*\"" "%XmlFile%"') do (
    set "XmlLine=%%J" 
    goto GetNivValue 
) 

if "%XmlFile:~-3%" == "tmp" del "%XmlFile%" & goto :EOF 
call "%ReplaceTool%" ">" ">\r\n" /XSEQ /F "%XmlFile%" /O "%TEMP%\%~n1.tmp" 
set "XmlFile=%TEMP%\%~n1.tmp" 
goto GetXmlLine 

:GetNivValue 
if "%XmlFile:~-3%" == "tmp" del "%XmlFile%" 
setlocal EnableDelayedExpansion 
set "XmlLine=!XmlLine:* Niv=!" 
for /F "delims=> = " %%J in ("!XmlLine!") do endlocal & set "NivValue=%%~J" 

if /I "%NivValue%.xml" == "%~nx1" goto :EOF 

if not exist "%NivValue%.xml" ren "%~1" "%NivValue%.xml" & goto :EOF 
echo Error: File "%NivValue%.xml" exists already. 
echo  File %1 cannot be renamed for that reason. 
set "MessageOutput=1" 
goto :EOF 

注:ブラウザによって表示さコマンドラインFOR とラベル:GetNivValue下の行87で>=delims=後ではなく4つのスペース間の水平タブ文字がなければなりません。すべてのXMLファイルに空白文字の代わりにタブ文字が含まれていない場合、4行のスペースとして表示されるタブ文字もこの行から完全に削除できます。使用するコマンドを理解し、どのように彼らは、コマンドプロンプトウィンドウを開き、そこに次のコマンドを実行し、完全にページが非常に慎重に、各コマンドのために表示されているすべてのヘルプを読んで、仕事のため

  • call /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?

も読むマイクロソフトarticl約Using Command Redirection Operatorsの説明については、
>nul,2>nulおよび2>&1となる。リダイレクト演算子>はリテラル文字として解釈さコマンドラインのにキャレット文字^でエスケープする必要がある場合に、Windowsのコマンドインタプリタプロセス、このコマンドライン別のコマンドに埋め込まれ
dirコマンドラインを実行
するためのコマンドを実行する前に、プロセスはバックグラウンドで開始されました。

Single line with multiple commands using Windows batch fileオペレータ&は、指定されたバッチファイルのコードの一部のコマンドラインで使用されています。

+0

ありがとう、あなたは素晴らしい天才です。私はこの問題だけを解決することはできませんでした。 –

関連する問題