v2
This commit is contained in:
75
parse-archives.py
Normal file
75
parse-archives.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Install these first:
|
||||
# pip install patool py7zr rarfile
|
||||
|
||||
try:
|
||||
from patoolib import extract_archive
|
||||
except ImportError:
|
||||
print("Error: 'patool' is not installed. Run: pip install patool py7zr rarfile")
|
||||
sys.exit(1)
|
||||
|
||||
def extract_archive_to_folder(archive_path: Path, base_dir: Path):
|
||||
"""Extract a single archive to extracted_{name} folder."""
|
||||
# Get archive name without extension
|
||||
name_without_ext = archive_path.stem
|
||||
extract_dir = base_dir / f"bg_mountains_{name_without_ext}"
|
||||
|
||||
# Create the output directory if it doesn't exist
|
||||
extract_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
print(f"Extracting: {archive_path.name} → {extract_dir.name}/")
|
||||
|
||||
try:
|
||||
# patool automatically detects format (zip, rar, 7z, etc.)
|
||||
extract_archive(str(archive_path), outdir=str(extract_dir), verbosity=0)
|
||||
print(f"✓ Successfully extracted: {archive_path.name}\n")
|
||||
except Exception as e:
|
||||
print(f"✗ Failed to extract {archive_path.name}: {e}\n")
|
||||
|
||||
def main(folder_path: str = ".", recursive: bool = False):
|
||||
base_dir = Path(folder_path).resolve()
|
||||
|
||||
if not base_dir.exists():
|
||||
print(f"Error: Folder '{base_dir}' does not exist.")
|
||||
return
|
||||
|
||||
print(f"Scanning for archives in: {base_dir}\n")
|
||||
|
||||
# Supported extensions
|
||||
extensions = {'.zip', '.rar', '.7z'}
|
||||
|
||||
# Find all matching archives
|
||||
if recursive:
|
||||
archive_files = [p for p in base_dir.rglob("*") if p.is_file() and p.suffix.lower() in extensions]
|
||||
else:
|
||||
archive_files = [p for p in base_dir.iterdir() if p.is_file() and p.suffix.lower() in extensions]
|
||||
|
||||
if not archive_files:
|
||||
print("No .zip, .rar, or .7z files found.")
|
||||
return
|
||||
|
||||
print(f"Found {len(archive_files)} archive(s).\n")
|
||||
|
||||
for archive in sorted(archive_files):
|
||||
extract_archive_to_folder(archive, base_dir)
|
||||
|
||||
print("Extraction process completed!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Usage examples:
|
||||
# python extract_archives.py # current folder, non-recursive
|
||||
# python extract_archives.py "/path/to/folder" # specific folder
|
||||
# python extract_archives.py "/path/to/folder" --recursive
|
||||
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="Extract .zip/.rar/.7z archives into separate folders.")
|
||||
parser.add_argument("folder", nargs="?", default=".", help="Folder to scan (default: current directory)")
|
||||
parser.add_argument("-r", "--recursive", action="store_true", help="Search subfolders recursively")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
main(args.folder, args.recursive)
|
||||
Reference in New Issue
Block a user