Go Back   The macosxhints Forums > OS X Help Requests > AppleScript

Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 12-01-2011, 08:26 PM   #1
Join Date: Dec 2011
Posts: 4
Question Find 'duplicate' files and move into new folder

Hi all,
I need someone to please help me build a script.

I have many .wav files within one folder. They are labeled as such:
[C1]AmpPERC.wav, [C2]AmpPERC.wav, [C3]AmpPERC.wav, etc.
Next file is:
[C1]BlueMist.wav, [C2]BlueMist.wav, [C3]BlueMist.wav, etc. and so on.....
I need a script to search for these 'duplicate' files names, create a folder of the same name and move those files into that folder.
So i would en up with a folder called [C1]AmpPERC which would contain the files [C1]AmpPERC.wav, [C2]AmpPERC.wav, [C3]AmpPERC.wav, etc.
I wouldnt mind if the folder is called [C1]AmpPERC, i could use automator to remove [C1].

Could someone help me do this?
k0ms is offline   Reply With Quote
Old 12-02-2011, 08:23 AM   #2
Join Date: May 2004
Posts: 2,099

for F in *.wav
    BASE="$(basename "$F" .wav)"
    NEW="$(echo "$BASE" | sed "s/^\[[A-Z0-9]\{2\}\]//")"
    mkdir -p "$NEW"
    mv -i "$F" "$NEW"/
Sorry it isn't AppleScript, but this should do fine.

I haven't tested this, but it should work fine if you save it as a text file (something like: move-wav.sh) and run it from the Terminal (make it executable) in the directory containing your files.

Paste the above script into a TextEdit document.
Save it as move-wav.sh, in the directory containing your WAV files.
Open the Terminal.app
Type (without the quotes, and note the space at the end): "cd "
Drag the folder containing your WAV files into the Terminal window, you should see the path entered for you.
Press enter.
You can type "ls" to see the files that are in this folder. move-wav.sh should be there as well.
Type: "chmod +x move-wav.sh"
Press enter. (that made the script executable so it can be run like an application)
Type: "./move-wav.sh"
Press enter.

That should be it. It may be a good idea to run the script with "echo " at the beginning of the line that starts with "mv". This will print out all the moves that are going to be done, without actually moving anything. This will let you check that nothing bad is about to happen.

Let me know if you need any help with this. And be careful in the Terminal. It can be a powerful and dangerous place :-)
fracai is offline   Reply With Quote
Old 12-02-2011, 09:47 AM   #3
Join Date: Dec 2011
Posts: 4
Awww, it worked like a treat! You made my life hell of a lot easier!
I didnt mid the bash script, i love the terminal. Used to use linux alot back in the day so the commands dont scare me. Lovely stuff man!

Thank you very much for takin the time to write that!
k0ms is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 02:56 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.