ts
A tiny little python script to rename media files (images, audio or video files) into a formatted timestamp, which, by default, YYYYMMDDGMTHHMMSS.ext or, when -r is passed to the script, ARY·MM·DD-GMT-HH·MM·SS.ext, based on the file’s modification or creation time, whichever is earlier; with a REG file for shell integration on Windows.
The python script and the universal batch / bash wrappers shall be put into %PATH%, where in my case, C:\_c\ on Windows or /usr/local/bin on Linux. On Linux, those scripts shall also be marked as ‘executable’. And of course, on Windows, python must be installed.
The REG file only injects the renaming of media files into default timestamp format to all sorts of media file types; use it in the command line with the -r switch if the AR calendar is desired.
The python script
import os
import sys
import datetime
X = (
'.3gp', '.3GP', '.aac', '.AAC', '.aif', '.AIF', '.aifc', '.AIFC', '.aiff', '.AIFF',
'.alac', '.ALAC', '.amr', '.AMR', '.ape', '.APE', '.arw', '.ARW', '.avi', '.AVI',
'.bmp', '.BMP', '.cr2', '.CR2', '.f4v', '.F4V', '.flac', '.FLAC', '.flv', '.FLV',
'.gif', '.GIF', '.heic', '.HEIC', '.heif', '.HEIF', '.jpeg', '.JPEG', '.jpg', '.JPG',
'.m4a', '.M4A', '.m4b', '.M4B', '.m4v', '.M4V', '.mid', '.MID', '.midi', '.MIDI',
'.mkv', '.MKV', '.mov', '.MOV', '.mp3', '.MP3', '.mp4', '.MP4', '.mpeg', '.MPEG',
'.mpg', '.MPG', '.nef', '.NEF', '.oga', '.OGA', '.ogg', '.OGG', '.opus', '.OPUS',
'.orf', '.ORF', '.pef', '.PEF', '.png', '.PNG', '.raf', '.RAF', '.raw', '.RAW',
'.rm', '.RM', '.rmvb', '.RMVB', '.rw2', '.RW2', '.srw', '.SRW', '.svg', '.SVG',
'.tif', '.TIF', '.tiff', '.TIFF', '.wav', '.WAV', '.webm', '.WEBM', '.webp', '.WEBP',
'.wma', '.WMA', '.wmv', '.WMV',
)
T = '%Y%m%dGMT%H%M%S'
R = '{ar:03}·%m·%d-GMT-%H·%M·%S'
def tf(ts, r):
dt = datetime.datetime.fromtimestamp(ts).astimezone(datetime.timezone.utc)
if r:
ar = dt.year - 1911
return dt.strftime(R).format(ar=ar)
else:
return dt.strftime(T)
def en(x):
x = x.lower()
if x == '.aiff':
return '.aif'
if x == '.jpeg':
return '.jpg'
if x == '.midi':
return '.mid'
if x == '.mpeg':
return '.mpg'
if x == '.tiff':
return '.tif'
return x
def rn(l, r):
for f in l:
st = os.stat(f)
t = min(st.st_mtime, st.st_ctime)
s = tf(t, r)
d, n = os.path.split(f)
_, x = os.path.splitext(n)
x = en(x)
nn = os.path.join(d, s + x)
c = 0
while os.path.exists(nn):
c += 1
nn = os.path.join(d, f'{s}-{c}{x}')
os.rename(f, nn)
if __name__ == '__main__':
r = '-r' in sys.argv
l = [
a for a in sys.argv[1:] if a != '-r'
] or [
f for f in os.listdir() if f.endswith(X)
]
rn(l, r)
The wrappers
@echo off
chcp 65001 >nul
py -3 "%~dpn0.py" %*
#!/bin/bash
s=$0
[[ $s != */* ]] && s=$(command -v -- "$s")
s=$(readlink -f -- "$s")
python3 "${s}.py" "$@"
The REG file
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SystemFileAssociations\audio\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\audio\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\image\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\image\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\video\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\video\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.3gp\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.3gp\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aac\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aac\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aif\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aif\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aifc\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aifc\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aiff\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.aiff\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.alac\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.alac\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.amr\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.amr\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ape\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ape\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.arw\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.arw\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.cr2\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.cr2\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.f4v\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.f4v\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.flac\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.flac\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.flv\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.flv\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.heic\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.heic\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.heif\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.heif\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4a\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4a\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4b\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4b\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4v\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.m4v\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mov\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mov\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mp4\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mp4\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.nef\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.nef\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.oga\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.oga\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ogg\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ogg\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.opus\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.opus\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.orf\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.orf\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.pef\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.pef\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.raf\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.raf\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.raw\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.raw\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rm\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rm\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rmvb\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rmvb\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rw2\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.rw2\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.srw\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.srw\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.svg\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.svg\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webm\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webm\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\0TS]
@="Rename as formatted timestamp"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\0TS\command]
@="\"C:\\_c\\ts.bat\" \"%1\""