Skip to content

Instantly share code, notes, and snippets.

@LeXuanKhanh
Created July 26, 2022 03:38
Show Gist options
  • Save LeXuanKhanh/fb8ca27fa36f728929a2002807ecf295 to your computer and use it in GitHub Desktop.
Save LeXuanKhanh/fb8ca27fa36f728929a2002807ecf295 to your computer and use it in GitHub Desktop.
Rename files script base on modified or created time, place it in folder which contain your files you want to rename
import pathlib
import glob
import os
import time
import pprint
import functools
import uuid
import enum
class NameSortType(enum.Enum):
IndexCreatedTime = 1
IndexModifiedTime = 2
# Change this if you want to change name and sort base on created time of modified time
nameSortType= NameSortType.IndexCreatedTime
# File extension which you want to change and sort
fileExtension = 'mp4'
# Time seperator ':' will change to this symbol (can't rename file with symbol ';') (Ex: 08:00:00 -> 08,00,00)
timeSeperator = ","
class VideoFile:
filePath: str = ''
createdTime: float = 0
modifiedTime: float = 0
def __init__(self, filePath):
self.filePath = filePath
self.createdTime = os.path.getctime(self.filePath)
self.modifiedTime = os.path.getmtime(self.filePath)
def newPathTempWith(self, absolutePath: str, index: int):
return f"{absolutePath}/{index}_{self.createdTimeStrFormatted}_tmp.{self.fileExtension}"
def newPathWith(self, absolutePath: str, index: int):
return f"{absolutePath}/{index}_{self.createdTimeStrFormatted}.{self.fileExtension}"
@property
def createdTimeStrFormatted(self):
return time.ctime(self.getTime).replace(" ", "_").replace(":",timeSeperator)
@property
def getTime(self):
if nameSortType == NameSortType.IndexCreatedTime:
return self.createdTime
if nameSortType == NameSortType.IndexModifiedTime:
return self.modifiedTime
@property
def description(self, prettyPrint: bool = True):
desc = vars(self)
if prettyPrint:
return pprint.pformat(desc)
return vars(self)
# get last element (ex: ex.mp4 -> ['ex', 'mp4'])
@property
def fileExtension(self):
return self.filePath.split(".")[-1]
def setTempName(self, absolutePath: str, index: int):
newPath = self.newPathTempWith(absolutePath, index)
self.renameFile(newPath)
def setNewName(self, absolutePath: str, index: int):
newPath = self.newPathWith(absolutePath, index)
self.renameFile(newPath)
def setRandomName(self, absolutePath: str):
newName = uuid.uuid1()
newPath = f"{absolutePath}/{newName}.{self.fileExtension}"
self.renameFile(newPath)
def renameFile(self, newPath):
os.rename(self.filePath, newPath)
self.filePath = newPath
# ========= MAIN =========
def main():
currentPath = pathlib.Path(__file__).parent.resolve()
videoPaths = glob.glob(f"{currentPath}/*.{fileExtension}")
print("executing script")
# print(currentPath)
# print(videoPaths)
videoFiles: list[VideoFile] = []
for index, videoPath in enumerate(videoPaths):
videoFile = VideoFile(videoPath)
videoFiles.append(videoFile)
videoFiles.sort(key = functools.cmp_to_key(lambda first, second: first.createdTime - second.createdTime))
# for index, video in enumerate(videoFiles):
# print(video.description)
for index, video in enumerate(videoFiles):
video.setRandomName(currentPath)
for index, video in enumerate(videoFiles):
video.setNewName(currentPath, index)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment