replace files in (sub)folders -REPOST, PLEASE HELP!!!

Is it possible to:
Choose a Folder = Folder with new files
Choose a destinaton Folder = Folder with old files
Check if the files in the destination exist
if no just move the new files*
if yes, move these files in a subfolder of the destination Folder called “Old”
Move the new files to the location of the files that were in the destination (sub)folders*

The files are called:
12345678_Landscape_L.jpg and need to be relaced by 12345678_Portrait_L.jpg (M and S also exist for both files)
or
12345678-1_Landscape_L.jpg -------------------------------- 12345678-1_Portrait_L.jpg (M and S also exist for both files)
or
12345678-10_Landscape_L.jpg -------------------------------- 12345678-10_Portrait_L.jpg (M and S also exist for both files)

The variable is the part between the first and the second underscore.

*The Folders have a fixed structure: Volumes/Telescope/Productgroups/12/123 - the first two and three digits in the filename

– Peter –

This can become rather deeply recursive, Peter. If a file has been modified several times, then the “Old” subfolder may have a file with the same name as the destination folder, so we’d have to check whether a new file existed in Destination, then again whether it existed in Old, etc. If it did, do we create a folder “Older” in “Old”?

I don’t understand this line: “Move the new files to the location of the files that were in the destination (sub)folders*” The shortage of answers here is probably the result of not really understanding the question.

Hi Peter,

I agree to Adam, we need more information. Do you have any code yet?

Just for understanding:
You have a folder1 which has files in it whose names are 12345678_Landscape_L.jpg, an something like that.
And you have a folder2 with files named 12345678_Portrait_L.jpg and so on.
If in folder1 the filename is the same except for the middle part (Landscape or Portrait) the file in folder1 should be moved into subfolder “old” and the file of folder2 should be moved to folder1
Is that correct?

Some further questions:
Are the new files really new files or do you only want to rename the existing files?
Are you searching only the destination folder or also its subfolders (“old”)?
Are the filenames unique (e.g. if the name has a timestamp in it) or could it be, as Adam assumed, that there are multiple files with the same name?

Are the variable parts of the names always “landscape” and “portrait”, or do they differ?

greetz,
Wiggum

Hi Adam and Wiggum.
Thanks for your response.
Here are the anwers to your questions:

No code yet (Goes way beyond my scripting capabilities)

ust for understanding:
You have a folder1 which has files in it whose names are 12345678_Landscape_L.jpg, an something like that.
And you have a folder2 with files named 12345678_Portrait_L.jpg and so on.
If in folder1 the filename is the same except for the middle part (Landscape or Portrait) the file in folder1 should be moved into subfolder “old” and the file of folder2 should be moved to folder1
Is that correct?

This is correct, the files that need to be replaced are in a fixed folderstructure : Volumes/Telescope/Productgroups/12/123 - the first two and three digits in the filename

The new files are really new

