Files
Botexercito/Scripts/Sacarpdf.py

58 lines
1.5 KiB
Python
Raw Normal View History

2026-03-15 10:00:07 +00:00
from __future__ import annotations
from pathlib import Path
import shutil
def copy_pdfs_from_share(
src_root: Path,
dst_root: Path,
recursive: bool = True,
overwrite: bool = False,
keep_structure: bool = True,
) -> int:
src_root = Path(src_root)
dst_root = Path(dst_root)
if not src_root.exists():
raise FileNotFoundError(f"Source path does not exist: {src_root}")
dst_root.mkdir(parents=True, exist_ok=True)
iterator = src_root.rglob("*.pdf") if recursive else src_root.glob("*.pdf")
copied = 0
for pdf_path in iterator:
if not pdf_path.is_file():
continue
if keep_structure:
rel = pdf_path.relative_to(src_root)
target_path = dst_root / rel
target_path.parent.mkdir(parents=True, exist_ok=True)
else:
target_path = dst_root / pdf_path.name
if target_path.exists() and not overwrite:
continue
try:
shutil.copy2(pdf_path, target_path)
copied += 1
except PermissionError as e:
print(f"Permission denied copying: {pdf_path} -> {target_path} ({e})")
return copied
if __name__ == "__main__":
INPUT_PATH = Path(r"\\intranet.exercito.local\areas-sectoriais")
OUTPUT_DIR = r"D:/PDFs"
total = copy_pdfs_from_share(
src_root=INPUT_PATH,
dst_root=OUTPUT_DIR,
recursive=True,
overwrite=False,
keep_structure=True,
)
print(f"Copied {total} PDF(s) to: {OUTPUT_DIR}")