bug in my code cant find it

I have a bug in my code and concerns writing a log file.

I have written it before and it worked but i am now in the process of tidying and deleteing old obsolete code, in the hope to make it faster.

The code snippet that i am having problems on is below:


property logFile : "Shared G4 HD:Users:karrenmay:Desktop:checkFolderAgainstFile:lastRunLog.txt" as alias
property libraryFile : "Shared G4 HD:Users:karrenmay:Desktop:checkFolderAgainstFile:filesUploaded.txt" as alias
property theFolder : "Shared G4 HD:Users:karrenmay:Desktop:web pics:" as alias
property uploadedFolder : "Shared G4 HD:Users:karrenmay:Desktop:checkFolderAgainstFile:uploaded to Net:" as alias
------------------------------------------------------------------------------------------------------------------------
-- ^---- Above sets the global variables
-- v---- Below sets the counters and starts the script
------------------------------------------------------------------------------------------------------------------------
set fileTabber to "1"
set notCount to "0"
set delCount to "0"

------------------------------------------------------------------------------------------------------------------------
-- ^---- Above sets the counters and starts the script
-- v---- Below  checks the files against the fileName
------------------------------------------------------------------------------------------------------------------------
--clearTxtFile(logFile)

set fileList to read libraryFile as list using delimiter return
set listCount to count fileList

set folderList to list folder theFolder without invisibles
set folderCount to count folderList
--try
repeat with n from 1 to folderCount
	set theItem to item n of folderList as string
	if fileList does not contain theItem then
		my addToLogFile(logFile, theItem, fileTabber)
		set notCount to (notCount + 1)
		if fileTabber is equal to 3 then
			set fileTabber to 1
		else
			set fileTabber to (fileTabber + 1)
		end if
	else
		my moveTheFile((theFolder as string) & theItem)
		set delCount to (delCount + 1)
		
	end if
end repeat
my appendToFile(logFile, listCount, notCount, delCount)
--on error theError


--end try
------------------------------------------------------------------------------------------------------------------------
-- ^---- Above checks the files against the fileName
-- v---- Below adds the item to the log file
------------------------------------------------------------------------------------------------------------------------

on addToLogFile(theLogFile, theItem, fileTabber)
	try	
		
		if fileTabber is less than 3 then
			open for access theLogFile with write permission
			write (theItem & "              ") to theLogFile starting at eof
			close access theLogFile
		else
			
			open for access theLogFile with write permission
			write (return & theItem & "              ") to theLogFile starting at eof
			close access theLogFile
		end if
	
	on error theError
		display dialog theError
		try
			close access theLogFile
		end try
	end try
end addToLogFile

Basically what should happen is this code looks through a folder namely web pics, compares the filenames against the txt file, if its not been uploaded the same filename is then sent to the logFile and if it has been uploaded then it will move the file to the uploaded folder.

The code itself works, it checks against the file and is faster than before, but it doesnt write the log.

Any ideas?

It looks like you are calling two different file writing handlers (addToLogFile and appendToFile) but only one is present. Did you snip one for the sake of this post?

yes i did the appendToFile works, and is just about the same code.


on appendToFile(theFile, listCount, notCount, delCount)
	try
		clearTxtFile(theFile)
		set numberChecked to (delCount + notCount)
		open for access theFile with write permission
		write (return & " --------------------- " & return & "On " & start & " checkTextFile was run and this is what happened:" & return & return & "Number of files uploaded so far: " & listCount & return & "Number of Files Checked: " & numberChecked & return & "Number Already Uploaded: " & delCount & "   <------<< these files have been moved to the uploadFolder" & return & "Number Not Uploaded: " & notCount & return & " --------------------- " & return) to theFile starting at eof
		close access theFile
	on error theError
		display dialog theError
	end try
end appendToFile

Its just a diagnostics report. But that gets added to the file always, so it does work, but from what i can see its the same as the other method.

The thing is its basically just a cut and paste.

p.s. it might be abit confusing me using theFIle and thelogFile but its just for the testing and trying to find the error, normally both methods would refer to the file as theFile

Can you try without using ‘my’ in front of appendToFile(logFile, listCount, notCount, delCount)?

tried that just and still the same, the append still appends but the log doesnt want to know.

Could it be the repeat loop that its in?

In fact remove every ‘my’ that you’ve put in your code, ‘my’ is used inside a ‘tell…end tell’ block.

still no luck.

This is weird.

Have doen what you said though.

Is the clearTxtFile handler undoing something that one of the other handlers has done, giving the appearance that the file isn’t being written to?

the clearTxtFile just resets it, doing what you mentioned ages ago.


on clearTxtFile(theFile)
	open for access theFile with write permission
	try
		set eof of theFile to 0
		close access theFile
	on error
		try
			close access theFile
		end try
	end try

end clearTxtFile

I dont think that it causes any problems, but it does work, as i wrote gobble into the file and when it ran gobble was gone and the appendFile worked.

I’m no file access guru, but I remember from your previous posts on this that I thought it looked a bit odd. But since it seemed to work then I thought it best to not say anything.


open for access theLogFile with write permission 
write (theItem & "              ") to theLogFile starting at eof
close access theLogFile

The open call, returns a read/write file access reference number. But instead of using the returned number you write to the file using the file reference or alias. The standard additions dictionary suggests that your method and mine below should both work, but I’m always more comfortable accesing a file using the file access reference number.

The following might be worth trying if nothing else.


set fileReferenceNum to (open for access theLogFile with write permission)
write ((theItem & "              ") to fileReferenceNum starting at eof
close access fileReferenceNum