* Bug #15472 fixed - mdelete('test') worked like mdelete('test.*') under Windows
[scilab.git] / scilab / modules / fileio / macros / listfiles.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2007 - INRIA - Allan CORNET
3 // Copyright (C) 2012 - Scilab Enterprises - Antoine ELIAS
4 //
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 //
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
13
14
15 function files= listfiles(paths,flag,flagexpand)
16
17     // list the files in a set of directories
18     // files= listfiles(paths [,flag,flagexpand])
19     // paths           : a string matrix giving a set of pathnames (eventually ended by a pattern built with  * )
20     // flag,flagexpand : boolean optional parameters. (default value  %t ).
21     // files           : a string matrix
22
23     [lhs,rhs]=argn(0)
24     if rhs < 1  then paths="./" ; end
25     if rhs <= 1 then flag = %t ; end
26     if rhs <= 2 then flagexpand = %t ; end
27     files=[];
28
29     // Stripblanks paths
30     paths = stripblanks(paths);
31
32     // list files of the current directory
33     paths(find(paths=="")) = "./";
34
35     if flag == %t then
36         paths = pathconvert(paths,%f,flagexpand);
37     end
38
39     // list files of the root directory
40     if getos() <> "Windows" then
41         paths(find(paths=="")) = "/";
42     end
43
44     DIR_SEPARATOR=filesep();
45
46     bMulti = %f;
47     if size(paths,"*") > 1 then
48         bMulti = %t;
49     end
50
51     for i=1:size(paths,"*")  // Loop on the file/path list
52         if isdir(paths(i)) then // It is a directory then returns all the file in the dir
53             path = paths(i) + filesep();
54             fname = "*";
55             extension = "";
56         else //isfile
57             [path, fname, extension] = fileparts(paths(i)); // Retrieve the information
58             if path == "" then
59                 path = "./";
60             end
61             
62             if fname == "" then
63                 fname = "*";
64             end
65         end
66
67         filesi=findfiles(path,fname+extension);
68
69         if filesi<>[] then
70             filesi=filesi($:-1:1);
71             // prepend with the path, if required
72             // when listing a full directory, path is not prepended
73             dirs=paths(i);
74             if part(dirs,length(dirs))<>DIR_SEPARATOR then
75                 if isdir(dirs) then
76                     if bMulti then //yes
77                         with_dir=%t;
78                         dirs = dirs + filesep();
79                     else
80                         with_dir=%f;
81                     end
82                 else                //no
83                     k=strindex(dirs,DIR_SEPARATOR);
84                     if k==[] then
85                         with_dir=%f;
86                     else
87                         dirs=part(dirs,1:k($));
88                         with_dir=%t;
89                     end
90                 end
91             else
92                 with_dir=%f;
93             end // if part(dirs,length(dirs) ...
94
95             if with_dir then
96                 filesi = dirs+filesi;
97             end
98
99             files=[files;filesi];
100         end // if filesi<>[] then
101     end // for i=1:size(paths,'*')
102 endfunction