Keeping a count of files copied??

I am trying to keep a count of all files that get successfully copied, then display that count in a dialog upon finishing all the files in the folder. This counter needs to count thousands of files in a single shot. Anybody have a minute to help me with this? And thanks to everybody for the help to this point. Here is the script so far:

set this_folder to choose folder
set theFolder to alias "Masters_6TB:test:"

tell application "Finder"
	set fileList to every file of this_folder
end tell

set matchedCodes to {}
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to text 1 thru 4 of thisFilename
	set filedate to text 6 thru 11 of thisFilename
	
	if (filecode is in matchedCodes) then
		-- If this file code has appeared earlier in the run, search the odd-numbered items of matchedCodes for it.
		set i to 1
		repeat until (item i of matchedCodes is filecode)
			set i to i + 2
		end repeat
		-- Read the artist name from the following, even-numbered item.
		set ArtName to item (i + 1) of matchedCodes
	else
		-- Otherwise, get the artist name from FMP.
		tell application "FileMaker Pro"
			open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
			try
				delete every request
			end try
			create request
			set cell "Code Number" of request 1 to filecode
			find
			set ArtName to field "Artist Name" of current record
		end tell
		
		-- Update the list of matched codes and artist names.
		set end of matchedCodes to filecode
		set end of matchedCodes to ArtName
		
		-- Try making a folder for this new artist.
		tell application "Finder"
			try
				make new folder at theFolder with properties {name:ArtName}
			end try
		end tell
	end if
	set dateFolder to filedate as string
	tell application "Finder"
		try
			make new folder in folder ArtName in theFolder with properties {name:filedate} -->creates dated subfolder
		end try
		set fileVar to thisFile as string
		try
			move fileVar to folder dateFolder in folder ArtName in theFolder -->moves file into dated subfolder
		
		end try
		
		
	end tell
	
end repeat

Model: Intel MacPro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

hi paul,

this may be too simple, but couldn’t you just count the loops you go through?


set this_folder to choose folder
set theFolder to alias "Masters_6TB:test:"
set myCount to 0

tell application "Finder"
	set fileList to every file of this_folder
end tell

set matchedCodes to {}
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to text 1 thru 4 of thisFilename
	set filedate to text 6 thru 11 of thisFilename
	
	if (filecode is in matchedCodes) then
		-- If this file code has appeared earlier in the run, search the odd-numbered items of matchedCodes for it.
		set i to 1
		repeat until (item i of matchedCodes is filecode)
			set i to i + 2
		end repeat
		-- Read the artist name from the following, even-numbered item.
		set ArtName to item (i + 1) of matchedCodes
	else
		-- Otherwise, get the artist name from FMP.
		tell application "FileMaker Pro"
			open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
			try
				delete every request
			end try
			create request
			set cell "Code Number" of request 1 to filecode
			find
			set ArtName to field "Artist Name" of current record
		end tell
		
		-- Update the list of matched codes and artist names.
		set end of matchedCodes to filecode
		set end of matchedCodes to ArtName
		
		-- Try making a folder for this new artist.
		tell application "Finder"
			try
				make new folder at theFolder with properties {name:ArtName}
			end try
		end tell
	end if
	set dateFolder to filedate as string
	tell application "Finder"
		try
			make new folder in folder ArtName in theFolder with properties {name:filedate} -->creates dated subfolder
		end try
		set fileVar to thisFile as string
		try
			move fileVar to folder dateFolder in folder ArtName in theFolder -->moves file into dated subfolder
		
		end try
		
		
	end tell
	set myCount to (myCount + 1)
end repeat

display dialog "Copied " & myCount & " files."

hey waltr,

Thanks for the response. Although that does work, my concern is that it is counting the loops and not the files copied. Therefore if for some reason it does not copy the file during the current loop, it will still count the file as copied. The idea is to compare the count of files copied to the count of files in the original folder to make sure everything made it over. hmmmmmmmmmmm

thanks

Would doing what waltr said except moving the “set mycountcopied to (mycountcopied + 1)” to after the try block containing the move command. Would that then not count a file if the try did not succeed? Here is what I have…

