from pathlib import Path
from datetime import datetime
import re
import stat
# --------------------------------------------------
# Hardcoded project paths
# --------------------------------------------------
directories = {
"1": {
"name": "LAND",
"path": "/u/udevel/sandbox/subject/scheduler/file_land_dir"
},
"2": {
"name": "LOAD",
"path": "/u/udevel/sandbox/subject/scheduler/file_load_dir"
},
"3": {
"name": "OUTDIR",
"path": "/u/udevel/sandbox/subject/scheduler/file_outbound_dir"
}
}
# --------------------------------------------------
# Find archive directories
# --------------------------------------------------
def find_archives(base_path):
"""
Find valid archive directories.
Archive directory format:
YYYYMMDD
Example:
20260807
"""
path = Path(base_path)
archives = []
for item in path.iterdir():
if item.is_dir():
# Must contain exactly 8 digits
if re.fullmatch(r"\d{8}", item.name):
try:
# Make sure it is a valid date
datetime.strptime(
item.name,
"%Y%m%d"
)
archives.append(item)
except ValueError:
pass
return archives
# --------------------------------------------------
# Get file details
# --------------------------------------------------
def get_file_details(file):
"""
Get file name, size, modified date
and permissions.
"""
info = file.stat()
return {
"name": file.name,
"size": info.st_size,
"modified": datetime.fromtimestamp(
info.st_mtime
).strftime(
"%Y-%m-%d %H:%M:%S"
),
"permissions": stat.filemode(
info.st_mode
)
}
# --------------------------------------------------
# Find files using pattern
# --------------------------------------------------
def find_files(archive_path, pattern):
"""
Find files inside an archive
matching the given pattern.
"""
files = []
for item in archive_path.glob(pattern):
if item.is_file():
files.append(
get_file_details(item)
)
return files
# --------------------------------------------------
# Display file report
# --------------------------------------------------
def print_file_report(files, archive_name):
print("\n")
print("=" * 160)
print(
"Archive:",
archive_name
)
print("=" * 160)
print(
f"{'File Name':<100}"
f"{'Size(Bytes)':>20}"
f"{'Modified Date':>25}"
f"{'Permissions':>15}"
)
print("-" * 160)
for file in files:
print(
f"{file['name']:<100}"
f"{file['size']:>20}"
f"{file['modified']:>25}"
f"{file['permissions']:>15}"
)
print("-" * 160)
print(
"Total matching files:",
len(files)
)
# --------------------------------------------------
# Main program
# --------------------------------------------------
def main():
print("""
=============================
Archive Search Utility
=============================
1. LAND
2. LOAD
3. OUTDIR
""")
# ----------------------------------------------
# Select directory type
# ----------------------------------------------
choice = input(
"Select option: "
).strip()
if choice not in directories:
print("\nInvalid option.")
return
selected = directories[choice]
print(
"\nSelected:",
selected["name"]
)
print(
"Base Path:",
selected["path"]
)
# ----------------------------------------------
# Check base directory
# ----------------------------------------------
base_path = Path(
selected["path"]
)
if not base_path.exists():
print(
"\nBase directory does not exist."
)
return
if not base_path.is_dir():
print(
"\nBase path is not a directory."
)
return
# ----------------------------------------------
# Find archives
# ----------------------------------------------
archives = find_archives(
base_path
)
if not archives:
print(
"\nNo archive directories found."
)
return
print(
"\nArchives found:"
)
for archive in archives:
print(
archive.name
)
# ----------------------------------------------
# Get file pattern
# ----------------------------------------------
pattern = input(
"\nEnter file name or pattern "
"(* for all files): "
).strip()
if not pattern:
print(
"\nFile pattern cannot be empty."
)
return
# ----------------------------------------------
# Search each archive
# ----------------------------------------------
total_files = 0
for archive in archives:
files = find_files(
archive,
pattern
)
if files:
print_file_report(
files,
archive.name
)
total_files += len(files)
# ----------------------------------------------
# Final summary
# ----------------------------------------------
print("\n")
print("=" * 160)
print(
"Total matching files across all archives:",
total_files
)
print("=" * 160)
# --------------------------------------------------
# Program entry point
# --------------------------------------------------
if __name__ == "__main__":
main()8 views