how to find files with same name but different extension

Hallo,
I have photos in two formats RAW & JPEG. ( eg DSC1000.CR2 & DSC1000.JPG)These are stored on a daily basis in different folders. I check the JPEG folder and select certain files to delete and put them in the parent folder. ( Thus the day´s folder contains some loose jpegs and one folder for RAWS and one folder for jpegs) I´m trying to write a script which will find the RAWS corresponding to the JPEGS and put them in a new folder. I can get a list of the jpegs in the selected folder but I cannot work out how to query the raws folder.
TIA
Martin

this is what I tried

tell application "Finder"
activate
set folderSelected to (choose folder with prompt "choose the daily folder")
set badfold to make new folder at folderSelected with properties {name:"BADRAWS"}
set goodraws to folder of folderSelected whose name is "03RAWS"
set fileExtension to "jpg"
set badjpegs to name of every file of folderSelected whose name extension is fileExtension as text
set badjpegs to badjpegs as list
set badraws to name of every file of goodraws whose name is in badjpegs
set files2Move to badraws
move files2Move to badfold
reveal badfold
end tell

EMAC, Mac OS X (10.4.8)

Posted on Feb 17, 2007 9:51 AM

Reply
8 replies

Feb 17, 2007 4:06 PM in response to softmartin

You're a little long-winded here, but you're close. Your problem is that you're running the statement:

<pre class=command>set badraws to name of every file of goodraws whose name is in badjpegs</pre>

This works fine, except it's just a list of file names (e.g. {"DSC1000.JPG", "DSC1002.DSC"}).
You then tell the Finder to move this list of file names which it can't do - there's no context - where should it find these files?

Instead, eschew the file names - you don't need the name when you have a reference to the files themselves, so instead of:

<pre class=command>set badjpegs to name of every file of folderSelected whose name extension is fileExtension as text
set badjpegs to badjpegs as list
set badraws to name of every file of goodraws whose name is in badjpegs
set files2Move to badraws
move files2Move to badfold</pre>

You can just say:

<pre class=command>set badjpegs to every file of folderSelected whose name extension is fileExtension
move badjpegs to badfold</pre>

Since the second example asks the Finder for the matching files, not just the matching file names, you can move them directly.

Feb 18, 2007 12:17 AM in response to softmartin

Hello

If I'm not mistaken about your intentions, the following script may help.

SCRIPT1 is a simple modification of your original script. It is NOT TESTED, for 'displayed name' and 'name extension' is for OSX's Finder only and I don't use OSX myself. I think, however, it may work if you have set Finder not to show the name extensions so that 'displayed name' becomes the name without extension.

SCRIPT2 is my code. It is TESTED under OS9.1.

Good luck.
H


-- SCRIPT1
tell application "Finder"
activate
set folderSelected to (choose folder with prompt "choose the daily folder")
try
set badfold to folder ("" & folderSelected & "BADRAWS:")
on error
set badfold to make new folder at folderSelected with properties {name:"BADRAWS"}
end
set goodraws to folder ("" & folderSelected & "03RAWS:")
set fileExtension to "jpg"
set badjpegs to (displayed name of every file of folderSelected whose name extension is fileExtension) as list
set badraws to every file of goodraws whose displayed name is in badjpegs
move badraws to badfold
reveal badfold
end tell
-- END OF SCRIPT1


-- SCRIPT2
(*
Move raw files in RAWS which corresponds to jpeg files loose in DAILY to BADRAWS.

* Initial state e.g.:

DAILY:
002.jpg
003.jpg
JPEGS:
001.jpg
004.jpg
RAWS:
001.cr2
002.cr2
003.cr2
004.cr2
BADRAWS:


* Goal state e.g.:

DAILY:
002.jpg
003.jpg
JPEGS:
001.jpg
004.jpg
RAWS:
001.cr2
004.cr2
BADRAWS:
002.cr2
003.cr2
*)
main()
on main()
script o
property RAWS_ : "03RAWS" -- RAWS folder name
property BADRAWS_ : "BADRAWS" -- BADRAWS folder name
property fdDAILY : choose folder with prompt "Choose the daily folder."
property nn1 : {}
property nn2 : {}
property mm : {} -- list of name (without extension) of jpeg files loose in fdDAILY.
property aa : {} -- list of raw files to be moved.