try
			move fileVar to folder dateFolder in folder ArtName in theFolder -->moves file into dated subfolder
			set mycountcopied to (mycountcopied + 1)
		end try

here is the whole thing again modified a bit. I just want to make sure that if a file doesn’t copy that I am notified. Thanks for the help.

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file



set this_folder to choose folder
set theFolder to alias "Masters_6TB:test:"
set mycountcopied to 0

tell application "Finder"
	set fileList to every file of this_folder
	set mycount to the number of items in fileList as Unicode text
end tell

set matchedCodes to {}
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to text 1 thru 4 of thisFilename
	set filedate to text 6 thru 11 of thisFilename
	
	if (filecode is in matchedCodes) then
		-- If this file code has appeared earlier in the run, search the odd-numbered items of matchedCodes for it.
		set i to 1
		repeat until (item i of matchedCodes is filecode)
			set i to i + 2
		end repeat
		-- Read the artist name from the following, even-numbered item.
		set ArtName to item (i + 1) of matchedCodes
	else
		-- Otherwise, get the artist name from FMP.
		tell application "FileMaker Pro"
			open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
			try
				delete every request
			end try
			create request
			set cell "Code Number" of request 1 to filecode
			find
			set ArtName to field "Artist Name" of current record
		end tell
		
		-- Update the list of matched codes and artist names.
		set end of matchedCodes to filecode
		set end of matchedCodes to ArtName
		
		-- Try making a folder for this new artist.
		tell application "Finder"
			try
				make new folder at theFolder with properties {name:ArtName}
			end try
		end tell
	end if
	set dateFolder to filedate as string
	tell application "Finder"
		try
			make new folder in folder ArtName in theFolder with properties {name:filedate} -->creates dated subfolder
		end try
		set fileVar to thisFile as string
		
		try
			move fileVar to folder dateFolder in folder ArtName in theFolder -->moves file into dated subfolder
			set mycountcopied to (mycountcopied + 1)
		end try
		
		
		set this_data to fileVar as text
		set this_file to (((path to desktop folder) as text) & "MastersLog")
		my write_to_file(this_data, this_file, true)
		
	end tell
	
end repeat
if mycountcopied < mycount then display dialog "Some Items Were Not Copied"
display dialog "Copied " & mycountcopied & " Files out of " & mycount

Conincidentally, I just typed a solution in a another thread a few minutes ago
http://bbs.applescript.net/viewtopic.php?id=19578

Compare the md5 checksum of the original file with the md5 of the duplicate with:


   set b to (POSIX path of fileVar) as string
   set md51 to do shell script "md5 '" & b & "'"
   set md52 to do shell script "md5 '" & ... & "'" --same for duplicate

then try to update fileCount only if md5’s are equal


try
if md51 = md52 then 
set myCount to (myCount + 1)
else
set problemFiles to problemFiles & {fileVar}
end if
on error
set problemFiles to problemFiles & {fileVar}
end try

make sure that problemFiles is initialised to {} at the beginning
and read it out (or label them in Finder) at the end to see what went wrong.

Thanks eelco,

I saw your other post. I just didn’t quite know how to implement it on my end. Actually I am still not sure. What would the variable for the duplicate file be? I see that b is set to the POSIX path of FileVar, isn’t that the path to the original location? Is there a way to check it and make sure I didn’t screw anything up? Excuse my lack of knowledge on this.


set b to (POSIX path of fileVar) as string
			set c to (POSIX path of ---->what goes here?
			set md51 to do shell script "md5 '" & b & "'"
			set md52 to do shell script "md5 '" & c & "'"-->is this right?

Actually I didn’t look into your script, but for md5 to work the filepath must be


set fileVar to "HD:Users:yourname:Desktop:Movie.mov"
-- to be "Posixed" into: /Users/yourname/Desktop/Movie.mov:
set b to (POSIX path of fileVar) as string

As I just checked, in Tiger the md5 shell command also returns the POSIX object so the md5 value is the last word:


set md51 to last word of (do shell script "md5 '" & b & "'")

Try playing with some known files on your desktop (and their duplicates) to see how this works.