Move file based on part of filename

Hi,

I’ve looked around on this great forum but couldn’t find exactly I was looking for. The case is this:
A receive folder gets files with names like “AA_xxx_0010c_yyyy.pdf” this file has to be moved to another folder named “AA_0010”. An incoming file with a name like BB_xxx_0001c_yyyy.pdf" has to be moved to folder “BB_0001”.
The receiving folder is on a Mac OSX macine and the folders to be moved to are on a Windows XP machine.

If possible it could be use it as a folderaction?

Can anyone help me out on this one?

Thanks in advance!

MacBert

Yes it is possible and also very simple to do.

Are your using smb protocol for connection to Windows?

You can do this by folder action if you like but also do by hand.

If you do it by hand it can simply be done like (i’m at a Window machine so couldn’t test my code but gives you an idea of how you can do it)

set theSourceFolder to "path/to/folder/with/items/"
set theTargetRoot to "/Volumes/WindowVolume/path/to/folder/with/items/"
set theItems to list folder (posix file theSourceFolder as string) without invisibles

repeat with thisItem in theItems
set targetParentFolderName to characters 1 thru 2 of thisItem & "_" & characters 8 thru 11 of thisItem as string
do shell script "mv " & quoted form of (theSourceFolder & thisitem) & " " & quoted form of (theTargetRoot & targetParentFolderName & "/" & thisItem as string)
end

Hi DJ,

Thank you for the quick answer. It works perfect.
I forgot one thing actually, not all names are made in the mentioned format. There are some files with additional numbers at the end. Is it possible to do something with that also.

Is it also possible to ignore the first 2 characters (AA)?

For instance: “AA_xxx_0001k_yyyy_01.pdf” should be placed in a folder that contains xxxx0001k_01?

And is it possible to pass files in to a predefined folder like files containing “AA_” should be placed in folfer “AA” and files containing BB_ and 0010k should be placed in folder “0010k_CC”?

I’m not an pro Applescripter, not even a amateur so some help is appreciated if I don’t ask to much…

Thanks in advance!

MacBert

Hi Bert,

You mean something like this?

set theSourceFolder to "path/to/folder/with/items/"
set theTargetRoot to "/Volumes/WindowVolume/path/to/folder/with/items/"
set theItems to list folder (posix file theSourceFolder as string) without invisibles

repeat with thisItem in theItems
if length of thisItem = 21 then --no additional numbers in file names
set targetParentFolderName to characters 1 thru 2 of thisItem & "_" & characters 8 thru 11 of thisItem as string
else
set targetParentFolderName to characters 8 thru 12 of thisItem & "_" & characters -7 thru -5 of thisItem as string
end if
do shell script "mv " & quoted form of (theSourceFolder & thisitem) & " " & quoted form of (theTargetRoot & targetParentFolderName & "/" & thisItem as string)
end