Added new script
This commit is contained in:
parent
76ea5b0860
commit
e716611863
@ -30,8 +30,9 @@ Useful links:
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from argparse import ArgumentParser, RawTextHelpFormatter
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
# define some shell color codes
|
|
||||||
class COLORS:
|
class COLORS:
|
||||||
HEADER = '\033[95m'
|
HEADER = '\033[95m'
|
||||||
OKBLUE = '\033[94m'
|
OKBLUE = '\033[94m'
|
||||||
@ -43,6 +44,16 @@ class COLORS:
|
|||||||
BOLD = '\033[1m'
|
BOLD = '\033[1m'
|
||||||
UNDERLINE = '\033[4m'
|
UNDERLINE = '\033[4m'
|
||||||
|
|
||||||
|
class LOGGER:
|
||||||
|
def __init__(self, verbose: bool = False):
|
||||||
|
self.verbose = verbose
|
||||||
|
|
||||||
|
def log(self, to_log, verbose=False, end="\n"):
|
||||||
|
to_log = to_log + COLORS.ENDC
|
||||||
|
if (self.verbose and verbose) or not verbose: # only print if it's not verbose or verbose is enabled
|
||||||
|
print(to_log, end=end)
|
||||||
|
|
||||||
|
|
||||||
# set up the ArgumentParser for the CLI
|
# set up the ArgumentParser for the CLI
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
description='filename2mtime.py Copyright (C) 2024 Benjamin Burkhardt\r\n'
|
description='filename2mtime.py Copyright (C) 2024 Benjamin Burkhardt\r\n'
|
||||||
@ -53,20 +64,62 @@ parser = ArgumentParser(
|
|||||||
'\r\n'
|
'\r\n'
|
||||||
'This helper script is able to extract a date and time from the name of all \r\n'
|
'This helper script is able to extract a date and time from the name of all \r\n'
|
||||||
'files in a directory and set their respective UNIX mtime (the modified timestamp).\r\n'
|
'files in a directory and set their respective UNIX mtime (the modified timestamp).\r\n'
|
||||||
'BE AWARE THAT ALL CHANGES TO THE FILES CANNOT BE UNDONE! USE AT YOUR OWN RISK!',
|
'BE AWARE THAT ALL CHANGES TO THE FILES CANNOT BE UNDONE! USE AT YOUR OWN RISK!',
|
||||||
epilog='by Benjamin Burkhardt, 2024',
|
epilog='by Benjamin Burkhardt, 2024',
|
||||||
add_help=False,
|
add_help=False,
|
||||||
formatter_class=RawTextHelpFormatter)
|
formatter_class=RawTextHelpFormatter)
|
||||||
|
|
||||||
parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose output') # on/off flag
|
|
||||||
parser.add_argument('--help', action='store_true', help='display this help message and exit') # on/off flag
|
parser.add_argument('-f', '--format', type=str, default="", help="give the format of the strings\r\nsee https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior for more details")
|
||||||
|
parser.add_argument('-e', '--format-error-exit', action='store_true', default=False, help='exit if the format does not fit to one file') # on/off flag
|
||||||
|
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='enable verbose output') # on/off flag
|
||||||
|
parser.add_argument('-h', '--help', action='store_true', default=False, help='display this help message and exit') # on/off flag
|
||||||
|
|
||||||
|
|
||||||
# parse given arguments
|
# parse given arguments
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# print help and exit if prompted to
|
# print help and exit if prompted to
|
||||||
if args.help:
|
if args.help or not len(sys.argv) > 1:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
# set up logger
|
||||||
|
logger = LOGGER(args.verbose)
|
||||||
|
|
||||||
|
# check for an empty format
|
||||||
|
if args.format == '' and not args.auto_format:
|
||||||
|
logger.log(COLORS.FAIL + 'You need to specify a format.')
|
||||||
|
parser.print_usage()
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Start of the real program, format is surely set
|
||||||
|
|
||||||
|
all_files = os.listdir()
|
||||||
|
parsed_datetimes = {}
|
||||||
|
longest_filename_len = max([len(f) for f in all_files])
|
||||||
|
|
||||||
|
|
||||||
|
for f in all_files:
|
||||||
|
try:
|
||||||
|
parsed_datetimes[f] = datetime.strptime(f, args.format)
|
||||||
|
logger.log(f"Detecting: {f} -> {parsed_datetimes[f]}", verbose=True)
|
||||||
|
except ValueError:
|
||||||
|
if args.format_error_exit:
|
||||||
|
logger.log(f"{COLORS.WARNING}{f.ljust(longest_filename_len + 1)}: Failed to parse the date and time.")
|
||||||
|
logger.log(f"{COLORS.FAIL}EXITING (as -e flag was set)!")
|
||||||
|
exit(1)
|
||||||
|
else:
|
||||||
|
logger.log(f"{f.ljust(longest_filename_len + 1)}: {COLORS.WARNING}Failed to parse the date and time.")
|
||||||
|
|
||||||
|
for f, dt in parsed_datetimes.items():
|
||||||
|
# get atime timestamp (as os.utime requires this as an argument - we leave it unchanged)
|
||||||
|
atime_timestamp = os.stat(f).st_atime
|
||||||
|
|
||||||
|
# get the mtime timestamp from the respective datetime object
|
||||||
|
mtime_timestamp = dt.timestamp()
|
||||||
|
|
||||||
|
# set the new mtime (atime stays unchanged)
|
||||||
|
os.utime(f, (atime_timestamp, mtime_timestamp))
|
||||||
|
Loading…
Reference in New Issue
Block a user