按x天数更新文件创建日期Mac OSX [英] Updating File Created Date by x number of days Mac OSX

查看:122
本文介绍了按x天数更新文件创建日期Mac OSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

如何究竟我应该将 +2180天并入其中吗?

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

Edi:马克·谢切尔有一个有效的解决方案,但我很想知道是否实际上有一种方法可以将 +2180天合并到基于GetFileInfo的-d日期变量中。

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.

推荐答案

看起来很简单的事情真有趣!!!我认为这可行,但是请在一些示例文件上进行测试。我保留了调试语句,但是您可以安全地删除所有 echo 语句。

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"

另存为 ReDate 并使其可执行(仅必要一次),

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

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

这篇关于按x天数更新文件创建日期Mac OSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