dir

PURPOSE ^

DIR List files on an SFTP server.

SYNOPSIS ^

function list = dir(h, path)

DESCRIPTION ^

DIR  List files on an SFTP server.

  Syntax:
    DIR(H)
    DIR(H, PATH)
    LIST = DIR(H, ...)

  Description:
    DIR(H, PATH) lists the files in a path.
    If the path is a directory, the files in the directory are listed.
    If the path is a file, tha file itself is listed.
    Otherwise, the path is considered a glob which may contain wildcards 
    ('?' or '*'), and only files matching the glob are listed, if any.

    LIST = DIR(H, ...) returns the files in an M-by-1 structure with fields: 
      NAME:    file name
      BYTES:   number of bytes allocated to the file
      ISDIR:   whether file is a directory or not
      DATE:    modification time timestamp (string)
      DATENUM: modification time as a serial date number

  Examples:
    % Print contents of current directory:
    dir(h)
    % Print contents of parent directory:
    dir(h, '..')
    % Get attributes of files in current directory:
    list = d(h)
    % Get attributes of files in parent directory:
    list = d(h, '..')

  See also:
    SFTP

  Authors:
    Joan Pau Beltran  <joanpau.beltran@socib.cat>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

dir.m

SOURCE CODE ^

0001 function list = dir(h, path)
0002 %DIR  List files on an SFTP server.
0003 %
0004 %  Syntax:
0005 %    DIR(H)
0006 %    DIR(H, PATH)
0007 %    LIST = DIR(H, ...)
0008 %
0009 %  Description:
0010 %    DIR(H, PATH) lists the files in a path.
0011 %    If the path is a directory, the files in the directory are listed.
0012 %    If the path is a file, tha file itself is listed.
0013 %    Otherwise, the path is considered a glob which may contain wildcards
0014 %    ('?' or '*'), and only files matching the glob are listed, if any.
0015 %
0016 %    LIST = DIR(H, ...) returns the files in an M-by-1 structure with fields:
0017 %      NAME:    file name
0018 %      BYTES:   number of bytes allocated to the file
0019 %      ISDIR:   whether file is a directory or not
0020 %      DATE:    modification time timestamp (string)
0021 %      DATENUM: modification time as a serial date number
0022 %
0023 %  Examples:
0024 %    % Print contents of current directory:
0025 %    dir(h)
0026 %    % Print contents of parent directory:
0027 %    dir(h, '..')
0028 %    % Get attributes of files in current directory:
0029 %    list = d(h)
0030 %    % Get attributes of files in parent directory:
0031 %    list = d(h, '..')
0032 %
0033 %  See also:
0034 %    SFTP
0035 %
0036 %  Authors:
0037 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0038 
0039 %  Copyright (C) 2014-2016
0040 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0041 %  <http://www.socib.es>
0042 %
0043 %  This program is free software: you can redistribute it and/or modify
0044 %  it under the terms of the GNU General Public License as published by
0045 %  the Free Software Foundation, either version 3 of the License, or
0046 %  (at your option) any later version.
0047 %
0048 %  This program is distributed in the hope that it will be useful,
0049 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0050 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0051 %  GNU General Public License for more details.
0052 %
0053 %  You should have received a copy of the GNU General Public License
0054 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0055 
0056   if (nargin < 2)
0057       path = '.';
0058   end
0059   
0060   try
0061     atts = mexsftp('lsfile', h.sftp_handle, path);
0062   catch exception
0063     if ~strcmp(exception.identifier, 'sftp:lsfile:ListError')
0064       rethrow(exception);
0065     end
0066     atts = [];
0067   end
0068   if isempty(atts)
0069     atts = mexsftp('lsglob', h.sftp_handle, path);
0070   elseif atts.isdir
0071     atts = mexsftp('lsdir', h.sftp_handle, path);
0072   end
0073   
0074   for i = 1:numel(atts)
0075     atts(i).datenum = datenum(atts(i).date);
0076     atts(i).date = datestr(atts(i).date, 'local');
0077   end
0078   
0079   if nargout < 1
0080     % Display in columns.
0081     disp(' ');
0082     if ~isempty(atts)
0083       entries = sortrows(char(strcat({atts.name},  {'  '})));
0084       width = [1 0] * get(0, 'CommandWindowSize')';
0085       cols = max(1, floor(width / size(entries, 2)));
0086       rows = ceil(size(entries, 1)/cols);
0087       entries(end+1:rows*cols, :) = ' ';
0088       disp(reshape(entries(reshape(1:rows*cols, rows, [])',:)', [], rows)')
0089     end
0090     disp(' ');
0091   else
0092     list = atts;
0093   end
0094   
0095 end

Generated on Fri 06-Oct-2017 10:47:42 by m2html © 2005