Created
April 3, 2017 11:31
-
-
Save nickbutcher/5774eef98fefe27c4c63e667b1b281e4 to your computer and use it in GitHub Desktop.
A script for setting creation and modification dates on all files within a folder. Specify the first date in the script then all files are stamped in alphabetical order, increasing by 1s.
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
USAGE="Set the desired date/time in the script and then call: timestamp /path/to/folder" | |
DATE="022109002017" # month day hour min year | |
if (( $# != 1 )); then | |
echo "Illegal parameters" | |
echo $USAGE | |
exit 2 | |
fi | |
if [ ! -d $1 ]; then | |
echo "$1 is not a directory" | |
echo $USAGE | |
exit 2 | |
fi | |
SEC=0 | |
# run through all files in the directory in ls order i.e. alphabetically | |
for i in $( ls "$1" ); do | |
TIMESTAMP=$(date -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S') | |
FILE="$1/$i" | |
# set both creation and modification timestamps | |
SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}" | |
# increment the timestamp by 1 second | |
SEC=$((SEC + 1)) | |
done | |
exit 0 |
This seems to work for me:
TOTAL_SECONDS=0
# run through all files in the directory in ls order i.e. alphabetically
for i in $( ls "$1" ); do
MIN=$((TOTAL_SECONDS / 60))
SEC=$((TOTAL_SECONDS % 60))
TIMESTAMP=$(date -v"$MIN"M -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S')
FILE="$1/$i"
# set both creation and modification timestamps
SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}"
# increment the timestamp by 1 second
TOTAL_SECONDS=$((TOTAL_SECONDS + 1))
done
I modified this a bit to increment hours as well,
# Run through all files in the directory in ls order i.e. alphabetically.
for i in $( ls "$1" ); do
HOUR=$((TOTAL_SECONDS / 3600))
MIN=$(( ((TOTAL_SECONDS / 60)) % 60 ))
SEC=$((TOTAL_SECONDS % 60))
FILE="$1/$i"
# Set both creation and modification timestamps.
TIMESTAMP=$(date -v"$HOUR"H -v"$MIN"M -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S')
SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}"
# Leaving a gap of few minutes makes it possible to insert additional media
# to Google Photos later. This has to be in the magnitude of minutes because
# Google Photos doesn't allow changing the seconds value when updating a
# media's timestamps.
TOTAL_SECONDS=$((TOTAL_SECONDS + 300))
done
By the way, is anyone else still using this script? For some reason, Google Photos orders videos by their Last Opened
timestamp, completely ignoring their created and modified timestamps. I had to spend many hours manually editing the timestamp of each video in my album.
I wish SpeakerDeck supported videos.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm... I start getting errors after 60 images: