I am trying to delete files, within a specific folder, that are smaller than 2MB.
I am first trying to get a list of files within that folder with their size. Then I will browsing through that list and delete all files that are smaller than 2MB.
I found and modified this script on the forum.
However I am having difficulties in getting the size of the files. Would someone have an idea why?
Regards!
Daniel
set HomeFolder to (path to home folder as text)
set pathFolder1Name to "FOLDERX"
set pathFolder2Name to "MPEGfiles"
set DestinationFolder to (path to home folder as text) & pathFolder1Name & ":" & pathFolder2Name & ":" --"FPAQ:MPEGfiles:"
set POSIXdestinationFolder to POSIX path of DestinationFolder
my deleteSmallFiles(2, DestinationFolder)
empty trash
on deleteSmallFiles(MinSize, deleteFolder)
tell application "Finder"
set file_list_size to (size of deleteFolder) / (1024 * 2)
if file_list_size < Min_Size then
set file_list to every item in deleteFolder as alias list
repeat with each_file in file_list
display dialog "deleting " & each_file
end repeat
end if
end tell
end deleteSmallFiles
to calculate the size of a folder can take some time.
Use a repeat loop to determine the finishing of calculation
.
on deleteSmallFiles(MinSize, deleteFolder)
tell application "Finder"
repeat until size of deleteFolder is not missing value
delay 0.2
end repeat
set file_list_size to (size of deleteFolder) / (1024 * 2) -- maybe you mean 1024 ^ 2
if file_list_size < Min_Size then
set file_list to every item in deleteFolder
repeat with each_file in file_list
display dialog "deleting " & each_file as text
end repeat
end if
end tell
end deleteSmallFiles
If you request the file sizes thru the Finder, you have to wait until the Finder has calculated the size. So you have to insert a repeat loop:
on getFileSize(anAlias)
set fs to missing value
repeat while fs is missing value
tell application "Finder" to set fs to size of anAlias
delay 1
end repeat
return fs
end getFileSize
If you want it without this crappy repeat loop, you can use shell scripts. I just copy pasted this line of code from [url=http://hintsforums.macworld.com/archive/index.php/t-11750.html]this post[url].
on getFileSize(anAlias)
set anAlias to anAlias as text
set myShell to "/bin/ls -lR " & ¬
quoted form of POSIX path of anAlias & ¬
" | awk '{sum += $5} END{print sum}'"
return do shell script myShell
end getFileSize
In browsing through the WEB, I remembered the UNIX Find command. To perform this operation I execute the following shell command. This command removes files that are less than 12MB.
At this moment, I use the -size option with 50000000c. However, I am browsing through the web to inquire about the different options when using -size.