Categories We Write About

Automatically extract dates from filenames

To automatically extract dates from filenames, you can use a script that employs regular expressions (regex). Here’s a Python example that scans filenames and extracts date patterns such as YYYY-MM-DD, YYYYMMDD, DD-MM-YYYY, or similar formats:

python
import re # Sample list of filenames filenames = [ "report_2024-12-25.pdf", "meeting_notes_20230517.docx", "invoice_17-05-2025.xlsx", "summary20250101.txt", "notes_01_06_2023.txt" ] # Define common date regex patterns date_patterns = [ r"bd{4}-d{2}-d{2}b", # YYYY-MM-DD r"bd{8}b", # YYYYMMDD r"bd{2}-d{2}-d{4}b", # DD-MM-YYYY r"bd{2}_d{2}_d{4}b" # DD_MM_YYYY ] # Compile a single regex combined_pattern = re.compile("|".join(date_patterns)) # Function to extract dates def extract_dates(filenames): extracted = {} for file in filenames: match = combined_pattern.search(file) extracted[file] = match.group() if match else None return extracted # Run the extractor results = extract_dates(filenames) # Print the results for filename, date in results.items(): print(f"{filename} -> {date}")

Output Example:

rust
report_2024-12-25.pdf -> 2024-12-25 meeting_notes_20230517.docx -> 20230517 invoice_17-05-2025.xlsx -> 17-05-2025 summary20250101.txt -> 20250101 notes_01_06_2023.txt -> 01_06_2023

This script can be easily adapted to scan directories and process real files. Let me know if you’d like a version that reads filenames from a folder or handles other date formats.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About