-- preparation
set fdpRAWS to "" & fdDAILY & RAWS_ & ":"
set fdRAWS to fdpRAWS as alias
try
set fdBADRAWS to ("" & fdDAILY & BADRAWS_ & ":") as alias
on error
tell application "Finder" to ¬
set fdBADRAWS to (make new folder at fdDAILY with properties {name:BADRAWS_}) as alias
end try

-- get alias list of raw files in fdRAWS corresponding to jpeg files loose in fdDAILY
set nn1 to list folder fdDAILY without invisibles
set nn2 to list folder fdRAWS without invisibles
repeat with n in my nn1
set n to n's contents
if n ends with ".jpg" then set end of my mm to (n's text 1 thru -5) -- name without extension
end repeat
repeat with n in my nn2
set n to n's contents
set m to getNameWithoutExt(n)
if m is in my mm then set end of my aa to (fdpRAWS & n) as alias
end repeat

-- move aa (= obtained alias list of raw files) to fdBADRAWS
if my aa is not {} then
tell application "Finder" to move my aa to fdBADRAWS with replacing
end if
end script
tell o to run
end main

on getNameWithoutExt(n)
(*
text n: name of item
return text : item name without name extension
*)
local astid, astid0, nn, m
set astid to a reference to AppleScript's text item delimiters
try
set astid0 to astid's contents
set astid's contents to {"."}
set nn to n's text items
if (count nn) = 1 then
set m to n
else
set m to "" & nn's text items 1 thru -2
end if
set astid's contents to astid0
on error errs number errn
set astid's contents to astid0
error "getNameWithoutExt(): " & errs number errn
end try
return m
end getNameWithoutExt
-- END OF SCRIPT2



Mac OS 9.1.x

Feb 18, 2007 4:40 AM in response to softmartin

Hallo,
Camelot & Hiroto : many thanks for your help so far. Hiroto´s description of my problem is accurate. I tried Script1 but moving the RAWS didn´t happen. In my finder preferences the extensions are not shown. I put in a test dialog to tell me what the displayed names are and they are identical ( ie displayed name for DSC1000.jpg is DSC1000 and for DSC1000.CR2 in the RAWS folder is also DSC1000
I´d be very appreciative of further suggestions.
Thank you
Martin

tell application "Finder"
activate
set folderSelected to (choose folder with prompt "choose the daily folder")
try
set badfold to folder ("" & folderSelected & "BADRAWS:")
on error
set badfold to make new folder at folderSelected with properties {name:"BADRAWS"}
end try
set goodraws to folder ("" & folderSelected & "03RAWS:")
set fileExtension to "jpg"
set badjpegs to (displayed name of every file of folderSelected whose name extension is fileExtension) as list
(* Hirotos script up to here; now come my tests*)

set testit to first item of badjpegs as string
display dialog testit
set testraws to (displayed name of every file of badfold) as list
set testraw to first item of badjpegs as string
display dialog testraw
(*end of tests*)
set badraws to every file of goodraws whose displayed name is in badjpegs
move badraws to badfold
reveal badfold
end tell

Feb 18, 2007 6:31 AM in response to softmartin

Hallo,
it´s much more complicated than I thought.
In my previous post I realise that my test routine was written wrong.
But in the meantime I have managed to get the script to work as written. The problem lies in the displayed name.
The photos I get from the camera have names like DSC1000.jpg, DSC100.CR2 and this is the name that Finder shows me even when Finder´s preferences "Show all file extensions" is not set.
When I manually delete the ".jpg" in Finder then the script will work. So I now need to script a way to delete the ".jpg" before I run the script that I now have. I tried renaming the files with Automator but this led to the files no longer being recognised as jpegs and the script failing.
Given these problems with the displayed names, maybe it would be better to check the displayed names for the presence of the extension, reduce the name to its basic form and search a list of the raw folder files without extensions. ( I think this is the approach Hiroto uses in Script2, but I don´t know how to do this in OSX)
TIA
Martin

Feb 18, 2007 10:51 AM in response to softmartin

You can rebuild badjpegs with files name without extension, try:

set badjpegs to (displayed name of every file of folderSelected whose name extension is fileExtension) as list
set revised_badjpegs to {}
set AppleScript's text item delimiters to {".jpg"}
repeat with _item in badjpegs
set _item to text item 1 of _item
copy _item to end of revised_badjpegs
end repeat
set AppleScript's text item delimiters to {""}
set badjpegs to revised_badjpegs -- now badjpegs contain files name without extension

Feb 18, 2007 11:29 AM in response to softmartin

Hello Martin,

Firstly, I think SCRIPT2 will work fine under both OS9 and OSX. I meant that it is only tested under OS9, not that it only works under OS9 😉

Anyway, here's a revised script along with your original line. It no longer relies upon 'displayed name', while it assumes that your raw files' extension is always "CR2".
(Again not tested here.)

Good luck,
H

-- SCRIPT1A
main()
on main()
tell application "Finder"
activate
set folderSelected to (choose folder with prompt "choose the daily folder")
try
set badfold to folder ("" & folderSelected & "BADRAWS:")
on error
set badfold to make new folder at folderSelected with properties {name:"BADRAWS"}
end
set goodrawsFold to folder ("" & folderSelected & "03RAWS:")
set jpgExt to "jpg"
set rawExt to "CR2"
set badjpegs to (name of every file of folderSelected whose name extension is jpgExt) as list

-- # added part
(* Note that it is assumed that raw file's extension is always rawExt *)
set badraws to {} -- list of name of raw files corresponding to jpeg files of which name is in badjpegs
set jpgExtLen to jpgExt's length
repeat with n in badjpegs
set end of badraws to (n's text 1 thru (-jpgExtLen -1)) & rawExt
end

set badrawsFiles to every file of goodrawsFold whose name is in badraws
move badrawsFiles to badfold with replacing
reveal badfold
end tell
end
-- END OF SCRIPT1A


Mac OS 9.1.x

Feb 18, 2007 12:37 PM in response to softmartin

property source_Folder : "03RAWS" -- Source of all files.
property bad_Folder : "BADRAWS" -- Destination of '.jpg' files.

set selected_Folder to (choose folder with prompt "choose the daily folder") -- Locate parent folder of 'source_Folder'.

set BADRAWS_Folder to ((selected_Folder as string) & bad_Folder) -- Create a string of 'bad_Folder's path.
tell application "Finder" to if (not ((folder BADRAWS_Folder) exists)) then set BADRAWS_Folder to make new folder at selected_Folder with properties {name:bad_Folder} -- Determine if 'bad_Folder' exists, if not - create it.
set BADRAWS_Folder to BADRAWS_Folder as alias -- Coerces 'BADRAWS_Folder' to a pointer.

try -- Used to trap any 'every file of entire contents of folder' exceptions.
-- Create a list of all files, in 'source_Folder', with a '.jpg' extension.
tell application "Finder" to set tFiles to every file of entire contents of folder (((selected_Folder as string) & source_Folder)) whose name extension is "zzz"
end try

try -- Used to trap any 'move' exceptions. Such as when 'tFiles' is empty.
tell application "Finder" to move tFiles to BADRAWS_Folder -- Move all'.jpg' files to 'BADRAWS_Folder'.
end try

-----

Naturally, the two (2) 'properties' lines can be removed if you find and replace 'source_Folder' with '"03RAWS"', and 'bad_Folder' with '"BADRAWS"' (including the double quote marks).

Mac OS X (10.4.6)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to find files with same name but different extension

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.