feat: Enhance directory processing to preserve full folder structure

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 10:12:08 -08:00
parent 9b9322ddec
commit 6a580785a8

View File

@ -2,6 +2,7 @@ import streamlit as st
import os import os
import logging import logging
import warnings import warnings
import shutil
from io import BytesIO from io import BytesIO
import tempfile import tempfile
import zipfile import zipfile
@ -266,20 +267,31 @@ elif input_method == "Select Directory" and source_dir and dest_dir and st.butto
os.makedirs(dest_dir, exist_ok=True) os.makedirs(dest_dir, exist_ok=True)
# Get all files recursively # Get all files recursively
if file_type == "Excel": all_files = []
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True) files_to_process = []
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
else: # Word
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
if not files: for root, _, files in os.walk(source_dir):
st.warning(f"No {file_type} files found in the source directory") for file in files:
full_path = os.path.join(root, file)
file_lower = file.lower()
if file_type == "Excel" and file_lower.endswith(('.xlsx', '.xlsm')):
files_to_process.append(full_path)
elif file_type == "Word" and file_lower.endswith(('.docx', '.docm')):
files_to_process.append(full_path)
else: else:
all_files.append(full_path)
if not files_to_process:
st.warning(f"No {file_type} files found in the source directory")
total_files = len(files_to_process) + len(all_files)
progress_bar = st.progress(0) progress_bar = st.progress(0)
status_text = st.empty() status_text = st.empty()
files_processed = 0
for idx, source_path in enumerate(files): # Process Office files
for source_path in files_to_process:
try: try:
relative_path = os.path.relpath(source_path, source_dir) relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path) dest_path = os.path.join(dest_dir, relative_path)
@ -295,14 +307,39 @@ elif input_method == "Select Directory" and source_dir and dest_dir and st.butto
st.success("✅ Processing complete!") st.success("✅ Processing complete!")
progress = (idx + 1) / len(files) files_processed += 1
progress = files_processed / total_files
progress_bar.progress(progress) progress_bar.progress(progress)
status_text.text(f"Processed {idx + 1} of {len(files)} files") status_text.text(f"Processed {files_processed} of {total_files} files")
except Exception as e: except Exception as e:
st.error(f"❌ Error processing {relative_path}: {str(e)}") st.error(f"❌ Error processing {relative_path}: {str(e)}")
# Copy all other files
with st.expander("Copying other files", expanded=True):
for source_path in all_files:
try:
relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path)
# Create destination directory if it doesn't exist
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
# Copy the file
import shutil
shutil.copy2(source_path, dest_path)
files_processed += 1
progress = files_processed / total_files
progress_bar.progress(progress)
status_text.text(f"Processed {files_processed} of {total_files} files")
except Exception as e:
st.error(f"❌ Error copying {relative_path}: {str(e)}")
st.success(f"✨ All files processed and saved to: {dest_dir}") st.success(f"✨ All files processed and saved to: {dest_dir}")
if len(all_files) > 0:
st.info(f"📁 Copied {len(all_files)} additional files to maintain folder structure")
# Footer # Footer
st.sidebar.markdown("---") st.sidebar.markdown("---")