O seguinte programa em Python executará essas renomeações.
No início do programa, na seção Configuração, você precisará especificar o padrão com o ano (especialmente quando ele muda de "2017" para 2018 ") .
Se você precisar alterar as posições do ano / mês / dia no nome do arquivo, esses números podem ser alterados na função check_and_update
. (As posições do índice para a string "_20171202" podem ser acessadas usando foundAt.start()
e foundAt.end()
) .
#!/usr/bin/env python
import os
import sys
import traceback
import re
# ----------------------------------------------------------------------
# Configuration
# The date string that will be prefixed at the beginning of the file
year_pattern = "_2017[\d]{2}[\d]{2}"
# Only modify files that match this pattern
file_patterns = ["(.*)\.jpg$"]
# Directory to search
start_dirs = ["./"]
# Recursive, check sub-directories
recursive = False
# View the list of modified files, without making any changes
view_modifications_only = True
# ----------------------------------------------------------------------
def add_ending_slash(s):
suffix = "/"
if s.endswith(suffix):
return s
return s + suffix
# ----------------------------------------------------------------------
def pattern_matches_any(filename, file_patterns):
progs = []
for file_pattern in file_patterns:
progs.append(re.compile(file_pattern))
for prog in progs:
if prog.match(filename):
return True
return False
# ----------------------------------------------------------------------
def check_all_dirs(start_dirs):
total_files = 0
try:
for start_dir in start_dirs:
total_files += check_this_dir(start_dir)
except Exception as e:
print('*** Caught exception: %s: %s' % (e.__class__, e))
traceback.print_exc()
return total_files
# ----------------------------------------------------------------------
def check_this_dir(start_dir):
total_files = 0
try:
for filename in os.listdir(start_dir):
filepath = os.path.abspath(add_ending_slash(start_dir) + filename)
# Files
if os.path.isfile(filepath):
total_files += check_and_update(start_dir, filename)
# Directories
elif recursive and os.path.isdir(filepath):
total_files += check_this_dir(filepath)
except PermissionError as e:
print('*** Caught exception: %s: %s' % (e.__class__, e))
pass
return total_files
# ----------------------------------------------------------------------
def check_and_update(start_dir, filename):
if pattern_matches_any(filename, file_patterns):
filepath = os.path.abspath(add_ending_slash(start_dir) + filename)
if (view_modifications_only):
print("[MATCH] " + filepath)
else:
foundAt = re.search(year_pattern, filename)
if (foundAt):
year = filename[foundAt.end()-8:foundAt.end()-4]
month = filename[foundAt.end()-4:foundAt.end()-2]
day = filename[foundAt.end()-2:foundAt.end()]
filename_new = year + " " + month + "-" + day + " " + filename[0:foundAt.start()] + filename[foundAt.end():]
dest = os.path.abspath(add_ending_slash(start_dir) + filename_new)
print("[UPDATE] " + filepath)
if (not os.path.isfile(dest)):
os.rename(filepath, dest)
return 1
return 0
# ----------------------------------------------------------------------
# Main program body
if (view_modifications_only):
print("[INFO] This program is being run in \"read-only\" mode.")
print("[START] The following files will be renamed:")
total_files = check_all_dirs(start_dirs)
print("[END] File count: " + str(total_files))
print()
input("Press Enter to continue...")