Directory & File Lists
Directory Lists
cumulativeFolderSizeTree
Returns folder tree list.
Each line a tab delimited list; folder path, local content size, cumulative content size.
Sizes are in KB. See the '-s' entry of 'man ls' for details.
Note: The accumulator method skips folder paths which contain single/double quotes or unescaped grep metachars. Such skips causes the quick method of merging local & cumulative sizes ('paste') to return unreliable data. Check field count of last line to confirm absence of skips.
on mouseup
answer folder "select a folder"
if it = "" then return ""
put cumulativeFolderSizeTree(it) into dirTree
set the itemDel to tab
put the number of items in last line of dirTree into chk
set the itemDel to ","
if chk ≠3 then
alert sheet "Sync error!" explain "A path contains single/double quotes or unescaped metachar."
end if
--do something with dirTree
end mouseup
function cumulativeFolderSizeTree rootDir
if char 1 of rootDir ≠"/" then
put "osascript -e 'posix path of alias `[[rootDir]]`' | perl -pe chomp | perl -pe chop" into cmd
put shell(merge(cmd)) into rootDir
end if
--convert rootDir to unix path if necessary
put "`" into bq
--bq = command expansion metacharacter
get projectpath(this proj)
--begin
put "cd `[[bq]]osascript -e 'posix path of alias `[[it]]`' | perl -pe chomp[[bq]]`" into cmd
--set working dir to project folder
--begin
put " ; ls -Rs `[[rootDir]]` " after cmd
--ls long, recursive, 512byte block counts
--'ls -s' flag only way to include res fork size
put " | tr \n \t " after cmd
--replace lfs with tabs to make a single line
put " | perl -pe 's|^|[[rootDir]]:t|' " after cmd
--prepend rootdir to match ls output
put " | perl -pe 's|t/|n/|g' " after cmd
--replace tab '/' sequence with lf '/'
--each line begins with folder path
put " | perl -pe 's|:ttotal (d+).*|`/t` . (($1*512)/1024)|e' " after cmd
--strip each line to path<tab>size in KB.
put " | tee ~tmp.txt " after cmd
--save current state to a temp file
--dir path <tab> local size
--//end part 1
--begin part 2
put " | awk -Ft '{print $1}' " after cmd
--strip lines of size field
put " | perl -pe 's/([()$?*+])/\$1/g' " after cmd
--escape path chars that are grep regex metachars... ( ) . $ ? * +
--************
put " | perl -pe 's/./\./' " after cmd
--bug fix: dot escapes must be done separately
--************
--dynamic grep/awk
put " | awk -v dq='`' -v sq=`'` " after cmd
put " '{system( `grep -Ei ` sq `^` $0 sq ` ~tmp.txt ` " after cmd
put "` | awk -F\t ` sq `{s+= $2} END {print ` dq $0 dq dq`\t`dq ` s }` sq " after cmd
put "` >> ~tmp2.txt ` )}' " after cmd
--for each dir path, get self + child dir records from temp file
--sum sizes of self + child dirs
--save a new file...
--dir path <tab> cumulative size
--//
--//end part 2
put " ; cat ~tmp2.txt " after cmd
--read accumulated temp file
put " | awk -Ft '{print $2}' " after cmd
--strip path field
put " | paste ~tmp.txt - " after cmd
--merge temp file 1 & cumulative sizes
--path<tab>local size<tab>cumulative size
put " | iconv -c -f utf8-mac -t macroman " after cmd
--reencode to macroman
put " ; rm ~tmp.txt ; rm ~tmp2.txt " after cmd
--delete temp files
put merge(cmd) into cmd
return shell(cmd)
end cumulativeFolderSizeTree
cumulativeFolderSizeTree2
A slightly faster with large directories, slightly slower with small directories.
Relative paths relative to root.
Each line a tab delimited list; folder path, local content size, cumulative content size.
Sizes are in KB. See the '-s' entry of 'man ls' for details.
Note: The accumulator method skips folder paths which contain single/double quotes or unescaped grep metachars. Such skips causes the quick method of merging local & cumulative sizes ('paste') to return unreliable data. Check field count of last line to confirm absence of skips.
on mouseup
answer folder "select a folder"
if it = "" then return ""
get folderCumulativeSizeTree2(it)
put it into dirTree
set the itemDel to tab
put the number of items in last line of it into chk
set the itemDel to ","
if chk ≠3 then
alert sheet "Sync error!" explain "A path contains single/double quotes or unescaped metachar."
end if
--do something with dirTree
end mouseup
function folderCumulativeSizeTree2 rootDir
if char 1 of rootDir ≠"/" then
put "osascript -e 'posix path of alias `[[rootDir]]`' | perl -pe chomp | perl -pe chop" into cmd
put shell(merge(cmd)) into rootDir
end if
--convert rootDir to unix path if necessary
put "`" into bq
--bq = command expansion metacharacter
get projectpath(this proj)
--begin
put "cd `[[bq]]osascript -e 'posix path of alias `[[it]]`' | perl -pe chomp[[bq]]`" into cmd
put shell(merge(cmd)) into projDir
--begin
put " ; ls -Rs `[[rootDir]]` " after cmd
--ls long, recursive, 512byte block counts
--'ls -s' flag only way to include res fork size
put " | perl -pe 's/^ +//g' " after cmd
--delete leading spaces
put " | perl -pe 's/^(d+) /$1t/' " after cmd
--replace space following leading digits with a tab
put " | awk -Ft '{print $1}' " after cmd
--trim ls output to dir path & 512 char block sizes
put " | tr \n \t " after cmd
--replace lfs with tabs to make a single line
put " | perl -pe 's|^|[[rootDir]]:t|' " after cmd
--prepend rootdir to match ls output
--SC shell() so 'ls' doesn't echo the 'rootDir' like a real terminal
put " | perl -pe 's|t(.?/)|n$1|g' " after cmd
--replace tab '/' sequence or "./" with lf '/' or lf './'
--each line begins with folder path followed by tab delimited sizes
put " | perl -pe 's|:t|/t|' " after cmd
--replace colon following each path with a slash
put " | perl -pe 's|^[[rootdir]]/|./|' " after cmd
--make path relative to root
put " | awk -Ft '{s=0} {for (i=2;i<=NF;i++) s+=($i*512/1024)} {print $1 `t` s}'" after cmd
--concatenate path <tab> sum of sizes in KB for each dir
put " | tee ~tmp.txt " after cmd
--save current state to a temp file
--data=dir path <tab> local size
--//end part 1
--begin part 2
put " | awk -Ft '{print $1}' " after cmd
--strip lines of size field
put " | perl -pe 's/([()$?*+])/\$1/g' " after cmd
put " | perl -pe 's/./\./g' " after cmd
--escape path chars that are grep regex metachars... ( ) . $ ? * +
--dynamic grep/awk
put " | awk -v dq='`' -v sq=`'` " after cmd
put " '{system( `grep -Ei ` sq `^` $0 sq ` ~tmp.txt ` " after cmd
put "` | awk -F\t ` sq `{s+= $2} END {print ` dq $0 dq dq`\t`dq `s }` sq " after cmd
put "` >> ~tmp2.txt ` )}' " after cmd
--for each dir path, get self + child dir records from temp file
--sum sizes of self + child dirs
--save a new file...
--dir path <tab> cumulative size
--//
--//end part 2
put " ; cat ~tmp2.txt " after cmd
--read accumulated temp file
put " | awk -Ft '{print $2}' " after cmd
--strip path field
put " | paste ~tmp.txt - " after cmd
--merge temp file 1 & cumulative sizes
--path<tab>local size<tab>cumulative size
put " | tr \n \r " after cmd
--convert lfs to crs to allow perl to make root path absolute
--else perl below makes every path absolute
put " | perl -pe 's|^./|[[rootDir]]/|' " after cmd
--make root path absolute
put " | iconv -c -f utf8-mac -t macroman " after cmd
--reencode to macroman
put " ; rm ~tmp.txt ; rm ~tmp2.txt " after cmd
--delete temp files
put merge(cmd) into cmd
return shell(cmd)
end folderCumulativeSizeTree2
Comments (0)
You don't have permission to comment on this page.