73 lines
2.7 KiB
Python
Executable File
73 lines
2.7 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
"""
|
|
This helper script is able to extract a date and time from the name of all files in a directory and set their respective UNIX mtime (the modified timestamp).
|
|
Copyright (C) 2024 Benjamin Burkhardt
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
"""
|
|
Useful links:
|
|
- https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
|
|
- https://www.datacamp.com/tutorial/converting-strings-datetime-objects
|
|
- https://scrapfly.io/blog/parsing-datetime-strings-with-python-and-dateparser/
|
|
- https://stackoverflow.com/questions/466345/convert-string-jun-1-2005-133pm-into-datetime
|
|
"""
|
|
|
|
|
|
from datetime import datetime
|
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
|
import os
|
|
|
|
# define some shell color codes
|
|
class COLORS:
|
|
HEADER = '\033[95m'
|
|
OKBLUE = '\033[94m'
|
|
OKCYAN = '\033[96m'
|
|
OKGREEN = '\033[92m'
|
|
WARNING = '\033[93m'
|
|
FAIL = '\033[91m'
|
|
ENDC = '\033[0m'
|
|
BOLD = '\033[1m'
|
|
UNDERLINE = '\033[4m'
|
|
|
|
# set up the ArgumentParser for the CLI
|
|
parser = ArgumentParser(
|
|
description='filename2mtime.py Copyright (C) 2024 Benjamin Burkhardt\r\n'
|
|
'This program comes with ABSOLUTELY NO WARRANTY and is licensed under the '
|
|
'GNU General Public License 3.\r\n'
|
|
'You should have received a copy of the GNU General Public License\r\n'
|
|
'along with this program. If not, see <https://www.gnu.org/licenses/>.\r\n'
|
|
'\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'
|
|
'BE AWARE THAT ALL CHANGES TO THE FILES CANNOT BE UNDONE! USE AT YOUR OWN RISK!',
|
|
epilog='by Benjamin Burkhardt, 2024',
|
|
add_help=False,
|
|
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
|
|
|
|
|
|
# parse given arguments
|
|
args = parser.parse_args()
|
|
|
|
# print help and exit if prompted to
|
|
if args.help:
|
|
parser.print_help()
|
|
exit(0)
|
|
|