Apologies in advance. I am a complete newbie when it comes to programming. I am hoping that someone out there might be able to help me.
Based on the nature of my work, I have to constantly rename a bunch of images. The filenames sometimes vary, but are often some form of chronological numbers displayed in sequential order in their own separate directory (think image001.jpg, etc. but not always that naming format). Anyway, I have to rename all of the files in this particular directory to "000-001.jpg", "002-003.jpg", "004-005.jpg", etc. for every jpg file existing in that single directory. There are usually between 10-999 images in a given directory, but I am rarely given some directories with 1000+ images (in which case I begin naming all files "0000-0001.jpg", "0002-0003.jpg", etc.).
I have been manually renaming probably hundreds of thousands of these images over the course of the last several years. Now I'm finally looking for a way to batch rename them so I don't have to waste hours doing so. It seems that Automator on OSX is the easiest way to go, but Automator cannot rename files in the way I need them on its own.
I did notice that I can employ shell scripts or applescripts as part of an Automator function, so I was wondering if someone might be willing to write a short script for use in Automator (I work much better with a GUI) that would provide the above function? Unfortunately, I have no experience scripting. The absolute best ideal outcome would be for me to use that script within an Automator Application and just drag the files I need renamed onto it. This would bypass my need to open the OSX Terminal, with which I also have zero experience.
As an added note, I am using an older laptop running OSX 10.6.8. I might not have the most updated versions of Applescript, etc. to work from. I would be greatly appreciative if anyone were willing to help me out. Thanks!
-- JE
答案 0 :(得分:1)
我会在bash
shell中这样做,因为我发现Applescript非常冗长!该脚本将所有错误消息写入桌面上名为" renamerLog.txt"的文件中。
您必须将单个文件夹放到图标上。
它复制指定目录中的文件,将其按照您在途中的要求重命名为桌面上名为"输出"的目录(文件夹)。在开始之前必须不存在 - 否则会混淆不同书籍的页面。
在Automator
中看起来像这样:
您可以从下面复制并粘贴实际脚本:
# Place where we will save messages
LOG="$HOME/Desktop/renamerLog.txt"
# Clear log from any previous run
> "$LOG"
# Set up name for output directory
OUT="$HOME/Desktop/Output"
# Check user dropped a directory, rather than files
if [ ! -d "$1" ]; then
echo "ERROR: Please drop a DIRECTORY onto me!" >> "$LOG"
exit 1
fi
echo "Processing files in: $1" >> "$LOG"
# Go to that directory
cd "$1"
if [ $? -ne 0 ]; then
echo "ERROR: Unable to go to specified directory" >> "$LOG"
exit 1
fi
# Create output directory
mkdir "$OUT"
if [ $? -ne 0 ]; then
echo "ERROR: Unable to create output directory" >> "$LOG"
exit 1
fi
# Don't barf if no files or upper or lower case
shopt -s nullglob
shopt -s nocaseglob
# Count files to determine number of leading zeroes required
for f in *.jpg; do
((nfiles++))
done
# Tell user how many files we found and set up output formattiing
echo "Files: $nfiles" >> "$LOG"
format="$OUT/%03d-%03d.jpg"
[ $nfiles -gt 999 ] && format="$OUT/%04d-%04d.jpg" # Maybe this should be "-gt 499" rather than "-gt 999"
# Now copy the files renaming them as we go
i=0
j=1
for f in *.jpg; do
newname=$(printf $format $i $j)
((i+=2))
((j+=2))
echo Copy $f to $newname >> "$LOG"
# cp "$f" "$newname"
done
您可以尝试运行几次并查看日志以查看您是否喜欢它所说的内容,然后,如果您对它感到满意,可以从倒数第二行中删除#
以便它实际上是复制文件。
我更喜欢通过复制而不是重命名来实现,因为我不想冒失去数据的风险。