Please help a noob with finding files in folder

I have constructed a loop similar to the one below. This is supposed to look in a folder for specifically named files but it is not working and goes into the error trap each loop. Seems something is wrong with my command “set otherFilePath to (path of otherFile)” that means even if the file is present, AS still doesn’t pick it up.


property hFolder : "Macintosh HD:Users:xyz:abc:efg:"
	tell application "Finder"
		set newFileList to (every file of folder hFolder)
			repeat with m from 1 to number of items in newFileList
				try
					set otherFile to hFolder & m & ".NEF" -- This constructs the file name
					set otherFilePath to (path of otherFile) -- This is supposed to check if the file is in hFolder
					-- Do Some Actions if file is in hFolder
				on error
					-- Do Some Actions if file is not in hFolder
				end try
			end repeat
	end tell

Many Thanks.

Model: IMac G5 (Gen 1)
Operating System: Mac OS X (10.4)

Hi,

try this


property hFolder : "Macintosh HD:Users:xyz:abc:efg:"

set newFileList to list folder hFolder without invisibles
repeat with m from 1 to number of items in newFileList
	if m & ".NEF" is in newFileList then
		-- Do Some Actions if file is in hFolder
	else
		-- Do Some Actions if file is not in hFolder
	end if
end repeat

Thanks Stefan

I am getting errors but I know this is because of how I simplified the original script I posted

With your suggestion, what I have is essentially…


property tFolder : "Macintosh HD:Users:xyz:abc:" -- can't change this as it is used by other parts of script
set hFolder to (make new folder at tFolder with properties {name:"efg"}) -- can't change this as well for same reasons

-- Now need to get the files.  Tx

set newFileList to list folder hFolder without invisibles
repeat with m from 1 to number of items in newFileList
	if m & ".NEF" is in newFileList then
		-- Do Some Actions if file is in hFolder
	else
		-- Do Some Actions if file is not in hFolder
	end if
end repeat

Thanks a mil.

list folder returns a list of all file names(!) in the specified folder.
So it’s quite easy to compare just the name.

I forgot also a number to text coercion


.
if (m as text) & ".NEF" is in newFileList then
.

I assumed so

Don’t know why but this line “set newFileList to list folder hFolder without invisibles” returns an error saying ‘Can’t make folder “efg” of folder “abc” of folder “xyz” of folder “Users” of startup disk into file type’.

Perhaps I need to take a break for a while and come back to it later.

It’ s quite difficult to give further help, when the script you’ve posted is not the same as your script you are working with.

list folder works with a string path like in the property hFolder, but not with a Finder file specifier.
You have to coerce the file specifier to a string path or to an alias.

PS: If you want to make a new folder, you have to wrap the make new folder line in a Finder tell block

Thanks for your help Stefan.

As a newbie, my script is, shall we say, rather inefficient and long so I tried to construct a simplified version of the relevant part.

In any case, I have solved the issue.

Here is the whole long script

It takes files from my cardreader when connected (using image capture) into a new folders in a specified directory and renames them according to my new numbering system which is enough to give me 45m short unique file names which should last me a lifetime.

It works so I am happy to have been able to put it together after about 4 days experience with AS.

I know it could be tightened up


property defaultFolder : "Macintosh HD:Users:Dayo:Pictures:MyImages:"
property nameString : "ZZ99ZZ"
property oldNameString : "ZZ99ZZ"
property lowerStr : "abcdefghijklmnopqrstuvwxyz"
property upperStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property theNums : "0123456789"
property rawExt : "NEF" -- TODO: Make into list of raw file extentions for flexibility 


display dialog "Ingest Files to Image Library?" buttons {"No", "Yes"} default button 2 -- Ask whether to import

