2

Hi I have a load of jpg and avi files in sub folders which are named with the date the files were created. Unfortunately the files themselves have incorrect creation dates which I would like to batch change to that of the folder names.

I've had a hunt around the forum and found some good info on changing the creation date to that of the file name but can't find anything on the same process for using a folder name instead. I've used the rename tool to get the folders into the correct date format for using the touch -t command and then I was trying to find a way to use a similar process to the one in this post: read creation date from file name and change - batch to apply the date change in bulk to each file.

The files are in a folder with a subfolder for each date they were taken on. An example subfolder name is 2005_09_28 with files named IMG_0441.jpg, IMG_0445.jpg and MVI_0448.avi in them. I'd like to change the creation date of each file to match the subfolder the are in.

I've looked at doing it by using software such as 'a better finder' to rename the files and then use the method in the post mentioned above but would like to so it without renaming the files if possible.

0

1 Answer 1

1

Assuming a simple two-level structure of folders named YYYY-MM-DD and files directly in these folders:

cd PARENT-FOLDER
for d in *; do
    if [[ -d "$d" && "$d" =~ [0-9]{4}_[0-9]{2}_[0-9]{2} ]]; then
        touch -t "$(sed 's/_//g' <<< $d)0000" "$d" "$d"/*
    fi
done

Please note: No error handling, this may fail in case of "unexpected" data or empty folders.

  • for d in *; do iterates over all entries in the folder
  • -d "$d" tests whether the current entry actually is a directory
  • $d =~ [0-9]{4}_[0-9]{2}_[0-9]{2} tests whether the actual entry matches our YYYY-MM-DD pattern
  • $(sed 's/_//g' <<< $d)0000 runs sed in a subshell to replace the _ by "nothing" to get YYYYMMDD, and appends 0000 (midnight) for the time part. This could also be written as $(sed -E 's/_(..)_(..)$/\1\20000/g' <<< $d)

The above will change access and modification date (which often is enough). If you indeed want to change the creation date, you need to install Xcode from the App Store to get the SetFile utility. You can then run

cd PARENT-FOLDER
for d in *; do
    if [[ -d "$d" && "$d" =~ [0-9]{4}_[0-9]{2}_[0-9]{2} ]]; then
        SetFile -d "$(sed -E 's|(.*)_(.*)_(.*)|\2/\3/\1 00:00|' <<< $d)" "$d" "$d"/*
    fi
done
3
  • Maybe you could add that your suggestion changes the modification and access time of the file, while the OP asked for the creation time. Perhaps your solution is good enough for the OP, but if your answer is supposed to become a reference for this question, it's better to clarify this explicitly. Feb 28, 2022 at 11:53
  • 1
    @user1934428 It's not so simple, actually, see apple.stackexchange.com/a/99599/9058. But you are right, I should change this to use SetFile instead.
    – nohillside
    Feb 28, 2022 at 11:59
  • I definitely would add the SetFile alternative to the answer. It's drawback is that SetFile is not present by default on every Mac, while touch can be assumed to be here. Knowing both possibilities, the OP can then choose what fits best to his situation. Feb 28, 2022 at 12:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .