import os
import datetime
import re
def get_newest_in_current_day(path):
current_day_files = []
current_day_dirs = []
current_day = datetime.datetime.now().day
pattern = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z(\.pbm\.json)?$")
for name in os.listdir(path):
# Skip names that do not match the pattern
if not pattern.match(name):
continue
full_path = os.path.join(path, name)
timestamp_str = name.split('T')[0] # Extract the date part from the name
timestamp = datetime.datetime.strptime(timestamp_str, "%Y-%m-%d")
# Check if it's the current day
if timestamp.day == current_day:
if os.path.isfile(full_path):
current_day_files.append((timestamp, name))
elif os.path.isdir(full_path):
current_day_dirs.append((timestamp, name))
# Sort by timestamp (newest first) and get the first name
current_day_files.sort(reverse=True)
current_day_dirs.sort(reverse=True)
newest_file = current_day_files[0][1] if current_day_files else None
newest_dir = current_day_dirs[0][1] if current_day_dirs else None
return newest_file, newest_dir
path = "/mnt/backups/0300/rs"
newest_file, newest_dir = get_newest_in_current_day(path)
if newest_file:
print(f"Newest file created today: {newest_file}")
else:
print("No file created today found.")
if newest_dir:
print(f"Newest directory created today: {newest_dir}")
else:
print("No directory created today found.")