1 | function 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. |
---|
22 | connect(h) |
---|
23 | |
---|
24 | narginchk(0,2) |
---|
25 | if (nargin < 2) |
---|
26 | str = '.'; |
---|
27 | end |
---|
28 | |
---|
29 | ftp=h.jobject; |
---|
30 | ftp.setDataTimeout(15000); % in milliseconds |
---|
31 | listing=[]; |
---|
32 | try |
---|
33 | remoteFileList=ftp.listFiles(str); |
---|
34 | catch |
---|
35 | remoteFileList = []; |
---|
36 | end |
---|
37 | form = java.text.SimpleDateFormat('dd-MMM-yyyy HH:mm:ss'); |
---|
38 | %form = java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG,java.util.Locale.US); |
---|
39 | for 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 |
---|
60 | end |
---|
61 | |
---|
62 | % For Window's FTP server, listFiles is empty. Just get the names. |
---|
63 | if 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 |
---|
72 | end |
---|
73 | |
---|
74 | switch nargout |
---|
75 | case 0 |
---|
76 | disp(' '); |
---|
77 | if ~isempty(listing) |
---|
78 | disp(convertListToColumns(char(listing.name))); |
---|
79 | end |
---|
80 | disp(' '); |
---|
81 | case 1 |
---|
82 | varargout={listing}; |
---|
83 | end |
---|
84 | |
---|
85 | %================================================================================ |
---|
86 | function list=convertListToColumns(list) |
---|
87 | |
---|
88 | % Pad the list out with two extra spaces to separate the columns |
---|
89 | list=[list ' '*ones(size(list,1),2)]; |
---|
90 | |
---|
91 | % Calculate the number of columns that fit on the screen |
---|
92 | windowWidth=get(0,'CommandWindowSize')*[1;0]; |
---|
93 | numberOfColumns=floor(windowWidth/size(list,2)); |
---|
94 | if (numberOfColumns==0) |
---|
95 | numberOfColumns=1; |
---|
96 | end |
---|
97 | |
---|
98 | % Calculate the number of rows and pad out the remaining column |
---|
99 | rows=ceil(size(list,1)/numberOfColumns); |
---|
100 | pad=rows*numberOfColumns-size(list,1); |
---|
101 | list=[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)); |
---|
106 | ind=sub2ind(size(list), floor((J-1)/c)*rows+I, rem(J-1,c)+1); |
---|
107 | list=reshape(list(ind),rows,r*c/rows); |
---|