Last active
January 16, 2021 12:09
-
-
Save dtrugman/037b1e8954b48cbc2f983d90473cae00 to your computer and use it in GitHub Desktop.
Set all files names inside a directory according to their creation date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Copyright (c) 2021 Daniel Trugman | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
''' | |
from os import path, rename | |
from glob import glob | |
from datetime import datetime | |
from argparse import ArgumentParser | |
from enum import Enum | |
class Timestamp(Enum): | |
modify = 'modify' | |
change = 'change' | |
access = 'access' | |
def enumerate_files(directory): | |
return glob(path.join(directory, '*')) | |
def get_file_timestamp(filename, timestamp): | |
getters = { | |
Timestamp.modify.value: path.getmtime, | |
Timestamp.change.value: path.getctime, | |
Timestamp.access.value: path.getatime | |
} | |
return getters[timestamp](filename) | |
def get_file_extension(filename): | |
return path.splitext(filename)[1] | |
def get_file_target(directory, filename, fmt, timestamp): | |
ext = get_file_extension(filename) | |
time = get_file_timestamp(filename, timestamp) | |
time_string = datetime.fromtimestamp(time).strftime(fmt) | |
basename = time_string + ext | |
return path.join(directory, basename) | |
def main(directory, commit, fmt, timestamp): | |
target_files = {} | |
files = enumerate_files(directory) | |
for filename in files: | |
target_filename = get_file_target(directory, filename, fmt, timestamp) | |
if filename == target_filename: | |
continue | |
print('Renaming [{}] -> [{}]'.format(filename, target_filename)) | |
if path.exists(target_filename): | |
print('==> COLLISION! File [{}] already exists, skipping'.format(target_filename)) | |
continue | |
if target_filename in target_files: | |
print('==> COLLISION! During commit will collide with [{}]'.format(target_files[target_filename])) | |
continue | |
target_files[target_filename] = filename | |
if commit: | |
rename(filename, target_filename) | |
if __name__ == '__main__': | |
parser = ArgumentParser() | |
parser.add_argument("--directory", default='.', | |
help="Where should we rename the files (default: current directory)") | |
parser.add_argument("--commit", action="store_true", | |
help="Actually commit changes to disk") | |
parser.add_argument("--format", default="%Y%m%d_%H%M%S", | |
help="Output filename format (default: %(default)s)") | |
parser.add_argument("--timestamp", choices=Timestamp.__members__, default=Timestamp.modify.value, | |
help="The file timestamp to use (default: %(default)s)") | |
args = parser.parse_args() | |
main(args.directory, args.commit, args.format, args.timestamp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTICE: By default, running the script only performs a dry-run to avoid data loss. After running a dry-run and making sure you are satisfied with the results, add the '--commit' argument to commit the changes to disk.