I am trying to figure this out but with little success so far.
Is it possible to write an AppleScript that would stripe the first two numerical characters of any file (if it finds any) on a given hard drive? These files would all have the same extension (.wav)
Is this impossible? I’m doing an internship and I was given the task of renaming 15000 files manually!!! My brain will disintegrate if I have to do this one by one…
So far, I’ve got
tell application “Finder”
activate
make new Finder window to folder “media” of folder “Desktop” of folder “user_k” of folder “Users” of startup disk
set name of document file “00 unit1.wav” of folder “media” of folder “Desktop” of folder “user_k” of folder “Users” of startup disk to “unit1.wav”
end tell
How do I tell it to do it for every file in the folder? please tell me there’s a way…
Assuming that the file names contain exactly two numbers at the beginning of their names:
set F to choose folder -- the one containing the files
set newNames to {} -- place to store stripped names and names that didn't need stripping.
tell application "Finder"
set tPaths to files of entire contents of F as alias list -- digs down if there are folders in the chosen folder
repeat with aFile in tPaths
set N to name of aFile
try -- so those without leading numbers will be skipped
(text 1 thru 2 of N) as number -- this errors if the first two characters are not numbers - skips to next file.
if text 3 of N is space then -- check that the name wasn't 12 filename.ext
set NN to text 4 thru -1 of N -- skip the space too
else
set NN to text 3 thru -1 of N -- keep all but the first two characters of the name.
end if
if NN is in newNames then set NN to text returned of (display dialog "The proposed name already exists" & return & "Please edit or change the new name" default answer NN)
set name of aFile to NN
set end of newNames to NN -- record the name
on error -- no stripping was necessary, but record the name
set end of newNames to N -- record the original name even with no change
end try
end repeat
end tell
[Edit] By the way, if the number of numbers is variable that can be solved. If the name after stripping is the same as any other file in the folder, then you’ll be asked to supply a new name (just change what appears in the dialog).
another possibility might me to use a shell script instead of AppleScript for renaming -
a='#!/bin/sh'; echo $a > renameall.command; find * -type f -regex '.*\/[0-9][0-9][^/]*\.[wW][aA][vV]' | awk -F"/" '{printf("mv \""); for (n=1;n<NF;n++) printf("%s\/",$n); printf("%s\" \"",$NF);for (n=1;n<NF;n++) printf("%s\/",$n); sub(/^[0-9][0-9]/,"",$NF); printf("%s\"\n",$NF)}' >> renameall.command; chmod 755 renameall.command; echo "=========================================="; cat renameall.command; echo "=========================================="; echo 'enter "renameall.command" to process the renaming'
- open Terminal
- start ‘sh’ (if it’s not already the running shell)
- cd to your volume (‘cd /Volumes/NameOfYourVolume/’ + return)
then paste the script line above
- wait (up to some minutes depending on the volume size and processor speed)
- for security reasons it does not instantly rename - but builds a rename script first. At the end of processing this script is listed - so you can check what will be renamed first. It should look like this:
#!/bin/sh
mv “path/to/45thefile.wav” “path/to/thefile.wav”
mv “another/path/to/56thefile.wav” “another/path/to/thefile.wav”
…
If you’re satisfied - just enter ‘renameall.command’ + return
Hope that helps …
D.