Updating File Created Date by x number of days Mac

2019-09-04 12:09发布

问题:

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.

回答1:

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


回答2:

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