Duplicate orders in Excel from XML

Post Reply
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Duplicate orders in Excel from XML

Post by FlorianDSK »

Hello,

I need help with a batch file in Switch.
Following, the batch file accesses a folder, reads all the XML files in there and combines them into one XML.
So far everything works and the XML looks fine.
However, this file is then opened in Excel and suddenly every single node row is doubled and the jobs are duplicated in the Excel table.

What could be the reason for this?


Code: Select all

@echo off
setlocal enabledelayedexpansion

REM Abrufen des aktuellen Datums im Format "MM-JJJJ"
for /f "tokens=2 delims=. " %%a in ('echo %date%') do (
    set "currentMonth=%%a"
)

for /f "tokens=3 delims=. " %%b in ('echo %date%') do (
    set "currentYear=%%b"
)

set "outputFile=Monatsabschluss_Jobs_%currentMonth%-%currentYear%.xml"
set "inputFolder=C:\Switch\MOVE_TO\Monatsabschluss\Monatsabschluss_Jobs_XML_IN"
set "outputFolder=C:\Switch\MOVE_TO\Monatsabschluss\Monatsabschluss_Jobs_XML_OUT"

REM Erstellen des Unterordnerpfads basierend auf aktuellem Monat und Jahr
set "subfolder=%currentMonth%-%currentYear%"

REM Erzeugen des XML-Headers und öffnenden Tags
(
    echo ^<?xml version="1.0" encoding="UTF-8"?^>
    echo ^<jobs^>
) > "%outputFolder%\%outputFile%"

REM Verzeichnisprüfung und Schleife zur Verarbeitung von XML-Dateien
for %%f in ("%inputFolder%\%subfolder%\*.xml") do (
    for /f "delims=" %%j in ('powershell -Command "$content = Get-Content '%%~f' -Encoding UTF8; $root = [regex]::Matches($content, '<order>.*?</order>', [System.Text.RegularExpressions.RegexOptions]::Singleline); foreach ($order in $root) { Write-Output ('	<job>' + $order.Value + '</job>') }"') do (
        set "line=%%j"
        echo !line! >> "%outputFolder%\%outputFile%"
    )
)
REM Schließen des XML-Tags
echo ^</jobs^> >> "%outputFolder%\%outputFile%"

endlocal
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Duplicate orders in Excel from XML

Post by magnussandstrom »

I don't know why you are getting duplicates. But it's easy to remove duplicates from an Excel-file in Switch with Easy Data Transform and Execute command.

A similar issue is discussed here: viewtopic.php?p=15023&hilit=Easy+Data+Transform#p15023
Post Reply