I’m in need of some serious help. I’m looking for a script that can help automate a few tedious task.
I have a parent folder, FILES and have hundreds of subfolders with unique numbered names (001, 005, 012, 99012, etc…) Now in the parent folder I’ve exported lots of files that I need to be sorted into the correct folder. For example file 001_august.pdf needs to be placed in folder 001 and file 99012_september.pdf needs to be moved to folder 90012. Now all the files are pdf and always begin with the folder number. PLEASE HELP!
marc
I got it. just hold a sec
let me know how this works
set the source_folder to choose folder with prompt "Folder containing items to edit:"
display dialog "What is the separator?" default answer "_"
set pp to POSIX path of source_folder
tell application "Finder"
set filelist to every item of source_folder whose kind is not "Folder"
repeat with i from 1 to number of items in filelist
set nom to name of (item i of filelist)
set bn to my textToList(nom, "_")
try
do shell script "mkdir \"" & pp & (item 1 of bn) & "\""
on error
--it is there already
end try
do shell script "mv \"" & (pp & nom) & "\" \"" & pp & (item 1 of bn) & "/" & nom & "\""
end repeat
end tell
display dialog "Done"
on textToList(theText, theSep)
set soFar to {}
set textSoFar to theText
repeat until theSep is not in textSoFar
set thePos to the offset of theSep in textSoFar
set thenewPos to thePos
if thenewPos is 1 then set thenewPos to 2
set nextBit to text 1 through (thenewPos - 1) of textSoFar
if textSoFar is not theSep then
set textSoFar to text (thePos + (count of text items in theSep)) through -1 of textSoFar
copy nextBit to the end of soFar
else
set textSoFar to ""
end if
end repeat
copy textSoFar to the end of soFar
return soFar
end textToList
Although I agree that doing the moves via shell scripting is much, much faster, I believe that using the [word] command in Applescript is simpler to actually generate the command variables for the file moving:
set Base_folder to choose folder
set bf_text to Base_folder as Unicode text
tell application "Finder"
set scat_files to the name of every file in folder Base_folder whose name extension is "pdf"
repeat with mf from 1 to (count scat_files)
set n_fold to (bf_text & word 1 of item mf of scat_files)
set m_file to (bf_text & (item mf of scat_files))
move file m_file to folder n_fold--This could easily be re-written as a shell script
end repeat
end tell
This is a quick and dirty script that should work, although I think having some try blocks and a command to checkk to see if the files is already there are great ideas. However, as long as each filename has the underscore (_) character in the correct place, this will work to grab the first word, and then tack it onto the bf_text variable, quickly and simply creating the correct location to move the file.
casdvm
Hey Craig this is the exact script I’m looking for to use at work!
set Base_folder to choose folder
set bf_text to Base_folder as Unicode text
tell application “Finder”
set scat_files to the name of every file in folder Base_folder whose name extension is “pdf”
repeat with mf from 1 to (count scat_files)
set n_fold to (bf_text & word 1 of item mf of scat_files)
set m_file to (bf_text & (item mf of scat_files))
move file m_file to folder n_fold–This could easily be re-written as a shell script
end repeat
end tell
I was wondering if this could be tweaked a bit so the file names can also recognize the name before a “.” and move it to the corresponding folder? I also tried to add (with no success) more name extensions: “ai” “eps” “jpg” “jpeg” “psd” “gif”
Ultimately I would like to make this script a droplet and just simply drag the files/folder and wahlah! the files are in the correct folders…
Thanks!
Ryan:
You indicated that your desktop has a folder named “1” that contains all the folders and files that need sorting into their proper folders. This script should run just fine all by itself, so long as all the folders are already there. For the future, it may be nice to add the functionality of creating a folder that is not already there, depending on your workflow requirements.
When each filename is analyzed, it only checks the first 2 “words” of the filename to find the right folder. For instance, if you have a folder named “2514_abf.jkl”, any file with 2514 and abf will placed into that folder. I could not think of a way to check for all three words in those cases where your folders have all three words. I hope this is acceptable.
set work_folder to ((path to desktop as Unicode text) & "1:")
set ext_list to {"eps", " jpg", "jpeg", "tiff", "tif", "psd", "ai", "htm", "html", "pdf", "gif"}
tell application "Finder"
set fold_names to the name of every folder of folder work_folder
set sort_Files to every file of folder work_folder
end tell
repeat with a_file in sort_Files
if ext_list contains (name extension of (info for a_file)) then
repeat with fol_der in fold_names
if fol_der contains (word 1 in (name of (info for a_file))) and fol_der contains (word 2 in (name of (info for a_file))) then --
tell application "Finder" to move file a_file to folder (work_folder & fol_der)
end if
end repeat
end if
end repeat
I eagerly await your report.
Ryan:
OK, I guess the [info for] does not work in this fashion. Sorry about that, I cannot explain why. Using the Finder, however, seems to do the trick:
set work_folder to ((path to desktop as Unicode text) & "1:")
set ext_list to {"eps", "jpg", "jpeg", "tiff", "tif", "psd", "ai", "htm", "html", "pdf", "gif"}
tell application "Finder"
set fold_names to the name of every folder of folder work_folder
set sort_Files to every file of folder work_folder
end tell
repeat with a_file in sort_Files
tell application "Finder" to set {a_Ext, a_Nam} to {name extension of a_file, (name of a_file as string)}
if ext_list contains a_Ext then
repeat with fol_der in fold_names
if fol_der contains (word 1 in a_Nam) and fol_der contains (word 3 in a_Nam) then --
tell application "Finder" to move a_file to folder (work_folder & fol_der)
end if
end repeat
end if
end repeat
Please let me know how this functions.
Hey Craig the script works awesome! The only drawback is files
that have a “.” and “-” are not getting recognized to move to their
folders?
Craig did note: (_) is a word, while a dash (-) is a space. Similarly, a period (.)
is a character. Since all your folders and filenames use them rather
willy-nilly (no obvious pattern), it is difficult to cover all the
bases.
I was wondering if anyone knows how to go about to get the files to be moved to the correct folders?
Ex. 3503530-AR1.eps to folder 3503530-AR1
Ex. 35030960.AR4.eps to folder 35030960.AR4
Thanks!
Ryan