批量更改QQ聊天场景mp3音乐文件名为其所在目录名
QQ的聊天场景中(在目录:IMScene/Scene中)的mp3音乐文件名是像这样的E00001.mp3的符号命名(老婆原来刻苦聊天时在聊天场景中攒下了200首mp3,IMScene目录有700多M),我把这些文件在linux下mp3blaster下播放时看不到音乐的中文文件名。碰巧聊天场景中的每一个目录的名字就代表了其下的音乐的名字,于是,我花了一个下午写了下面这个bash脚本来把mp3音乐的名字改成跟它的目录名一样。一方面是有了中文名字的音乐文件,一方面学习一下linux的bash脚本写作。我想,在windows下用批处理脚本也应该能完成这个工作。但是我学电脑的时候已经过了microsoft的dos时代,就没有学过批处理。倒是linux下的批处理好像还在类unix世界广泛使用。
当然,我很少用和写bash脚本,如果哪位同志看到其中的错误、不当、和可以改进之处请不吝赐教。谢谢。
使用的关键知识点:
1)、for循环遍历目录;
2)、文件相关判断;
3)、字符串比较与数字比较的区别;
4)、对文件名带中文和文件名中带有空格的文件进行操作时,必须对文件名加上双引号;
一些小技巧:
1)、控制脚本只在这个目录执行一次;
2)、控制一个目录下多个类似文件的改名;
#!/bin/bash
# rename.sh
# Rename the file's name as its directory name, like this:
# Rename "/home/m/Scene/求佛/E00001.mp3" to
# "/home/m/Scene/求佛/求佛.mp3"
NOARGS=65
CONFIRM_FAIL=66
RUNONCE_FLAG=67
# Input the first level directory.
# The dirctories structure is like this:
# first level directory
# second level directory 1
# files in second level directory 1
# second level directory 2
# files in second level directory 2
# second level directory 3
# files in second level directory 3
# ...
if [ $# -lt 3 ]
then
echo "Usage: $0 dir redir suffix ..."
exit $NOARGS
fi
dir=$1
redir=$2
suffix=$3
echo "your first argument is $dir"
echo "your second argument is $redir"
echo "your third argument is $suffix"
if [ $dir != $redir ]
then
echo "Usage: $dir must equal to $redir for confirming..."
exit $CONFIRM_FAIL
fi
if [ -e $dir/runonce.flag ]
then
echo "Usage: This program can only run once!"
exit $RUNONCE_FLAG
fi
directorys=$dir/*
for directory in $directorys; do # Find all second level directories
if [ -d "$directory" ] # Note! It must add double quote before
# and after $directory, otherwise, it will
# report error if there is blank or chinese
# in $directory.
then
echo "$directory is a directory"
count=0
for file in "$directory"/*$suffix; do # Find the mp3 file in second
# level directory
if [ -f "$file" ]
then
echo "$file is a file"
dirname=${directory##*/} # get the second level directory name
# without its prefix path like /home/m/
echo "$file's directory is $dirname"
if [ $count -gt 0 ]
then
echo "rename $file to $directory/$dirname$count$suffix"
echo " rename #$count file begin..."
mv "$file" "$directory/$dirname$count$suffix"
else
echo "rename $file to $directory/$dirname$suffix"
mv "$file" "$directory/$dirname$suffix"
fi
let "count = count + 1"
echo "rename $count file ok"
fi
done
else
echo "$directory is not a directory"
echo "Usage: $0 dir redir ..."
fi
done
# Write runonce flag file into the first level directory
echo "Run rename.sh once at `date`" > $dir/runonce.flag
exit 0
|
|
1、上面的脚本是基于不再使用QQ的聊天场景后只想听其中的音乐的作法,如果您的QQ聊天场景仍然需要使用,千万不能用上面的脚本更改里面的音乐文件名,否则您的QQ聊天场景应该不能够再继续正常使用! 原因如下,在QQ聊天场景中与音乐同在一个目录的有一个配置文件Config.qqs需要调用音乐名,如果您改了音乐的名字,那么这个配置文件就找不到原来的音乐名了。如果需要在改音乐文件名后仍然要正常使用聊天场景,那么修改这个配置文件也许可以。但是我不需要使用聊天场景,所以就不去做这个修改了。如果您感兴趣的话可以去做,做好了要是能共享在此就感激不尽了:)
下面是那个Config.qqs配置文件的内容示例,修改的话把里面的E000008.mp3换成您改过的音乐文件名就可能行了:
<?xml version="1.0" encoding="gb2312"?><theme ver="1" GUID="BB5306887CA3462E8B3D57532E22C2A8">
<Scene Type="1" BackGroupColor="#FFFFFF" FriendCaptionColor="#000000" MineCaptionColor="#000000" MsgTextColor="#000000" SystemRequestColor="#000000" SystemResultColor="#000000">
<Normal>
<JPG id="E000001" zIndex="1" visible="true" tile="false" left="71" top="0" width="168" height="230" SourcePath="E000007.jpg"/>
<SOUND Loop="true" SourcePath="E000008.mp3"/>
</Normal>
</Scene>
</theme>
2、上面脚本使用格式示例: ./rename.sh /home/m/Scene /home/m/Scene .mp3
注意: mp3前的.是必须的。
另外,当我想修改上面的日记时系统报错如下,可能是日记的内容有程序上的特殊字符,具体原因还没找到,记在这里备忘:
500 Servlet Exception
javax.servlet.jsp.JspException: An error occurred while evaluating custom
action attribute "value" with value "批量更改QQ聊天场景mp3音乐
...(是上面这篇日记的内容,全文略)
": Encountered "#", expected one of ["}", ".", ">", "gt", "<", "lt", "==",
"eq", "<=", "le", ">=", "ge", "!=", "ne", "[", "+", "-", "*", "/", "div",
"%", "mod", "and", "&&", "or", "||", ":", "("] (null)
at org.apache.taglibs.standard.lang.jstl.Evaluator.evaluate(Evaluator.java:109)
at org.apache.taglibs.standard.lang.jstl.Evaluator.evaluate(Evaluator.java:129)
at org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager.evaluate(ExpressionEvaluatorManager.java:75)
at org.apache.strutsel.taglib.utils.EvalHelper.evalString(EvalHelper.java:72)
at org.apache.strutsel.taglib.html.ELTextareaTag.evaluateExpressions(ELTextareaTag.java:786)
at org.apache.strutsel.taglib.html.ELTextareaTag.doStartTag(ELTextareaTag.java:632)
at _jsp._disall._disgoal._editadvice__jsp._jspService(disall/disgoal/editAdvice.jsp:241)
...(略)