2

I have a folder of videos (Mac OSX Yosemite) for which I need to change the Created Date by adding 2180 days to the existing Created Date.

Using SetFile from Terminal I am able to manipulate the Created Date, for example I can set it as equivalent to the Modified Date of the same file:

SetFile -d "$(GetFileInfo -m /Users/myfilename.mov)" /Users/myfilename.mov

However, if I try to add the ‘Add 2180 days’ part it stops working:

SetFile -d "$(GetFileInfo -d /Users/myfilename.mov) +2180 days" /Users/myfilename.mov

I suspect it is an issue with bracket and speech marks but the following did not work either:

SetFile -d "$(GetFileInfo -d /Users/myfilename.mov +2180 days)" /Users/myfilename.mov

How exactly should I be incorporating the '+2180 days' into it?

Edi: Mark Setchell has a solution which works but I am keen to know if there is in fact a way to incorporate '+2180 days' into the GetFileInfo-based -d date variable.

6
  • Suggest you read man SetFile for the format −d date is expecting. Feb 8, 2015 at 23:37
  • I have looked but am unable to solve my problem. I can find reference to date format but am not sure how the actual format of yyddmm etc relates to my question.
    – MrDave
    Feb 9, 2015 at 9:29
  • The man page for SetFile is quite explicit as to what format −d date requires and why I suggested you read man SetFile to begin with. Note that the output of GetFileInfo -d file is within the constraints of expected format that SetFile −d date requires. Therefore one must do in a similar way what Mark Setchell presented in order to do the math as neither utility has the ability to parse directly the math in the manner you've expressed in the OP. Feb 9, 2015 at 20:31
  • Ah, so does that mean that the format -d date requires excludes a suffix similar to "+xx days"? I assumed the format just applied for the actual yymmdd part and it still might permit an addition such as "+ x days".
    – MrDave
    Feb 10, 2015 at 7:01
  • I'm not sure I can make this any more clear then to say, you cannot do that which your trying to do in the manner you're trying to do it! SetFile -d date must conform to this format, "mm/dd/[yy]yy [hh:mm:[:ss] [AM | PM]]", e.g. 02/09/2015 01:11:50. The output of GetFileInfo -d or -m is in this format, mm/dd/yyyy hh:mm::ss, e.g. 02/09/2015 01:11:50. You cannot directly add mm/dd/yyyy hh:mm::ss + n days, e.g. 02/09/2015 01:11:50 + 2180 days and the values must be converted in order to do the math! Mark Setchell has shown you one of the ways to do the math. Do you get now? :) Feb 10, 2015 at 15:04

2 Answers 2

4

That's a lot of fun for something apparently so simple!!!! I think this works, but test it out on some sample files. I left my debugging statements in, but you can safely remove all the echo statements.

#!/bin/bash
# Get name of file as supplied as parameter
file=$1

# Get its timestamp in format "02/08/2015 21:14:44"
timestamp=$(GetFileInfo -d "$1")
echo timestamp:$timestamp

# Convert that to seconds since the Unix Epoch, e.g. 1423430084
epoch=$(date -j -f "%m/%d/%Y %H:%M:%S" "$timestamp" +%s)
echo epoch:$epoch

# Calculate seconds in 2180 days
((offset=2180*3600*24))
echo offset:$offset

# Add offset to epoch
((epoch+=offset))
echo new epoch:$epoch

# Get new date in format that SetFile wants
newdate=$(date -r $epoch "+%m/%d/%Y %H:%M:%S")
echo new date:$newdate

# And finally set the date of the input file
SetFile -d "$newdate" "$file"

Save it as ReDate and make it executable (only necessary once) with

chmod +x ReDate

and run it like this:

./ReDate /Users/myfilename.mov

Sample run:

./ReDate "/Users/Mark/tmp/file with sapce in name.mov"
timestamp:02/09/2015 09:54:01
epoch:1423475641
offset:188352000
new epoch:1611827641
new date:01/28/2021 09:54:01
5
  • From the OP, it said "...I need to change the Created Date by adding 2180 days to the existing Created Date" so timestamp=$(GetFileInfo -m "$1") needs to be timestamp=$(GetFileInfo -d "$1"). Note that -m needs to be -d in order to get the files created date as the former gets the modified date. Feb 9, 2015 at 10:33
  • @user3439894 Many thanks for pointing that out. It looks like I got the hard part right and the easy part wrong :-) I never use these functions myself. Hopefully it is correct now. Thanks again. Feb 9, 2015 at 10:39
  • This looks great, but as you say is surprisingly complicated. I need to find some time soon to test it out. Can I check, is there really no way to incorporate '+ x number of days' into a SetFile command line?
    – MrDave
    Feb 9, 2015 at 19:32
  • Not that I know of - though I could be wrong. I guess this is open to peer review and improvement on Stack Overflow. The number of days could easily be parameterised on my script if that was necessary. Feb 9, 2015 at 19:43
  • Or thinking about it, my question is about incorporating it into the GetFileInfo date variable
    – MrDave
    Feb 9, 2015 at 20:17
1

I know this is an older thread, but I wanted to do something like this. The OP @MrDave is asking about adding via normal speech. This can be accomplished with AppleScript.

A bit verbose, but it works:

on run {input}
  set filename to input as text
  set fileDate to (creation date of (get info for input))
  set newDate to fileDate + (1 * days)

  set newMonth to month of newDate as number
  set newMonth to lessThanTen(newMonth)

  set newDay to day of newDate as number
  set newDay to lessThanTen(newDay)

  set newYear to year of newDate as number

  set newHour to hours of newDate as number
  set newHour to lessThanTen(newHour)

  set newMinute to minutes of newDate as number
  set newMinute to lessThanTen(newMinute)

  set divider to "/"
  set newSetDate to newMonth & divider & newDay & divider & newYear & " " & newHour & ":" & newMinute as text
  set printNewDate to "\"" & newSetDate & "\"" as string
  log printNewDate
  -- date format for SetFile: mm/dd/yyyy hh:mm
  do shell script ("SetFile -d " & printNewDate & " -m " & printNewDate & " " & input)
end run

on lessThanTen(num)
  set thisNum to num as number
  if thisNum is less than 10 then
    set newNum to "0" & thisNum
  else
    set newNum to thisNum
  end if
  return newNum as string
end lessThanTen

now from terminal run:

osascript /Users/path/to/scriptName.scpt filename

and here you can actually just drag the file from Finder onto the Terminal instead of typing the filename itself, then hit enter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.