I’m a total script noobie and I could really use some help creating a script to move files to existing folders by name.
For example I want to move 123.jpg, 123_A.jpg, 123_randomtext,jpg to an existing subfolder called “print” in an existing folder named “123_somerandomtext”
The first section is a variable it will always be start with a number and end with an underscore but the number of digits changes and it may be followed with a letter.
I found a script that almost works by cwtnospam but I don’t know enough to know how to modify it. Any help modifying it would be much appreciated.
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to (every file in thisfolder) as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set n to name of eachfile
set w1 to text item 1 of n
set thecommand to "echo " & n & " | perl -pe 's/[0-9]/\\n/g'"
set thefoldername to paragraph 1 of (do shell script thecommand)
set wdcount to count of words in thefoldername
if wdcount > 1 then
set thefoldername to words 1 thru -2 of thefoldername as text
set thefoldername to (do shell script "echo " & thefoldername & " | perl -pe 's/\\./ /g'") as text
else
if thefoldername = "" then
set thefoldername to w1
else
set thefoldername to word 1 of thefoldername
end if
end if
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
move eachfile to thisfolder & thefoldername as alias
end repeat
set AppleScript's text item delimiters to tid
end tell
Thanks for the reply. I am trying to move the file by a variable. I have files with names like 123, 223, 323 and I would like to move them to folders called 123, 223, 323.
try this, the folders 123_somerandomtext will be created if they don’t exist
property someRandomText : "someRandomText"
property printFolder : "/Users/myUser/Desktop/Print/" -- POSIX path to the folder "Print", the slash at the end is important
set baseFolder to choose folder
tell application "Finder" to set fileList to files of baseFolder
repeat with aFile in fileList
set itemFolder to getPrefix(name of aFile, name extension of aFile) & "_" & someRandomText
set destination to printFolder & itemFolder & "/" & name of aFile
do shell script "/usr/bin/ditto " & quoted form of POSIX path of (aFile as text) & space & quoted form of destination
do shell script "/bin/rm " & quoted form of POSIX path of (aFile as text)
end repeat
on getPrefix(aName, anExtension)
set {TID, text item delimiters} to {text item delimiters, "_"}
if (count text items of aName) > 1 then
set prefix to text item 1 of aName
else
set prefix to text 1 thru ((get offset of "." & anExtension in aName) - 1) of aName
end if
set text item delimiters to TID
return prefix
end getPrefix
Thanks! that’s almost what I’m looking for. The script moves all the files to folders named 123_somerandomtext even if a folder with the 123_ prefix already exists in the destination. Is there a way to get it to move to an existing folder?
this is a version which looks for an existing folder starting with the given prefix
set baseFolder to choose folder with prompt "choose source folder"
set printFolder to choose folder with prompt "choose folder \"Print\""
-- or hard-coded
-- set printFolder to alias "MacHD:Users:myuser:path:to:Print:"
tell application "Finder" to set fileList to files of baseFolder
repeat with aFile in fileList
set prefix to getPrefix(name of aFile, name extension of aFile)
tell application "Finder"
try
set destinationFolder to (1st folder of printFolder whose name begins with prefix)
move aFile to destinationFolder
end try
end tell
end repeat
on getPrefix(aName, anExtension)
set {TID, text item delimiters} to {text item delimiters, "_"}
if (count text items of aName) > 1 then
set prefix to text item 1 of aName
else
set prefix to text 1 thru ((get offset of "." & anExtension in aName) - 1) of aName
end if
set text item delimiters to TID
return prefix
end getPrefix