Folders, Lists and text documents

How can i place the contents of a folder into a list, and the contents of a text document into a list so that i can then compare the two lists.

This will in essence creat a search and will help a great deal.

Basically what should happen is this, the psudo code is java ish as i know that language quite well, but the list part is from applesccript



set uploadList to {<--Info from File-->}
set folderList to {<--Folder List-->}

for (i = 0 and i < folderList.length i++){
  for (j = 0 and j < uploadList.length j++){
     if (folderList[i] == uploadList[j]) then 
            this file has been uploaded
            do nothing just move on
            break from for loop
      } else{
      }
}//end j for loop
}//end i for loop

Do you know what i am tryubing to say??

I think I’ve got what you are trying to do. Does this work for you?

Jon

almost. Basically what i want to do is search the text document with the values in the folder.

As i understand it the code you gave me returns the number of elements, is hat right??

So basically if the folder had these files

hello.txt
goodbye.txt

then it should search a file for those two elements.

the file will have thousands of lines and i am looking for the best way to do this.

after i know how to do that i am going to create a seperate log, which i think i koq how to do of the filenames that aren’t in the file. This is so we dont duplicate pictures.

Ok, so what you mean is you have a folder full of files. When someone adds a new file to the folder, you want to search the text file to make sure the file is new and, if it is, add the file to the text file so that if the file is placed in the folder again, you won’t catalog it. Is this right? This would be easier if you used a Folder Action to just get the single name of the file added as opposed to iterating through all the folder’s items but, barring that, you can try something like:

Jon

yeah that is right from what i can see, havent tested just yet.

The reason for not doing the folder acrtion thing is that it will not always be the same folder.

As there are 20,000 pictures to upload to the net and all from all over the place, there are alread 4800 and the idea behind this is that we didnt want duplicate copies we also wanted it to be able to read all folders.

The file that will log the ones that aren’t there will be a different file.

This is so that the person using it can then look for that file and upload it knowing it wont be there.

Have had a thought i could just copy the file to another folder.

Hmmmmm. Is this simple. can i just:

copy theFile to folder:path

A real database would make this much easier for you in the long run.

Again, I’m slightly confused by what you want. The code below will read items from a text file and then search through a number of folders. If an item from any of the folders is not in the text file, that item will be added to a separate log file: This should be enough to get you started.

Jon

yeah that looks like what i want.

The company that i am working for at the moment, only on Summer Internship before starting my final year at uni, are almost archaic and use really old ways to do things. I’ve been told to learn pplescript and make their system work alot better. They will get a database and i just thought that AS would work well enough fro what i want to do.

Thanks for the help!

yeah that looks like what i want.

The company that i am working for at the moment, only on Summer Internship before starting my final year at uni, are almost archaic and use really old ways to do things. I’ve been told to learn pplescript and make their system work alot better. They will get a database and i just thought that AS would work well enough fro what i want to do.

Thanks for the help!

there was something missing in the code.

should have read;


          set the_item to (item i of list_from_folder)  as string

before it never said as string

also have catered the script to my needs, changing file names and so on but i dont understand it enough. Well i do, but instead of getting the names that aren’t in the list it just puts all the file names into the log file. regardless.

In the folder i have 3 files, 2 are on the document and 1 needs to be uploaded but the log file has all 3 filenames. i am trying to debug it but hmmm, cant do it, well i cant see the problem either way.

Could it be that the initial file isnt being read properly???

Sorry, to just get the item names and not the full paths, then change

to

Jon

That works great thanks although there is a slight bug which i will try and iron out.

Basically it seems to work fine with number files, i.e 009867.jpg but doesn’t seem to work for name files for example A.colas3/tempest.jpg, i know a few of the files in the folder are uploaded, so appear on the txt document, most dont get included but if its got letters then it doesnlt work.

i have a problem.

I have created a script, with help froom johnn8 and i cant seem to get it right.

Basically i compare a list of files agaisnt a txt file. Then the filenames that match get inputted into a log file.

I would like to delete or move to trash the files that are in the txt file.


set the_log_file to "Shared G4 HD:Users:karrenmay:Desktop:libraryLog.txt" as alias
set the_file to "Shared G4 HD:Users:karrenmay:Desktop:lbraryno.txt" as alias -- a text file with the file names each on a seperate line, one per line 
set all_folders to {"Shared G4 HD:Users:karrenmay:Desktop:web pics:"}
--------------------------------------------------------
-- This checks a folders files to see if they have been uploaded
-- If the file han't been then it will send the filename toa log file
-- Created by Andrew Davey with help from macscripter.net
--------------------------------------------------------

set list_from_file to read the_file using delimiter return

repeat with j from 1 to (count of all_folders)
	set the_folder to (item j of all_folders) as alias
	set list_from_folder to list folder the_folder without invisibles
	
	repeat with i from 1 to (count of list_from_folder)
		set the_item to (item i of list_from_folder) as string
		if list_from_file does not contain the_item then my add_item_to_file(the_log_file, the_item)
		if list_from_file contains the_item then my removeTheFile(the_item)
	end repeat
end repeat

on add_item_to_file(the_file, the_item)
	try
		open for access the_file with write permission
		write (the_item & return) to the_file starting at eof
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
end add_item_to_file

on removeTheFile(the_item)
	try
		tell application "Finder" to delete the_item
	on error
		display dialog "Could not delete " & the_item & ""
	end try
end removeTheFile

the last method is where its hould do it.

I always get errors.

Your credit line is a bit of a stretch…

The problem is that the item you are passing to the Finder to delete is just an item name, not it’s full path. The script below fixes it. You’d think by now you’d be starting to get the hang of this.

Jon

yeah fair point, i am almost i kow what is happening, but cant seem to find any really good resources for AS, ooh well enough of the excuses.

I had tried the following line, with a number of variations, but no luck.


		tell application "Finder" to delete "Shared G4 HD:Users:karrenmay:Desktop:web pics:" & the_item & ""

I thought that it did the same but i wouldnt work. I never thought of sending through the_folder as string.

Hmmmmm. Maybe one day.

I’m beginning to see the end of the tunnel :lol:

It is the same as sending the folder name through except you’ll notice in the script I posted above, you have to tell the Finder that this string is an alias to the file. Again, on it’s own, the Finder just sees a string and not a file reference. The beauty of sending the folder name through the script is that you don’t have to hard code the folder name into the delete subroutine and, if you add more folders to the list of folders to search at the top, the files will be deleted properly since the Finder will know where they reside instead of always looking to the same hard coded folder.

Jon

i have been using the script now and has worked well.

Although when it finsishes for some reason it says:

cant save applescript, stack over flow error.

What is that about. is there a away that i can get rid of it??

Try emptying the big variable at the end of the script with:

Jon

just did what you suggested but i get this error now when i ry and save it, after running it and after running it once saved:


Could not write o file because of an applescript stack overflow

Any ideas?