Applescript + Shell比较文件列表到文件列表并将文件移动到子文件夹中

时间:2014-02-18 04:13:42

标签: bash shell applescript

我正在尝试创建一个将运行下面的bash脚本的AppleScript。将名为temp_m的任何文件移动到具有匹配名称的文件夹。屏幕截图仅供参考。 AppleScript的好处是用户可以轻松选择“松散图像”(源)文件夹和构建(存档)文件夹。它们通常存在于不同的位置,有时位于服务器/共享位置。

E.G。来自

enter image description here

enter image description here

set myFolder to (choose folder)
set pFolder to POSIX path of myFolder
set folderNames to paragraphs of (do shell script "

PARENTDIRECTORY="pFolder"
find . -name "*.jpg" | while read file
do
   temp_m=$(basename "$file" .jpg)
   look=$(echo "$PARENTDIRECTORY/*/*${temp_m}" | sed 's/ /\\ /g') #I need to match all     subfolders in this parent folder that have the same name as temp_m name
   look=$(eval echo $look)
   if [ -f "$look" ]
   then
       todir="${look%/*}"
       echo mv "${temp_m}.jpg" ${todir}/ # echo action to perform - after testing remove echo
   else
       echo "No unique match for $file ($look)"
   fi
done

2 个答案:

答案 0 :(得分:1)

你可能只是使用这样的shell命令:

shopt -s nullglob;for f in ~/Pictures/loose\ files/*.jpg;do f2=${f##*/};d=~/Pictures/*/*/*/*/${f2%_*};[[ $d ]]&&echo mv "$f" $d;done

shopt -s nullglob使与任何文件不匹配的文件名模式扩展为空字符串。 ${f##*/}*/开始删除最长的f模式,${f2%_*}_*的末尾删除最短的f2模式。 [[ $d ]]相当于[[ -n $d ]],或者测试$d是否已设置且未设置为空字符串。

如果目标文件夹可以处于不同的嵌套级别,则可以使用find

for f in ~/Pictures/loose\ files/*.jpg;do f2=${f##*/};d=$(find ~/Pictures -type d -name ${f2%_*});[[ $d ]]&&echo mv "$f" $d;done

答案 1 :(得分:0)

您的bash代码有几个问题;这是一个已清理的版本,它会根据您的描述和屏幕截图尝试我认为您正在尝试做的事情:

# Set the source and target [root] dir as POSIX paths.
# Do NOT quote spaces here - this will be done below - but
# note that, if applicable, you must use the explicit home-directory
# path rather than '~'.
tell application "System Events"
  set SOURCEDIRECTORY to "."
  # Example: Set to ~/Pictures/Archive
  set PARENTDIRECTORY to POSIX path of home folder & "/Pictures/Archive"
end tell

# Set the level in the target subdirectory hierarchy at which the 
# specific target directories are located.
set SUBDIRLEVEL to 4

do shell script "
SUBDIRLEVEL=" & SUBDIRLEVEL & "
SOURCEDIRECTORY=" & quoted form of SOURCEDIRECTORY & "
PARENTDIRECTORY=" & quoted form of PARENTDIRECTORY & "

find \"$SOURCEDIRECTORY\" -type f -name \"*.jpg\" | while read file; do

  # Determine the subdir name prefix - everything before
  # the first \"_\" - with the extension stripped in any event.
  temp_m=$(cut -d '_' -f 1  <<<\"$(basename \"$file\" .jpg)\")

  # At the specified subdir level, find subdirs. of \"$PARENTDIRECTORY\"
  # whose name matches the prefix.
  matches=$(find \"$PARENTDIRECTORY\" \\
           -mindepth $SUBDIRLEVEL -maxdepth $SUBDIRLEVEL \\
           -type d -name \"${temp_m}\")

  # Expected: exactly 1 match.
  # Note: As long as only 1 level of subdirs is matched, there is 
  #          no strict need to test for *more* than 1 match.
  if [[ -n $matches && $(wc -l <<<\"$matches\") -eq 1 ]]; then
    todir=$matches
    # echo action to perform - after testing, remove echo       
    echo mv \"$file\" \"${todir}/\" 
  else
    echo \"No unique match for $file ($temp_m)\"
  fi

done
"

注意:

  • 此代码 - 作为您的原始代码 - 并非旨在简单地列出文件夹名称,因此set folderNames to paragraphs of之前的do shell script没有意义。
  • 子目录仅在指定的层次结构级别匹配。如果您需要更多灵活性,请根据需要调整-mindepth $SUBDIRLEVEL -maxdepth $SUBDIRLEVEL选项(删除它们将搜索所有级别)。
相关问题