Compare and Move - Sync, looking for script

Hi all of you,

I kindly ask for help. I need script which will do following:

I have two Folders: DPX and Watchman

In DPX there is files named as this A008_C001_0701XU_01291.dpx

In Watchman folders are subfolders and files like this:
Watchman >
>Season 1
>Season 2
> Scene 25/1
>Season 3

I need to take the DPX file, find the same MOV file according to first 16 letters from filename and move it to the same sub-directory as the MOV file is.

Or better, take the DPX file, find the matching MOV file, read the SUB-Directories it’s in (Season, Scene) and re-create the same subdirectories in DPX folder and move the DPX file to there.

Thanks for help!

Hi issic,

The following script is a first shot at your problem, maybe it already points you in the right direction. It requires Mac OS X 10.5 and you will need to adjust the folder paths according to your own settings:

property dpxfolderpath : "DandyDisk:Users:martin:Desktop:DPX:"
property wmfolderpath : "DandyDisk:Users:martin:Desktop:Watchman:"

on run
		tell application "System Events"
			set dpxfiles to every file in (dpxfolderpath as alias) whose name ends with ".dpx"
		end tell
		repeat with dpxfile in dpxfiles
			set dpxfileinfo to (info for dpxfile)
			set dpxfilename to (name of dpxfileinfo)
			set usoffset to offset of "_" in ((reverse of (characters 1 through -1 of dpxfilename)) as text)
			set filenamepart to ((characters 1 through -(usoffset + 1) of dpxfilename) as text)
			set command to "mdfind -onlyin " & quoted form of POSIX path of wmfolderpath & " \"kMDItemFSName == '*" & filenamepart & "*'\""
			set output to (do shell script command)
			if output is not "" then
				set wmfilepath to ((POSIX file output) as text)
				set foldernames to {}
				repeat 2 times
					set parentfolderpath to my getparentfolderpath(wmfilepath)
					set foldername to name of (info for (parentfolderpath as alias))
					set foldernames to foldernames & foldername
					set wmfilepath to parentfolderpath
				end repeat
				set newsubfolderpath to ((POSIX path of dpxfolderpath) & (item 2 of foldernames) & "/" & (item 1 of foldernames) & "/")
				set command to "mkdir -p " & quoted form of newsubfolderpath
				do shell script command
				set command to "mv -n " & quoted form of POSIX path of (dpxfile as alias) & " " & quoted form of (newsubfolderpath & dpxfilename)
				do shell script command
			end if
		end repeat
	on error errmsg number errnum
		tell me
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
		end tell
	end try
end run

-- I am returning the parent folder path of a given item path
on getparentfolderpath(itempath)
	set olddelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set itemcount to (count text items of itempath)
	set lastitem to the last text item of itempath
	if lastitem = "" then
		set itemcount to itemcount - 2 -- folder path
		set itemcount to itemcount - 1 -- file path
	end if
	set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
	set AppleScript's text item delimiters to olddelims
	return parentfolderpath
end getparentfolderpath

You just saved me a lot of trouble! Thank you! Thank you! :D:D:D

It worked flawlessly It just returned this error.

File Backup
:Volumes:Backup HD:Watchman:4.dil:4-45: wasn’t found.
(-43)" buttons {“OK”} default button 1 with icon stop giving up after 30
{button returned:“OK”, gave up:false}

But that was because duplicated files in two subdirectories, so by-product of this was that showed duplicated files.


You are king!


A bit late, but here’s a slightly more compact offering. I’ve only been able to test it in Jaguar so far, but I should think it’ll be OK with later systems too:

-- This assumes that the DPX & Watchman folders are on the desktop. Adjust to circumstances.
set desktopPath to (path to desktop as Unicode text)
set DPXFldrPath to POSIX path of (desktopPath & "DPX:")
set WatchmanFldrPath to POSIX path of (desktopPath & "Watchman:")

set hierarchyOffset to (count WatchmanFldrPath) + 1 -- A known offset into the path of each movie file.
set qtdWatchmanFldrPath to quoted form of WatchmanFldrPath -- To save repeating this in the repeat.

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/" as Unicode text

set DPXPaths to paragraphs of (do shell script ("find " & (quoted form of DPXFldrPath) & " -maxdepth 1 -name '*.dpx'"))
	repeat with thisDPXPath in DPXPaths
		set basename to text 1 thru 16 of text item -1 of thisDPXPath
		set moviePath to (do shell script ("find " & qtdWatchmanFldrPath & " -name " & (quoted form of (basename & "*.mov")) & "  -print"))
		set hierarchyInsert to text from hierarchyOffset to text item -2 of moviePath
		set newDPXHierarchy to quoted form of (DPXFldrPath & hierarchyInsert)
		do shell script ("mkdir -p " & newDPXHierarchy & "; mv " & (quoted form of thisDPXPath) & " " & newDPXHierarchy)
	end repeat
on error
	display dialog "An error occurred while handling the base name: " & basename buttons {"OK"} default button 1 with icon stop giving up after 30
end try

set AppleScript's text item delimiters to astid