xml ordering
Anonymous
plain_text
03/11/2025 10:38 AM
2.0 KB
12
Indexable
#pip install lxml
import os
import shutil
from lxml import etree
# Specify the source and destination directories
source_dir = '//pesftp.file.core.windows.net/risftp/Datasync/BRP/Berichtenverkeer/XML/Test bestanden/test - verkeerde filter berichten'
destination_base_dir = '//pesftp.file.core.windows.net/risftp/Datasync/BRP/Berichtenverkeer/XML/Test bestanden/test resultaat'
def move_file_based_on_processtypeid(file_path):
try:
tree = etree.parse(file_path)
root = tree.getroot()
# Search for the 'processtypeid' element
processtypeid = root.xpath('//*[local-name()="ProcessTypeID"]/text()')
if processtypeid:
processtypeid_value = processtypeid[0]
destination_dir = os.path.join(destination_base_dir, processtypeid_value)
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
shutil.move(file_path, destination_dir)
print(f'Moved {file_path} to {destination_dir}')
else:
print(f'No "ProcessTypeID" found in {file_path}')
except etree.XMLSyntaxError:
print(f'Failed to parse {file_path}')
def process_directory(directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path) and file_path.endswith('.xml'):
move_file_based_on_processtypeid(file_path)
# Process the source directory
process_directory(source_dir)
Editor is loading...
Leave a Comment