lundi 9 février 2015

Recursive set creation date of pictures and videos based on filename


I have a folder structure of thousands of pictures and videos. They are all exported and therefore have the same creation date, time. I would like to set the creation date for all the files according to the names the have. All files follow the same naming convention and have the date and time in the file name.


Example:



Pictures (top folder)
-- Folder 1
---- 2010-05-03
------ 2010-05-03 at 11-58-32.jpg
------ 2010-05-03 at 11-59-32.jpg
------ 2010-05-03 at 12-58-32.jpg
---- 2010-07-04
------ 2010-07-04 at 11-58-32.jpg
------ 2010-07-04 at 12-01-32.mov
-- Folder 2
---- 2090-03-03
------ 2010-03-03 at 10-01-32.jpg
------ 2010-03-03 at 11-01-32.jpg


My current version of the code includes the touch command and I go recursive through the folder structure, but this seems to get stuck after the first time I reach the lowest folder path.



for dir in Pictures/*; do
if [[ -d "$dir" ]]; then
cd "$dir"
for dir2 in *; do
if [[ -d "$dir2" ]]; then
cd "$dir2"
for f in *; do
t=$(echo $f | sed -E 's/([A-z]*-)|([A-z ,;])|(\..*)//g' | sed -E 's/(.*)(..)/\1.\2/')
touch -t $t "$f"
done
cd -
fi
done
cd -
fi
done


I also have thought about the find command, but I am not sure how to then extract the date and time from the filename for the touch command.





Aucun commentaire:

Enregistrer un commentaire