Im trying to compare items from a fold with a previous state of the same folder. I want get the name of files and folder diferents
tell application "Finder"
set filelist to every item of entire contents of folderpath
-- save every item and (with his modification time) to a filelist.txt
end tell
------------- After i want launch a code for compare (i have done "when" launch this code using launchd)
set filelist2 to every item of entire content of folderpath
-- compare filelist.txt with filelist2
if item have same ItemName compare file modificationDate
if modificationDate is diferent get nameofItemModifed
if item in filelist.txt exist and in filelist2 no get nameofItemdeleted
if item in filelist.txt No exist and in filelist2 yes get nameofItemdded
By using growl latter I will display a message with (nameofItemmodified, nameofitemdeleted, nameofitemadded)
With this shell command
ls -lTd $PWD /pathTofolder/* | awk ‘{print $6,$7,$8,$9,$10,$11}’ >/PathToSaveFileList/Filelist.txt
I can write a txt with a list of files with his modification dates ordered by rows but if i launch inside an applescript dont work, i use:
set ruta to (POSIX path of (path to downloads folder from user domain)) as text
do shell script "ls -lTd $PWD " & ruta & " | awk '{print $6,$7,$8,$9,$10,$11}' >/PathToSaveFileList/Filelist.txt"
But dont work too
Is better go with finder to do this comparative or better go with shell ??
I would use an applescript method because you can read and write lists to a file easily. That means you can create a list of names and modification dates, write them to a file, the later you can read that file and get the exact same list back. This makes it easy, expecially when you will need to compare dates. Because you keep them as class date instead of as class text it makes comparing them simple. Anyway here’s how you can create the list and write it to a file. You’ll need to run this initially to create the file.
set ruta to path to downloads folder from user domain
set fileListPath to (path to desktop as text) & "Filelist.txt"
set theList to fileAndModDateListOfFolder(ruta) -- get the list from a folder
writeTo(fileListPath, theList, false, list) -- write it to a file
(*================== SUBROUTINES ===================*)
-- get list of items and mod dates
on fileAndModDateListOfFolder(folderPath)
set folderPath to folderPath as text
set theList to {}
try
tell application "Finder"
set theItems to every item of folder folderPath
repeat with anItem in theItems
set end of theList to {name of anItem, modification date of anItem}
end repeat
end tell
end try
return theList
end fileAndModDateListOfFolder
on writeTo(targetFile, theData, apendData, mode) -- apendData is true or false... mode is string, list, or record (no quotes around either)
try
set targetFile to targetFile as text
if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as mode
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
Once you have the file, use this code. This will give the list from the file (fileList) and the current list of items in the folder (currentList). All you have to do now is compare the 2 lists and generate the output you require.
set ruta to path to downloads folder from user domain
set fileListPath to (path to desktop as text) & "Filelist.txt"
-- get the original list from the file
set fileList to readFrom(fileListPath, list)
-- get the new list
set currentList to fileAndModDateListOfFolder(ruta)
-- compare the fileList to the currentList
(*================== SUBROUTINES ===================*)
-- get list of items and mod dates
on fileAndModDateListOfFolder(folderPath)
set folderPath to folderPath as text
set theList to {}
tell application "Finder"
set theItems to every item of folder folderPath
repeat with anItem in theItems
set end of theList to {name of anItem, modification date of anItem}
end repeat
end tell
return theList
end fileAndModDateListOfFolder
on readFrom(targetFile, mode)
try
set targetFile to targetFile as text
targetFile as alias -- if file doesn't exist then you get an error
set openFile to open for access file targetFile
set theData to read openFile as mode
close access openFile
return theData
on error
try
close access file targetFile
end try
return false
end try
end readFrom
do shell script "mdfind -onlyin '/MonitorizedPath/folder' 'kMDItemFSContentChangeDate >= $time.now(-10000)' >~/MonitorizeFolder/UpdatedItems.txt"
This writte a text file with items updated in updateditems.txt, i found one problem with this txt and punctuated text, by example one word like “TÃtulo” is writted in updatedItems.txt like “TÄötilo”, how i can writte updatedItems.txt with text with punctuation marks respected.
Latter i can read line by line this updateditems.txt and show those lines in Growl, but if those lines are wrong writted i can not get the path for read the icon of every file/folder
i think that $time.now(-10000) would be $time.now(-1) but with -1 mdfind writte anything in updateditems.txt… strange…