将文件创建日期更新x天数Mac OSX

时间:2015-02-08 23:22:09

标签: macos bash shell datetime

我有一个视频文件夹(Mac OSX Yosemite),我需要通过在现有创建日期添加2180天来更改创建日期。

使用终端中的SetFile我可以操作创建日期,例如我可以将其设置为等同于同一文件的修改日期:

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

但是,如果我尝试添加“添加2180天”部分,它将停止工作:

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

我怀疑支架和语音标记是一个问题,但以下情况也不起作用:

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

我应该如何纳入' +2180天'进去吧?

Edi:Mark Setchell有一个有效的解决方案,但我很想知道是否实际上有一种方法可以整合' + 2180天'到基于GetFileInfo的-d日期变量。

2 个答案:

答案 0 :(得分:1)

对于显然如此简单的事情来说,这很有趣!我认为这有效,但在一些示例文件上测试它。我留下了调试语句,但您可以安全地删除所有echo语句。

#!/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"

将其保存为ReDate,并使用

使其可执行(仅需一次)
chmod +x ReDate

然后像这样运行:

./ReDate /Users/myfilename.mov

示例运行

./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

答案 1 :(得分:0)

我知道这是一个较旧的线程,但是我想做这样的事情。 OP @MrDave正在询问有关通过普通语音进行添加的问题。可以使用AppleScript来完成。

有点冗长,但是有效:

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

现在从终端运行:

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

在这里,您实际上可以将文件从Finder拖到终端上,而不是键入文件名本身,然后按Enter