Code covered by the BSD License  

Highlights from
Disk Usage

image thumbnail
from Disk Usage by Robert Bemis
Lightweight GUI sums up how your disk space is used.

bytes(varargin)
function varargout = bytes(varargin)
%BYTES summarizes disk usage for everything under a folder
%   BYTES with no inputs starts analyzing current directory
%   BYTES(SPECFOLDER) starts with specified folder (if valid)
%   FIG = BYTES(...) returns figure handle (otherwise hidden)
%   BYTES('callback_name', ...) invokes named callback function
%   Note: bytes.m holds the application code for bytes.fig

% Last Modified by GUIDE v2.5 16-Jul-2004 23:31:24

% Copyright 2006-2010 The MathWorks, Inc.

global cwd cur_path

if nargin==0 || nargin==1 && isdir(varargin{1}) % LAUNCH GUI
  
  fig=openfig(mfilename,'reuse');
  
  % Generate a structure of handles to pass to callbacks, and store it. 
  handles=guihandles(fig);
  guidata(fig, handles);
  
  % generate drive letter pulldown
  alphabet=char(floor('A'):floor('Z'));  % a-z
  list={}; cwd={};
  for i=1:length(alphabet)
    dr_ltr=[alphabet(i) ':'];
    if exist([dr_ltr '\'],'dir')==7  % valid drive letter
      list{end+1}=dr_ltr;
      cwd{end+1}='\';
    end
  end
  if length(list)<1, error('Empty drive list'), end
  set(handles.drive,'string',list)
  
  if nargin==1 %user specified starting folder
    owd = pwd;
    cd(varargin{1})
    cur_path = pwd;
    cd(owd)
  else %cwd (current directory)
    cur_path = pwd;
  end
    
  % analyze working directory
  cur_drive=cur_path(1:2);
  cur_dr_num=findcell(list,upper(cur_drive));
  set(handles.drive,'value',cur_dr_num)
  %cur_folder=cur_path(3:length(cur_path));
  bytes_analyze(cur_path,handles)
  
  if nargout>0
    varargout{1}=fig;
  end
  
elseif ischar(varargin{1})  % INVOKE NAMED SUBFUNCTION OR CALLBACK
  
  %hack to allow alternative launch syntax
  
  try
    if (nargout)
      [varargout{1:nargout}]=feval(varargin{:});  % FEVAL switchyard
    else
      feval(varargin{:});  % FEVAL switchyard
    end
  catch
    disp(lasterr);
  end
  
end


%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and 
%| sets objects' callback properties to call them through the FEVAL 
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the 
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback.  You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks.  Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.


% --------------------------------------------------------------------
function varargout = drive_Callback(h, eventdata, handles, varargin)
% --------------------------------------------------------------------
global cwd cur_path
cur_drive=cur_path(1:2);
%cur_folder=cur_path(3:length(cur_path));
list=get(handles.drive,'string');
cur_dr_num=findcell(list,upper(cur_drive));
new_dr_num=get(handles.drive,'value');
if new_dr_num~=cur_dr_num  % preserve CWD previous drive
  cwd{cur_dr_num}=cur_path(3:length(cur_path));
  new_folder=cwd{new_dr_num};
  cur_path=[list{new_dr_num} new_folder];
  bytes_analyze(cur_path,handles)
end


% --------------------------------------------------------------------
function loc=findcell(x,y)
% Returns indices of X that match Y
% --------------------------------------------------------------------
loc=[];
for k=1:length(x)
  if x{k}==y, loc=[loc k]; end
end


% --------------------------------------------------------------------
function varargout = folders_Callback(h, eventdata, handles, varargin)
% --------------------------------------------------------------------
global cwd cur_path
folder_num = get(handles.folders,'value');
list = get(handles.folders,'string');
new_folder = list{folder_num};
if findstr(new_folder,' [')
  while findstr(new_folder,' [')
    new_folder = new_folder(1:end-1);
  end
  new_folder = new_folder(1:end-1);
end
old_path = cur_path;  % preserve
%old_folder=old_path(3:length(old_path));
if strcmp(new_folder,'\')  % root
  new_path = old_path(1:3);
elseif strcmp(new_folder,'..')  % back
  last = max(findstr(old_path,'\'));
  keep = max(3,last-1);
  new_path = old_path(1:keep);
else  % forward
  if length(old_path)==3
    new_path = [old_path(1:2) new_folder];
  else
    new_path = [old_path new_folder];
  end
end
bytes_analyze(new_path,handles)
cur_path = new_path;
dr_ltr = get(handles.drive,'value');
cwd{dr_ltr} = new_path(3:length(cur_path));


% --------------------------------------------------------------------
function bytes_analyze(cur_path,handles)
% --------------------------------------------------------------------
cur_folder = cur_path(3:end);
set(handles.path,'string',cur_path)
set(handles.total,'string','')
set(handles.folders,'value',1)
set(handles.folders,'string','analyzing...'), drawnow

if length(cur_folder)>1  % CWD not root
  list = {'\', '..'};
else
  list = {};
end
folders=0; files=0; file_bytes=0; total_bytes=0;
fn = dir(cur_path);
num_folders = length(fn);
folder_info = cell(0,3);
for i=1:num_folders
  if fn(i).isdir
    if strcmp(fn(i).name,'.') || strcmp(fn(i).name,'..')
      fbytes = 0;
    else %real folder to analyze
      fbytes = folder_size([cur_path '\' fn(i).name]);
      folders = folders+1;
      fsize = byte_fmt(fbytes);
      %disp(sprintf('\\%s(%s)',fn(i).name,fsize))
      msg = sprintf('\\%s [%s]',fn(i).name,fsize);
      list{end+1} = msg;
      %folder_info(length(list)-2,1:3)={fn(i).name,fsize,fbytes};
      folder_info(end+1,1:3) = {fn(i).name,fsize,fbytes};
    end
  else
    fbytes = fn(i).bytes;
    files = files+1;
    file_bytes = file_bytes+fbytes;
  end
  total_bytes = total_bytes+fbytes;
end
%%% RAB ToDo-optional sorting code %%%
if strmatch(get(handles.menu_SortSize,'Checked'),'on')
  [sorted,ascending] = sort(cell2mat(folder_info(:,3)));
  new_order = ascending(end:-1:1);
  if length(cur_folder)==1 %root folder
    list = list(new_order);
  else %subdirectory
    list(3:end) = list(2+new_order);
  end
end
%%% RAB ToDo-optional sorting code %%%
set(handles.folders,'string',list)
msg = 'Total';
if folders*file_bytes>0, msg=[msg ':']; end
if folders>0
  msg = [msg sprintf(' %d folder(s)',folders)];
end
if file_bytes>0
  if folders>0, msg=[msg ' +']; end
  msg = [msg sprintf(' %d file(s)',files)];
end
msg = [msg sprintf(' = %s bytes',byte_fmt(total_bytes))];
set(handles.total,'string',msg)


% --------------------------------------------------------------------
function [folder_bytes]=folder_size(fpath)
% --------------------------------------------------------------------
d=dir(fpath);
folder_bytes=0;
for i=1:length(d)
  if d(i).isdir
    if strcmp(d(i).name,'.') || strcmp(d(i).name,'..')
      fbytes=0;
    else
      fbytes=folder_size([fpath '\' d(i).name]);
    end
  else
    fbytes=d(i).bytes;
  end
  folder_bytes=folder_bytes+fbytes;
end


% --------------------------------------------------------------------
function [fsize] = byte_fmt(fbytes)
% --------------------------------------------------------------------
if fbytes==0
  fsize = '0';
else
  switch floor(log2(fbytes)/10);
  case 0, fsize=sprintf('%d',fbytes);
  case 1, fsize=sprintf('%.1fK',fbytes/1024);
  case 2, fsize=sprintf('%.1fM',fbytes/1024^2);
  case 3, fsize=sprintf('%.1fG',fbytes/1024^3);
  end
end


% --------------------------------------------------------------------
function menu_SortAlpha_Callback(hObject, eventdata, handles)
% hObject    handle to menu_SortAlpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if strmatch(get(hObject,'Checked'),'on') %already checked
  return  %nothing to do
end
set(handles.menu_SortAlpha,'Checked','on')
set(handles.menu_SortSize,'Checked','off')
%cur_path=get(handles.);
%bytes_analyze(cur_path,handles)


% --------------------------------------------------------------------
function menu_SortSize_Callback(hObject, eventdata, handles)
% hObject    handle to menu_SortSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strmatch(get(hObject,'Checked'),'on') %already checked
  return  %nothing to do
end
set(handles.menu_SortSize,'Checked','on')
set(handles.menu_SortAlpha,'Checked','off')
%bytes_analyze(cur_path,handles)


% --- Executes on button press in cd.
function cd_Callback(hObject, eventdata, handles)
% hObject    handle to cd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global cwd cur_path
new_folder = uigetdir(cur_path);
if new_folder==0, return, end
bytes_analyze(new_folder,handles)
cur_path = new_folder;
dr_ltr = get(handles.drive,'value');
cwd{dr_ltr} = new_folder(3:length(cur_path));


% --------------------------------------------------------------------
function path_Callback(hObject, eventdata, handles)
% hObject    handle to path (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global cwd cur_path
new_folder = get(hObject,'String');
if ~exist(new_folder,'dir')
  set(hObject,'String',cur_path)
  return
end
bytes_analyze(new_folder,handles)
cur_path = new_folder;
dr_ltr = get(handles.drive,'value');
cwd{dr_ltr} = new_folder(3:length(cur_path));

Contact us at files@mathworks.com