首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Applescript在多个文件夹中查找最接近给定时间创建的文件

Applescript在多个文件夹中查找最接近给定时间创建的文件
EN

Stack Overflow用户
提问于 2019-01-24 01:51:52
回答 1查看 79关注 0票数 2

我正在尝试编写一个脚本,它将搜索100个文件夹,并在每个文件夹中找到最接近上午9点(例如)创建的文件。

我正在研究一个长达30年的时间流逝(已经在4年内),我希望能够找到最接近一天中的某个时间创建的照片,快速有效。

任何建议都是非常感谢的。

我已经开始用applescript编写代码,但我不知道如何比较"date created“

EN

回答 1

Stack Overflow用户

发布于 2019-02-05 09:34:01

我认为在Macs 中做这件事是相当可怕的,所以我在bash中做了这件事,它已经安装在所有的上了。

您也可以使用do shell script从applescript调用我的脚本。

因此,您需要在主目录中将以下内容保存为NearestBirth

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/bin/bash
################################################################################
# NearestBirth
# Mark Setchell
#
# Usage:
# NearestBirth DIRECTORY H:M:S
# Finds the file in the specified directory with the birth time nearest the 
# specified time.
################################################################################

# Set DEBUG=1 for verbose output, set DEBUG=0 for quiet
DEBUG=1

# Check we have 2 parameters and pick them up
if [ $# -ne 2 ]; then
   echo "Usage:"
   echo "NearestBirth DIRECTORY H:M:S"
   exit 1
fi

# Pick up parameters
dir=$1
hms=$2

[ $DEBUG -eq 1 ] && echo "DEBUG: dir=$dir, hms=$hms"

################################################################################
# hms2s - convert HH:MM:SS to seconds since midnight
################################################################################
hms2s(){
   IFS=: read h m s <<< "$1"
   ((result=(10#$h*3600)+(10#$m*60)+10#$s))
   echo $result
}

################################################################################
# birthtime - get birthtime of file in seconds since midnight on day of creation
################################################################################
birthtime(){
   # The following command gives the birthtime of a file on macOS
   # stat -f "%SB" someFile.txt
   # Feb  4 18:49:05 2019
   s=$(stat -f "%SB" "$1" | awk '{print $3}')
   result=$(hms2s $s)
   echo $result
}

################################################################################
# main
################################################################################
cd "$top" || { echo "Unable to change to $top"; exit 1; }

# Work out target age of file in seconds since midnight
tgt=$(hms2s "$hms")
[ $DEBUG -eq 1 ] && echo "DEBUG: Target age in seconds=$tgt"

shopt -s nullglob

nearestTime=100000                # More than number of seconds in a day - must surely be beaten
nearestFile="ERROR: None found"   # Must surely get overwritten

# Iterate over all files in the directory
for f in *; do
   birth=$(birthtime "$f")
   ((diff=tgt-birth))
   [ $diff -lt 0 ] && ((diff=-1*diff))
   if [ $diff -lt $nearestTime ] ; then
       nearestTime=$diff
       nearestFile="$f"
       [ $DEBUG -eq 1 ] && echo "DEBUG: New nearest ($birth): $f"
   fi
done
echo "$nearestFile"

然后,您将启动终端,并使用以下命令使脚本可执行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
chmod +x $HOME/NearestBirth

然后,您可以像这样运行脚本,在目录/Users/mark/StackOverflow中查找距离09:00:00最近的文件出生时间

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$HOME/NearestBirth  "/Users/mark/StackOverflow"  09:00:00

示例输出(使用DEBUG=1)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
./NearestBirth /Users/mark/StackOverflow 09:00:00
DEBUG: dir=/Users/mark/StackOverflow, hms=09:00:00
DEBUG: Target age in seconds=32400
DEBUG: New nearest (64051): 16bit.py
DEBUG: New nearest (60948): 2d-plot
DEBUG: New nearest (46205): 45.py
DEBUG: New nearest (26224): 565.py
DEBUG: New nearest (38203): HSV.py
DEBUG: New nearest (32131): IMBoxAreas
DEBUG: New nearest (32235): restore.py
restore.py

关键词:macOS,OSX,文件出生时间,出生时间,创建时间,stat,mtime,ctime,atime

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54338301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文