You are not logged in.
Hello,
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.
Applescript:
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
Offline
It's not clear to me exactly what you're trying to do, but it seems like the following might be of use you:
Moves a file from folder x to folder y:
Applescript:
tell application "Finder"
move file "HD:Users:Desktop:y:file.txt" to "HD:Users:Desktop:x"
end tell
Moves the selected object in finder to folder y:
Applescript:
tell application "Finder"
set theObjects to selection
move theObjects to "HD:Users:Desktop:Y"
end tell
Last edited by Ole (2012-02-24 09:37:27 pm)
Offline
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.
Offline
try this, the folders 123_somerandomtext will be created if they don't exist
Applescript:
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
Last edited by StefanK (2012-02-27 08:56:30 am)
Offline
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?
Offline
if you want only the prefix as folder name ("123_" instead of "123_someRandomText")
change this line
Applescript:
set itemFolder to getPrefix(name of aFile, name extension of aFile) & "_"
Offline
That's not quite it I would like it to move the file to an existing folder rather than creating a new one.
Offline
this is a version which looks for an existing folder starting with the given prefix
Applescript:
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
Offline
Wow That's perfect. Thank you so much! I have several thousand images to move and this will make things a lot easier.
Is there a way to move the files into a subfolder of the destination folder?
Offline
If the subfolder has always the same name
Applescript:
…
move aFile to folder "subFolder" of destinationFolder
…
Last edited by StefanK (2012-02-27 10:08:14 am)
Offline
I was so close. I was trying to figure it out on my own but I thought I had to specify that it was a sub folder. Thanks for all the help on this.
Offline
hmm I noticed a little glitch. I have a file name 1457.jpg and one called 1457F.jpg and it's putting both of them in the 1457F folder.
Offline
Can the "_" delimiter be used to differentiate between 1457_ and 1457F_?
Offline
of course, if the prefix ends always with an underscore you can use
Applescript:
set destinationFolder to (1st folder of printFolder whose name begins with (prefix & "_"))
Offline