PDA

View Full Version : Batch subdirectory processing


Dougal
September 20th, 2004, 11:48
Hi all,

Can any of you script gurus help me out here?

I need to find a way to loop down through a set of subdirectories and in each directory :-

1 - enter this directory
2 - create a directory in another part of the directory tree with the same name
3 - run a shell script (in this case to convert MP3 quality) and write the converted file to the new directory location.

I have a shell script that will convert the mp3 files in the current directory but I'm dealing with a couple of hundred artists/albums here so I don't fancy doing it all by hand.

Any pointers would be a great help,

Dougal

molotov
September 20th, 2004, 12:21
its not too hard to do in perl (I dont know shell scripting that well)

#!/usr/bin/perl -w
use strict;
@ls = `ls target/dir`;
foreach (@ls) {
echo `cd $_;mkdir $_;mp3 script;mv new_mp3s $_`;
}


thats massivly untested and I havnt writen perl in something like two years but its the general idea. Later tonight Ill fix it up so it works (Im going to assume it dosn't, but it might)

hope this helps?

Kernel_Killer
September 20th, 2004, 14:04
#!/bin/sh

DIR=$1
DEST=$2
echo "Starting with $2$1"
echo "new directory: ""$2$1"

echo "Is this correct?"
echo "1. yes, 2. no"
read yn
if [ $yn -eq 1 ] ; then
echo "Running...."
mkdir $2$1 && /home/user/wav2mp3 *.wav && cp $1* $2$1 && exit 0;
else
if [ $yn -eq 2 ] ; then
exit 0;
fi
fi

Try that. The way that it's used would be like this:

script.sh [directory of music] [directory to make the new folder]

So, in other words.

# ./script GoodMusic/ /music/newMP3s/

You might want to change the command "wav2mp3 *.wav" to your conversion app, and whatever you want it to do.

bmw
September 20th, 2004, 14:22
Here's my entry, in shell. Untested, but should work. Set TO and FROM to the top of the tree where the converted mp3's should go to and where the input mp3's come from respectively. You supply the conversion script "conv-mp3-script" which reads an mp3 on stdin and writes the converted mp3 on stdout.


#!/bin/sh

FROM=/usr/old-mp3
TO=/usr/new-mp3

cd $FROM || exit 1
find . -name '*.mp3' | while :
do
read mp3 || break
targdir=$TO/`dirname $mp3`
[ -d $targdir ] || mkdir -p $targdir
conv-mp3-script < $mp3 > $TO/$mp3
done

[Re errors: Sheesh! That'll teach me to try to hack between meetings.]

I also realize now that the original req't had a conversion script that dealt with all the mp3's in each dir at once. My code above assumes that the conversion script does a single mp3 on each invocation. The find could be hacked to only list dirs

find . -type d | while :

and the rest of the script could be adjusted slightly accordingly.

soup4you2
September 22nd, 2004, 09:11
find . -type f -name *.mp3 -exec script.sh {} \;

Dougal
September 28th, 2004, 05:49
Hi All,

Thanks for the suggestions. I'll have a go as soon as I get a chance.

My server at home decided it's about to die so I'm sorting hardware and getting ready for a complete rebuild to get away from m$ 2K pro and onto OpenBSD completely.

As soon as the servers fettled I'll be right back to doing cool stuff again and will give your suggestions a go.