Compare commits
No commits in common. "c5cc433e2f8b300bc004530180667c0c6c0dceab" and "76ea5b086019f3d5a2745fcd5c3d770318cb24b0" have entirely different histories.
c5cc433e2f
...
76ea5b0860
@ -12,7 +12,6 @@ Be careful! Some of the scripts in here do changes that are irrevertible. Make s
|
|||||||
|
|
||||||
- `mtime2exifcreatedate.sh`: iterates over all files in a directory (every!) and (over-)writes the EXIF `CreateDate` tag to the modification time of the file
|
- `mtime2exifcreatedate.sh`: iterates over all files in a directory (every!) and (over-)writes the EXIF `CreateDate` tag to the modification time of the file
|
||||||
- `modify_mtime.py`: changes the modify date (mtime) of all files of the cwd by a given amount of time
|
- `modify_mtime.py`: changes the modify date (mtime) of all files of the cwd by a given amount of time
|
||||||
- `filename2mtime.py`: extract a date and time from the name of all files in a directory and set their respective UNIX mtime (the modified timestamp).
|
|
||||||
|
|
||||||
|
|
||||||
## LICENSE
|
## LICENSE
|
||||||
|
@ -30,9 +30,8 @@ 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'
|
||||||
@ -44,16 +43,6 @@ 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'
|
||||||
@ -64,62 +53,20 @@ 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('-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('--help', action='store_true', help='display this help message and exit') # on/off flag
|
||||||
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 or not len(sys.argv) > 1:
|
if args.help:
|
||||||
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