No search in Folder “Old” (btw, the Folder doesn’t need to be a subfolder, it can be: Volumes/Telescope/Productgroups_Old

The filenames are Unique

The only difference between the filenames is Landscape - Portrait

Many thanks in advance!!!

–Peter–

To illustrate how you would change the names if you had a list of them:

-- As in illustration:
set tFiles to {"12345678_Landscape_L.jpg", "12345678-1_Landscape_L.jpg", "2345678-10_Landscape_L.jpg"}
set nFiles to {}
set tid to AppleScript's text item delimiters
repeat with F in tFiles -- you would handle them one at a time, I'm just repeating here to show that it works on all of them.
	set AppleScript's text item delimiters to "Landscape"
	set tParts to text items of F -- splits each name into a list of parts on both sides of "Landscape", e.g., {"12345678_", "_L.jpg"}
	set AppleScript's text item delimiters to "Portrait"
	set end of nFiles to tParts as text -- joins the parts by inserting "Portrait" where they were split
end repeat
set AppleScript's text item delimiters to tid
nFiles --> {"12345678_Portrait_L.jpg", "12345678-1_Portrait_L.jpg", "2345678-10_Portrait_L.jpg"}

Hi Adam,

thanks, but I don’t want to change the names.
I need to replace the file witch contains Landscape with the file that contains Portrait - or the other way around.

hi Peter,

Applescript is not that bad! Believe in yourself! Keep on coding! :wink:

here my suggestion for your problem. it’s not the complete program, 'cause you can also do your part (or at least try to) :wink:

-- set up empty list
set list1 to {}
set list2 to {}


tell application "Finder"
	set newfiles to (every file of (choose folder))
	set oldfiles to (every file of (choose folder))
	
	-- save delimiters
	set TID to AppleScript's text item delimiters
	-- change delimiters
	set AppleScript's text item delimiters to "Portrait"
	repeat with aFile in newfiles
		-- get the file's name
		set newfile_name to name of aFile
		-- cut out the word "Portrait", result is a list
		set aux to every text item of newfile_name
		-- save the lists in a list
		set end of list1 to aux
	end repeat
	
	-- change delimiters
	set AppleScript's text item delimiters to "Landscape"
	repeat with aFile in oldfiles
		-- get the file's name
		set oldfile_name to name of aFile
		-- cut out the word "Landscape", result is a list
		set aux to every text item of oldfile_name
		-- save the lists in a list
		set end of list2 to aux
	end repeat
	-- restore delimiters
	set AppleScript's text item delimiters to TID
	log list1
	log list2
	
	repeat with i from 1 to (length of list1)
		-- an indicator
		set found to false
		repeat with j from 1 to (length of list2)
			-- check if the names are the same in both folders (except from the landscape/portrait part which has been cutted out)
			if (item i of list1) is (item j of list2) then
				set found to true
			end if
		end repeat
		
		if found then
			display dialog "found=" & found & return & (item i of newfiles)
			-- enter move/copy commands here
		else
			display dialog "NOT found:  " & (item i of newfiles)
			-- enter move/copy commands her
		end if
	end repeat
end tell

Hope it helps you and it is what you were asking for.
I tried to continously comment the code but feel free to ask, if you don’t understand some code lines. But it is always a good idea to check some intermediate results by inserting a “log xy” or a “display dialog xy” at some key points.

Bye,
Clancy Wiggum

hoooo, looks like both Adam and me can help you out…

Ahh, but your original post didn’t contain the word “replace”.

Hi Wiggum,

Thanks a lot for your input!!
I will work on this next Monday.
Have a nice weekend.
I’ll get back to you for my results and probably for some more help.

–Peter–

Hi Clancy and Adam,

I tweaked the script a little bit:

set oldfiles to (every file of entire contents of (choose folder with prompt "Choose Destination Folder"))

and:

if found then
			display dialog "found=" & found & return & (item i of newfiles) & (item i of oldfiles)
			tell application "Finder"
				move item i of newfiles to folder of item i of oldfiles
				move item i of oldfiles to folder "Volumes:TS-Leenbakker:JPEG_OLD:"

Now it takes in account all the files in the main folder
It moves the new file and after that moves the old file

What I would like is to have a move function for all the images that are really new. It has to come after the first check/move. (else)
Th files have to go to a specific Folder for example: TS-Leenbakker:JPEG:10:101:
The Folders represent the first two and three digits in the name

I have been playing around with this:

set chosenfolder to choose folder
tell application "Finder"
	set newfiles to every file in chosenfolder
end tell
repeat with aFile in newfiles
	set filename to name of aFile
	set aFile to aFile as Unicode text
	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 new_folderpath to "TS-Leenbakker:JPEG:" & fnamepartone & ":" & fnameparttwo & ":"
	tell application "Finder"
		move aFile to folder new_folderpath
	end tell
end repeat

But I don’t know how to get this in my script.
Or is there perhaps a better way to de this?

By the way, all the new files are always in a Folder on our server called “Volumes:TS-Leenbakker:New” and the desination is always “Volumes:TS-Leenbakker:JPEG” (including all subfolders) so choosing the Folders is not really neccesary.
Is this a clever approach, because there are about 30.000 files in the destination - it will take a lot of time to scan all the Folders.
Perhaps choosing a specific subfolder would be better after all (Volumes:TS-Leenbakker:10: and so on)

ONE BIG ISSUE: THE SCRIPT ONLY WORKS WHEN THE OLD FILE HAS “Landscape” AND THE NEW FILE HAS “Portrait” BUT IT CAN ALSO BE THE OTHER WAY AROUND!!!

Many thanks in advance,

– Peter –