if the button returned of the result is "Yes" then
	try -- begin authentication so that other users don't mess with files
		do shell script "echo ''" with administrator privileges
	on error
		return
	end try -- end authentication
	
	set defaultFolder to "Macintosh HD:Users:Dayo:Pictures:MyImages:" as alias -- begin get upload folder
	set homeFolder to choose folder with prompt "Select file" default location defaultFolder
	set homeFolder to homeFolder as string -- end get upload folder
	set FlashDisk to GetFlashDisk() -- begin load Flashdisk
	
	tell application "Finder"
		set cardFolder to folders of FlashDisk as alias
		set folderList to (every folder of folder cardFolder) -- end load Flashdisk
		set {year:y, month:m, day:d} to (current date) -- begin set default folder name for this day
		set theDate to (y * 10000 + m * 100 + d) as string
		set myCover to "000" as string
		repeat with x from 1 to 999
			set temp_name to theDate & "_" & myCover as string
			set test_path to homeFolder & temp_name & ":" -- end set default folder name for this day
			set test_result to my file_exists(test_path) -- begin check if folder exists.  if exists, increment suffix
			if test_result is true then
				set counter to x as string
				if x < 10 then
					set myCover to "00" & counter
				else
					if x < 100 then
						set myCover to "0" & counter
					end if
				end if
			else
				exit repeat
			end if -- end check if folder exists.  if exists, increment suffix
		end repeat
		
		set targetFolder to (make new folder at folder homeFolder with properties {name:temp_name}) -- begin moving files
		repeat with i from 1 to number of items in folderList
			set this_item to item i of folderList as string
			set fileList to (every file of folder this_item)
			repeat with j from 1 to number of items in fileList
				set this_file to item j of fileList
				move this_file to targetFolder
			end repeat
		end repeat -- end moving files
		
		set myNewFileList to (files of targetFolder as alias list) -- begin prep for rename
		set tFolder to targetFolder as alias
		set newfolderList to (every folder of folder homeFolder whose name contains temp_name)
		repeat with k from 1 to number of items in newfolderList
			set new_this_item to item k of newfolderList as string
			set newFileList to (every file of folder new_this_item)
			repeat with m from 1 to number of items in newFileList
				set newFile to item m of newFileList as string
				set {name:file_Nm, name extension:file_Ex} to info for file newFile
				set upperfile_Ex to my makeUpper(file_Ex) -- end prep for rename
				set newFileName to my rename_me() -- begin rename files
				set name of file newFile to newFileName & "00XX." & upperfile_Ex -- end rename files
				set onlyFileName to text 1 thru ((count file_Nm) - (count file_Ex) - 1) of file_Nm -- begin handle raw + jpeg
				set oFile to tFolder & onlyFileName & "." & rawExt as string
				try
					set otherFile to oFile as alias
					set nameString to oldNameString -- resets nameString so that Raw files are named the same as jpegs
				on error
					-- Do nothing with nameString so that it can be incremented to rename next jpeg 
				end try -- end handle raw + jpeg
			end repeat
		end repeat
	end tell
	--- TODO: Add to IView Catalog
end if


on rename_me()
	set mainList to every character of upperStr as list
	set numList to every character of theNums as list
	set tempList to every character of nameString as list
	set ItemA to item 1 of tempList
	set ItemB to item 2 of tempList
	set ItemC to item 3 of tempList
	set ItemD to item 4 of tempList
	set ItemE to item 5 of tempList
	set ItemF to item 6 of tempList
	
	
	set posA to my get_loc(ItemA, upperStr)
	set posB to my get_loc(ItemB, upperStr)
	set posC to my get_loc(ItemC, theNums)
	set posD to my get_loc(ItemD, theNums)
	set posE to my get_loc(ItemE, upperStr)
	set posF to my get_loc(ItemF, upperStr)
	
	if posF = 26 then
		set posF to 1
		if posE = 26 then
			set posE to 1
			if posD = 10 then
				set posD to 1
				if posC = 10 then
					set posC to 1
					if posB = 26 then
						set posB to 1
						if posA = 26 then
							set posA to 1
						else
							set posA to posA + 1
						end if
					else
						set posB to posB + 1
					end if
				else
					set posC to posC + 1
				end if
			else
				set posD to posD + 1
			end if
		else
			set posE to posE + 1
		end if
	else
		set posF to posF + 1
	end if
	
	
	set myA to item posA of mainList
	set myB to item posB of mainList
	set myC to item posC of numList
	set myD to item posD of numList
	set myE to item posE of mainList
	set myF to item posF of mainList
	
	
	set oldNameString to nameString
	set nameString to (myA & myB & myC & myD & myE & myF) as string
	return nameString
end rename_me


on get_loc(searchitem, searchString)
	considering case
		return offset of searchitem in searchString
	end considering
end get_loc


on file_exists(file_path)
	try
		get file_path as alias
		return true
	on error
		return false
	end try
end file_exists


on makeUpper(subjectString)
	set outputString to characters of subjectString
	repeat with myChars in outputString
		if myChars is in lowerStr then set contents of myChars to character (offset of myChars in lowerStr) of upperStr
	end repeat
	return outputString as string
end makeUpper


on GetFlashDisk()
	tell application "Finder"
		set DiskList to disks
		repeat with TheDisk in DiskList
			set FormatDisk to format of TheDisk as string
			if (FormatDisk begins with "MS-DOS" or FormatDisk contains "dfms") then
				return TheDisk as alias
			end if
		end repeat
	end tell
end GetFlashDisk