AppleScript的找到最接近创建一个给定的时间内,文件 - 贯穿多个文件夹

问题描述 投票:2回答:1

我想编写一个脚本,将通过100个文件夹搜索和查找每个文件夹中最接近创建上午9时(例如)的文件。

我的工作有30年之久延时(在已经4年),我希望能够找到照片创建最接近一天的某个时间快速,高效地。

任何意见是极大的赞赏。

我已经开始写在AppleScript的代码,但我无法弄清楚如何比较“创建日期”

applescript
1个回答
1
投票

我认为这将是相当可怕的试图做到这一点的AppleScript中,所以我在其中安装在所有Mac反正bash做到了。

您也可以从AppleScript的反正把我的脚本,使用do shell script

所以,你会保存在你的主目录NearestBirth如下:

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

然后,你将开始终端,使脚本可执行文件:

chmod +x $HOME/NearestBirth

然后,您可以运行这样的脚本,找到最近的文件诞生在09:00:00目录/Users/mark/StackOverflow

$HOME/NearestBirth  "/Users/mark/StackOverflow"  09:00:00

样品输出(与DEBUG = 1)

./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,文件出生时间,出生的时间,创建时间,统计,修改时间的ctime,atime的

© www.soinside.com 2019 - 2024. All rights reserved.