Help reading files in folder and counting items in it

HI All,

Would appreciate a lending hand here.

I am trying to read a dropped folder for items in it, reading each item if delimited by linefeed or carriage return, then storing the item’s name accordingly in a list. Below is the handler that I use:

on read_nameFromTexfileDoc()
	try
		set fileDoc to choose file with prompt "LOCATE YOUR TEXTFILE"
		set lf to ASCII character 10
		set rf to ASCII character 13
		
		--read the file first to check line delimiter (line feed or return??)
		set test_delim to read fileDoc
		set list_textNames to {}
		set file_Ref to (open for access fileDoc)
		try
			if test_delim contains lf then
				set thisName to read file_Ref using delimiter lf
				copy thisName to list_textNames
			else if test_delim contains rf then
				set thisName to read file_Ref using delimiter rf
				copy thisName to list_textNames
			end if
			close access file_Ref
		on error
			close access file_Ref
			display dialog return & "Error in reading the text file." with icon 0 buttons "OK" default button 1
		end try
	end try
	set fileCount to count of list_textNames
	return (fileCount)
end read_nameFromTexfileDoc

When all items in the folder is delimited by carriage return, the count of file items is correct. However, when some items are delimited by linefeed, this count always takes only those delimited by linefeed.

I tried several workarounds but I can’t seem to get the count right. How can I get this count right? I know there’s something wrong with it but I just cannot find it.

Thanks for the help.

archseed :frowning:

Hi All,

Further search of this forum revealed a related thread: http://bbs.macscripter.net/viewtopic.php?id=5455 but I am still having a problem adopting it. Here’s why:


set source_file to (choose file with prompt "LOCATE YOUR TEXTFILE")
set list_textNames to {}

--agree with jonn8, no need to open/close the file for access to read its content

set thisName to read source_file
copy thisName as string to list_textNames
--returns every text item (too numerous!); I need the count as strings

if I use jonn8’s code and read the source text file as string (as in the script below)


set source_file to (choose file of type {"TEXT"}) as string

then the source text file is dimmed. The only way I can choose the file is by removing the “of type {“TEXT”}”.

What gives?

TIA.

archseed :frowning:

Hi,

this handler does actually the same as yours


read_nameFromTexfileDoc()

on read_nameFromTexfileDoc()
	set fileDoc to choose file with prompt "LOCATE YOUR TEXTFILE"
	try
		return count (paragraphs of (read fileDoc))
	on error
		display dialog return & "Error in reading the text file." with icon 0 buttons "OK" default button 1
		return 0
	end try
end read_nameFromTexfileDoc

Hi Stafan,

I will try out your handler. It certainly is much much shorter than what I came up with.

Will let you know.

archseed :slight_smile:

Hi Stefan,

Your handler version works.
Nice and much shorter too.
Thanks a lot.

archseed :o