Please Help with replace files

I have the following problem:

I have a Folder witch contains subfolder.
Th structure is like this:
Volumes/Images/12/121, 122, 123 and so on
Volumes/Images/13/131, 132, 133 and so on
and so on

In these subfolder are images called for example :
12345678_Landscape_L.jpg or 12345678-10_Landscape_L.jpg (the “L” stands for Large I also have “M” and “S”)
The first three digits in the name are the same as the Subfolder name

I frequently get a lot of new Images with the same name but now - for example - the image has been made in Portrait dimensions so the name will become:
12345678_Portrait_L.jpg or 12345678-10_Portrait_L.jpg

What I would like is to have a script that:
Select a Folder by choice containing the new Images
Check if - for example - there already is an Image witch name begins with: 12345678 in Volumes/Images/
Move this Image to a Folder called Volumes/Images_old
Move the new file to the correct Folder EXAMPLE: 12345678 will go to Volumes/Images/12/123

Can this be done?
I really hope so because this goes way beyond my scripting capabilities!
And the script would save me a huge amount of work

Any input is greatly appreciated!!

–Peter–

Hi retepp,

Maybe the following code is a good starting point for you. I wrote it according to your requirements, but it may need further customization:


on run
	set chosenfolder to choose folder
	tell application "Finder"
		set imgfiles to every file in chosenfolder
	end tell
	repeat with imgfile in imgfiles
		set imgfile to imgfile as Unicode text
		set fileinfo to info for (imgfile as alias)
		set filename to name of fileinfo
		if not (length of filename) < 8 then
			set fnamepartone to ((characters 1 through 2 of filename) as Unicode text)
			set fnameparttwo to ((characters 1 through 3 of filename) as Unicode text)
			set fnamepartthree to ((characters 1 through 8 of filename) as Unicode text)
			set folderpath to "Images:" & fnamepartone & ":" & fnameparttwo & ":"
			try
				set folderalias to folderpath as alias
			on error
				do shell script "mkdir -p " & quoted form of ("/Volumes/Images/" & fnamepartone & "/" & fnameparttwo & "/")
			end try
			tell application "Finder"
				set matchfiles to (every file in (folderpath as alias) whose name begins with fnamepartthree)
				if matchfiles is {} then
					move (imgfile as alias) to (folderpath as alias)
				else
					try
						move (imgfile as alias) to ("Images:Images_old:" as alias)
					end try
				end if
			end tell
		end if
	end repeat
end run

If you have questions about the code, please do not hesitate to ask me.

Thanks soo far but I have a few questions.

The script expects the new files to be in the folder Images/xx/xxx but I have them in a Folder whatevername and no subfolder referring to the name of the file.
It works when the the right folder and subfolder are in the choosen folder.
Option one would be the simplest for me though.
When changing “Landscape” to Portrait the existing file in Volumes/Images is not being moved to Images_old and the new file is not being moved to the right directory (Volumes/Images/xx/xxx)

The script asks you to choose a folder containing your new image files right at the start. So I do not exactly understand you :frowning:

You are right. Now I added some more code to move existing images to Images_old and to move new images to the corresponding folder. Please note, that the script currently does not check if Images_old already contains an existing image file with the same name.


on run
	set chosenfolder to choose folder
	tell application "Finder"
		set imgfiles to every file in chosenfolder
	end tell
	repeat with imgfile in imgfiles
		set imgfile to imgfile as Unicode text
		set fileinfo to info for (imgfile as alias)
		set filename to name of fileinfo
		if not (length of filename) < 8 then
			set fnamepartone to ((characters 1 through 2 of filename) as Unicode text)
			set fnameparttwo to ((characters 1 through 3 of filename) as Unicode text)
			set fnamepartthree to ((characters 1 through 8 of filename) as Unicode text)
			set folderpath to "Images:" & fnamepartone & ":" & fnameparttwo & ":"
			try
				set folderalias to folderpath as alias
			on error
				do shell script "mkdir -p " & quoted form of ("/Volumes/Images/" & fnamepartone & "/" & fnameparttwo & "/")
			end try
			tell application "Finder"
				set matchfiles to (every file in (folderpath as alias) whose name begins with fnamepartthree)
				if matchfiles is {} then
					move (imgfile as alias) to (folderpath as alias)
				else
					try
						repeat with matchfile in matchfiles
							move matchfile to ("Images:Images_old:" as alias)
						end repeat
						move (imgfile as alias) to (folderpath as alias)
					end try
				end if
			end tell
		end if
	end repeat
end run

Martin,

What I mean is this:

I gives me an Applescript-error "File ‘Images:10:101 cannot be found’ see

set matchfiles to (every file in (folderpath as alias)

It fails on the 'alias part
The chosenfolder is called ‘new’ on my Mac without subfolders
The Destination is Volumes/Images/Subfolders called (in this case 10/101)