Skip to content

Instantly share code, notes, and snippets.

@baptx
Created October 12, 2014 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baptx/225ba4a76255a85934d7 to your computer and use it in GitHub Desktop.
Save baptx/225ba4a76255a85934d7 to your computer and use it in GitHub Desktop.
Rename by playlist index
IFS=$'\n'
if [ $# -gt 0 ]; then
if [ ! -f $1 ]; then
echo "Error: file $1 does not exist"
exit
fi
path=`echo $1 | rev | cut -d / -f 2- | rev`
if [ $path == $1 ]; then
path=$PWD
fi
fi
if [ $# == 1 ]; then
nb=0
for i in `cat $1`
do
if [ -f $path/$i ]; then
let ++nb
if [ $nb -lt 10 ]; then
digit="00"
elif [ $nb -lt 100 ]; then
digit="0"
else
digit=""
fi
mv -v $path/$i $path/$digit$nb" - "$i
fi
done
elif [ $# == 2 ] && [ $2 == "-r" ]; then
for i in `ls $path`
do
name=`echo $i | cut -d - -f 2- | cut -c 2-`
for j in `cat $1`
do
if [ $path/$name == $path/$j ]; then
mv -v $path/$i $path/$name
fi
done
done
else
echo "Playlist files will be renamed by playlist index.
This script is designed for M3U playlists using relative paths.
Playlist file needs to be in playlist files folder, subfolders not supported.
You can use this script with other playlists if you convert them.
Index uses 3 digits: playlist files are renamed alphabetically up to 999 files.
Usage: $0 playlist [OPTION]
OPTION: -r: remove index from filename (add by default)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment