Last active
November 30, 2022 12:04
-
-
Save yzhong52/56b6110afbba5a630c39 to your computer and use it in GitHub Desktop.
Rename files based on their created 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
Dropbox Style Filename |
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
import os, datetime | |
# Working directory | |
directory = './' | |
# extensions for files that we want rename | |
extensions = (['.jpg', '.jpeg']); | |
# Get a list of files in the directory | |
filelist = os.listdir( directory ) | |
# New file dictionary | |
newfilesDictionary = {} | |
# count the number of files that are renamed | |
count = 0 | |
for file in filelist: | |
# split the file into filename and extension | |
filename, extension = os.path.splitext(file) | |
# if the extension is a valid extension | |
if ( extension in extensions ): | |
# Get the create time of the file | |
create_time = os.path.getctime( file ) | |
# get the readable timestamp format | |
format_time = datetime.datetime.fromtimestamp( create_time ) | |
# convert time into string | |
format_time_string = format_time.strftime("%Y-%m-%d %H.%M.%S") # e.g. 2015-01-01 09.00.00.jpg | |
# Contruct the new name of the file | |
newfile = format_time_string + extension; | |
# If there is other files created at the same timestamp | |
if ( newfile in newfilesDictionary.keys() ): | |
index = newfilesDictionary[newfile] + 1; | |
newfilesDictionary[newfile] = index; | |
newfile = format_time_string + '-' + str(index) + extension; # e.g. 2015-01-01 09.00.00-1.jpg | |
else: | |
newfilesDictionary[newfile] = 0; | |
# rename the file | |
os.rename( file, newfile ); | |
# count the number of files that are renamed | |
count = count + 1 | |
# printing log | |
print( file.rjust(35) + ' => ' + newfile.ljust(35) ) | |
print( 'All done. ' + str(count) + ' files are renamed. ') |
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
# Change all pictures filenames from Google Nexus 5 to dropbox style in a directory | |
# An example of source file name: | |
# IMG_20141224_204028 (1).jpg | |
# Destination file name: | |
# 2014-12-24 20.40.28 (1).jpg | |
# Developed in Python 3 | |
import os, datetime | |
import re # For regular expression | |
# Working directory | |
directory = './' | |
# Get a list of files in the directory | |
filelist = os.listdir( directory ) | |
# count the number of files that are renamed | |
count = 0 | |
# regular string pattern | |
pattern = re.compile('IMG_\d{8,8}_\d{6,6}[ (0-9)]*.jpg') | |
for file in filelist: | |
newFile = 'Not Renamed' | |
# if file name matches the defined pattern | |
if ( pattern.match( file ) ): | |
str_year = file[4:8] | |
str_month = file[8:10] | |
str_day = file[10:12] | |
str_hour = file[13:15] | |
str_minute = file[15:17] | |
str_second = file[17:19] | |
# Contruct the new name | |
newFile = str_year + '-' + str_month + '-' + str_day + ' '; | |
newFile += str_hour + '.' + str_minute + '.' + str_second; | |
newFile += file[19:] | |
# rename the file | |
os.rename( file, newFile ); | |
count = count + 1 | |
# printing log | |
print( file.rjust(35) + ' => ' + newFile ) | |
print( 'All done. ' + str(count) + ' files are renamed. ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to modify this and I just lost my files 😭