gpt4 book ai didi

error-handling - Applescript中的错误处理,该错误基于文件名将文件 move 到目标文件夹

转载 作者:行者123 更新时间:2023-12-03 08:57:39 26 4
gpt4 key购买 nike

我写了一个Applescript,可以完成我想做的事情。那是:

根据文件名将文件从源文件夹移动到目标文件夹中的子文件夹的子文件夹。

但是,我的问题是脚本中没有错误处理。

示例:
文件
“130299_1833_9_Actierondje Cantomobili Figo 400mm_LR.jpg”被放置在目标文件夹中的子文件夹“0001833”的子文件夹“00009”中

但是,我希望脚本检查文件名的第二部分和第三部分是否实际上是数字/整数。如果它们不是数字/整数,则文件将移至错误文件夹,并且脚本将继续使用源文件夹的下一个文件。

示例:
文件
“130299_9_Actierondje Cantomobili Figo 400mm_LR.jpg”以及
““130299_1833_Actierondje Cantomobili Figo 400mm_LR.jpg”被放置在错误文件夹中

我目前有以下Applescript:

set source_folder to alias "Macintosh HD:Users:Roy:Desktop:_LR JPG's" --as alias
set destination_folder to "Macintosh HD:Users:Roy:Desktop:Produktcommunicatie test" as alias
set org_delimit to text item delimiters of AppleScript

tell application "Finder"

set source_files to files of source_folder

--display a dialog if there are no files to move
if number of source_files is 0 then
display dialog "There are no files to move" buttons {"OK"} default button 1
end if

repeat with this_file in source_files
--set the leveranciersnummer en modelnummer
set text item delimiters of AppleScript to {"_"}
set mgDocname to name of this_file
set mgLeveranciersnummer to text -7 thru -1 of ("0000000" & text item 2 of mgDocname)
set mgModelnummer to text -5 thru -1 of ("00000" & text item 3 of mgDocname)

--check if the folders already exist in the destination, if not, create the folders
if (exists folder mgLeveranciersnummer of folder destination_folder) is false then
make new folder at destination_folder with properties {name:mgLeveranciersnummer}
end if

set text item delimiters of AppleScript to org_delimit
set model_folder to destination_folder & mgLeveranciersnummer as string
--set model_folder to alias model_folder

if (exists folder mgModelnummer of folder model_folder) is false then
make new folder at model_folder with properties {name:mgModelnummer}
end if

set final_path to model_folder & ":" & mgModelnummer as string
move this_file to final_path with replacing
end repeat

--display a dialog if the all the files are moved
display dialog "All the files are moved to " & destination_folder buttons {"Thanks!"} default button 1

end tell

最佳答案

您可以改用如下所示的shell脚本:

cd ~/Desktop/_LR\ JPG\'s
for f in *; do
f2=$(cut -d_ -f2 <<< "$f")
f3=$(cut -d_ -f3 <<< "$f")
if [[ $f2 =~ ^[0-9]+$ && $f3 =~ ^[0-9]+$ ]]; then
d=~/Desktop/Produktcommunicatie\ test/$(printf %07d $f2)/$(printf %07d $f3)
else
d=~/Desktop/error
fi
mkdir -p "$d"
mv "$f" "$d"
done

关于error-handling - Applescript中的错误处理,该错误基于文件名将文件 move 到目标文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com