Friday, July 25, 2014

#1 2012-02-24 02:44:06 pm

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

move file to folder by name

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

 

#2 2012-02-24 09:35:26 pm

Ole
Member
Registered: 2011-10-03
Posts: 38

Re: move file to folder by name

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

 

#3 2012-02-27 08:28:56 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

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

 

#4 2012-02-27 08:55:06 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

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)


regards

Stefan

Offline

 

#5 2012-02-27 09:22:40 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

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

 

#6 2012-02-27 09:28:36 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

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) & "_"


regards

Stefan

Offline

 

#7 2012-02-27 09:39:29 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

That's not quite it I would like it to move the file to an existing folder rather than creating a new one.

Offline

 

#8 2012-02-27 09:42:44 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

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


regards

Stefan

Offline

 

#9 2012-02-27 10:05:13 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

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

 

#10 2012-02-27 10:08:01 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

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)


regards

Stefan

Offline

 

#11 2012-02-27 10:21:22 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

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

 

#12 2012-02-27 11:18:23 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

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

 

#13 2012-02-27 11:23:42 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

That's the problem with "starts with". You need an unique way to distinguish the folders


regards

Stefan

Offline

 

#14 2012-02-27 11:29:29 am

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

Can the "_" delimiter be used to differentiate between 1457_ and 1457F_?

Offline

 

#15 2012-02-27 11:37:17 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10456
Website

Re: move file to folder by name

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 & "_"))


regards

Stefan

Offline

 

#16 2012-02-27 12:51:36 pm

Gleeptoid7
Member
Registered: 2012-02-24
Posts: 14

Re: move file to folder by name

Thanks

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.038 seconds, 12 queries executed ]

RSS (new topics) RSS (active topics)