寻找Applescript来查找文件并将它们移动到不同的文件夹

时间:2013-02-25 21:35:33

标签: find applescript move

我正在尝试找到一个脚本来查找文件并将其移动到另一个文件夹。

我有一个包含数百张照片的文件夹:

PA-600-01.jpg, PA-600-02.jpg, PA-600-03.jpg, PA-600-04.jpg, PA-601-01.jpg, PA-601-02.jpg, PA-601-03.jpg, PA-602-01.jpg, PA-602-02.jpg, PA-602-03.jpg, PA-602-04.jpg, PA-602-05.jpg

我想将PA-600so PA-600-01.jpg, PA-600-02.jpg, PA-600-03.jpg and PA-600-04.jpg)的所有图片移动到名为PA-600的文件夹(新的或已经存在,更容易......)上,移动所有图片在名为PA-601的文件夹上PA-601-01.jpg, PA-601-02.jpg and PA-601-03.jpgPA-601),将PA-602PA-602-01.jpg, PA-602-02.jpg, PA-602-03.jpg, PA-602-04.jpg and PA-602-05.jpg)的所有图片移至名为PA-602的文件夹中。 。直到PA-699

我试图移动文件但不是一组文件:

tell application "Finder" make new folder at alias "Macintosh HD:Users:AirYoSo:Desktop:600-699" with properties {name:"PA-600"} copy file "Macintosh HD:Users:AirYoSo:Desktop:600-699:PA-600-01.jpg" to folder "Macintosh HD:Users:AirYoSo:Desktop:600-699:PA-600" end tell

2 个答案:

答案 0 :(得分:1)

尝试:

set myFolder to (choose folder)
set pFolder to POSIX path of myFolder
set folderNames to paragraphs of (do shell script "find " & quoted form of pFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | grep -Eo PA-[0-9]{3} | uniq")

repeat with aFolder in folderNames
    (do shell script "mkdir -p " & quoted form of (pFolder & aFolder))
    tell application "System Events" to move (every file of myFolder whose name begins with aFolder) to (pFolder & aFolder)
end repeat

EDIT 如果要将路径硬连接到可以使用的文件夹:

set myFolder to "Macintosh HD:Users:YoSo:Desktop:test"
set pFolder to myFolder's POSIX path & "/"
set folderNames to paragraphs of (do shell script "find " & quoted form of pFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | grep -Eo PA-[0-9]{3} | uniq")

repeat with aFolder in folderNames
    (do shell script "mkdir -p " & quoted form of (pFolder & aFolder))
    tell application "System Events" to move (every file of folder myFolder whose name begins with aFolder) to (pFolder & aFolder)
end repeat

答案 1 :(得分:0)

不知道如何在AppleScript中做到这一点,但是在你的Mac上安装的bash中这很简单:

#!/bin/bash
for (( c=600; c<=699; c++ ))
do
  echo "Processing PA-$c"
  mkdir -p PA-$c
  mv PA-$c-*.jpg PA-$c/
done

将其保存到文件中,例如script.sh,将文件复制到包含jpg文件的目录,并在终端中以这样的方式运行(使用实际路径替换/ Users / lionel / files)文件):

$ cd /Users/lionel/files 
$ bash script.sh