Created
August 20, 2019 17:17
-
-
Save RedBlaze42/ae00a86b7f806c34fe6390c9e056de61 to your computer and use it in GitHub Desktop.
Rename by creation date
This file contains 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
#!/usr/bin/env python | |
from stat import S_ISREG, ST_CTIME, ST_MODE | |
import os, sys, time | |
def digit(input,nb): | |
output=str(input) | |
while(len(output)<nb): | |
output="0"+output | |
return output | |
# path to the directory (relative or absolute) | |
dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.' | |
# get all entries in the directory w/ stats | |
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath)) | |
entries = ((os.stat(path), path) for path in entries) | |
# leave only regular files, insert creation date | |
entries = ((stat[ST_CTIME], path) | |
for stat, path in entries if S_ISREG(stat[ST_MODE])) | |
#NOTE: on Windows `ST_CTIME` is a creation date | |
# but on Unix it could be something else | |
#NOTE: use `ST_MTIME` to sort by a modification date | |
i=0 | |
for cdate, path in sorted(entries): | |
new_name="["+digit(i,3)+"] "+os.path.basename(path) | |
print(new_name) | |
os.rename(os.path.abspath(path),new_name) | |
i+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment