source: prextra/@ftp/dir.m @ 135

Last change on this file since 135 was 113, checked in by bduin, 8 years ago
File size: 3.2 KB
RevLine 
[113]1function varargout = dir(h,str)
2%DIR List directory on an FTP server.
3%   DIR(FTP,DIRECTORY_NAME) lists the files in a directory. Pathnames and
4%   wildcards may be used. 
5%
6%   D = DIR(...) returns the results in an M-by-1
7%   structure with the fields:
8%       name    -- filename
9%       date    -- modification date
10%       bytes   -- number of bytes allocated to the file
11%       isdir   -- 1 if name is a directory and 0 if not
12%       datenum -- MATLAB serial date number
13%
14%   Because FTP servers do not return directory information in a standard way,
15%   the last four fields in the structure may be empty or some items may
16%   be missing.
17
18% Matthew J. Simoneau, 14-Nov-2001
19% Copyright 1984-2011 The MathWorks, Inc.
20
21% Make sure we're still connected.
22connect(h)
23
24narginchk(0,2)
25if (nargin < 2)
26    str = '.';
27end
28
29ftp=h.jobject;
30ftp.setDataTimeout(15000); % in milliseconds
31listing=[];
32try
33    remoteFileList=ftp.listFiles(str);
34catch
35    remoteFileList = [];
36end
37form = java.text.SimpleDateFormat('dd-MMM-yyyy HH:mm:ss');
38%form = java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG,java.util.Locale.US);
39for i=1:length(remoteFileList);
40    file=remoteFileList(i);
41    listing(i,1).name=char(file.getName);
42    if nargout == 1
43        listing(i,1).bytes=file.getSize;
44        listing(i,1).isdir=~logical(file.isFile);
45        cal=file.getTimestamp;
46        if isempty(cal) % A user reported this coming back empty, see g575311.
47            listing(i,1).date = '';
48            listing(i,1).datenum = [];
49        else
50            listing(i,1).date=char(form.format(cal.getTime));
51            yr=cal.get(cal.YEAR);
52            mon=cal.get(cal.MONTH) + 1;
53            day=cal.get(cal.DATE);
54            hr=cal.get(cal.HOUR_OF_DAY);
55            min=cal.get(cal.MINUTE);
56            sec=cal.get(cal.SECOND);
57            listing(i,1).datenum=datenum(yr,mon,day,hr,min,sec);
58        end
59    end
60end
61
62% For Window's FTP server, listFiles is empty.  Just get the names.
63if isempty(listing)
64    names=ftp.listNames(str);
65    for i=1:length(names)
66        listing(i,1).name=char(names(i));
67        listing(i,1).date='';
68        listing(i,1).bytes=[];
69        listing(i,1).isdir=[];
70        listing(i,1).datenum=[];
71    end
72end
73
74switch nargout
75case 0
76    disp(' ');
77    if ~isempty(listing)
78        disp(convertListToColumns(char(listing.name)));
79    end
80    disp(' ');
81case 1
82    varargout={listing};
83end
84
85%================================================================================
86function list=convertListToColumns(list)
87
88% Pad the list out with two extra spaces to separate the columns
89list=[list ' '*ones(size(list,1),2)];
90
91% Calculate the number of columns that fit on the screen
92windowWidth=get(0,'CommandWindowSize')*[1;0];
93numberOfColumns=floor(windowWidth/size(list,2));
94if (numberOfColumns==0)
95    numberOfColumns=1;
96end
97
98% Calculate the number of rows and pad out the remaining column
99rows=ceil(size(list,1)/numberOfColumns);
100pad=rows*numberOfColumns-size(list,1);
101list=[list;' '*ones(pad,size(list,2))];
102
103% Reshape the list into the columns (trick from Zhiping)
104[r,c]=size(list);
105[I,J]=find(ones(rows,r*c/rows));
106ind=sub2ind(size(list), floor((J-1)/c)*rows+I, rem(J-1,c)+1);
107list=reshape(list(ind),rows,r*c/rows);
Note: See TracBrowser for help on using the repository browser.