dir / ls routines return values

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

dir / ls routines return values

Daniel Sebald
The dir() and ls() routines return strings for file names in a directory, but they place breaks in file names with spaces, which ostensibly makes the routines not useful for programming.  [I'm not a fan of spaces in file names... one of the most confusing and messy conventions in computer programming.]

For example, try

  mkdir('dir with spaces and # @ $ % [ ]')
  dir('./')
  ls -1
  d = ls -1

Note a couple things.  The list returned by dir and ls have line breaks, so there is no way to know exactly where line breaks and spaces are in files.  There is no robust way of using dir and ls that I see.

The second thing is that 'ls -1' gives a result that looks correct, spaces intact.  But 'ls -1' by itself does not return anything into 'ans'.  That's strange and inconsistent.

Dan
Reply | Threaded
Open this post in threaded view
|

dir / ls routines return values

John W. Eaton
Administrator
On 25-Jan-2009, Daniel J Sebald wrote:

| The dir() and ls() routines return strings for file names in a directory, but they place breaks in file names with spaces, which ostensibly makes the routines not useful for programming.  [I'm not a fan of spaces in file names... one of the most confusing and messy conventions in computer programming.]
|
| For example, try
|
|   mkdir('dir with spaces and # @ $ % [ ]')
|   dir('./')
|   ls -1
|   d = ls -1
|
| Note a couple things.  The list returned by dir and ls have line
| breaks, so there is no way to know exactly where line breaks and
| spaces are in files.  There is no robust way of using dir and ls
| that I see.

If you write

  x = dir ('./')

then you will get a structure array.  One of the fields is "name", and
I don't see a problem with spaces if you use this form.

| The second thing is that 'ls -1' gives a result that looks correct,
| spaces intact.  But 'ls -1' by itself does not return anything into
| 'ans'.  That's strange and inconsistent.

It's not uncommon for functions in Matlab (and Octave) to do different
things based on the value of nargout.  In the case of ls, if nargout
is zero, the result is printed.  If nargout is 1, it is returned.  I
don't see a bug here.

I'd recommend using the dir function if you are doing programming.
The ls function is handy for the command line though, so I think we
should keep it.  But I don't see a good enough reason to change the
way it works.

jwe
Reply | Threaded
Open this post in threaded view
|

Re: dir / ls routines return values

Daniel Sebald
John W. Eaton wrote:

> On 25-Jan-2009, Daniel J Sebald wrote:
>
> | The dir() and ls() routines return strings for file names in a directory, but they place breaks in file names with spaces, which ostensibly makes the routines not useful for programming.  [I'm not a fan of spaces in file names... one of the most confusing and messy conventions in computer programming.]
> |
> | For example, try
> |
> |   mkdir('dir with spaces and # @ $ % [ ]')
> |   dir('./')
> |   ls -1
> |   d = ls -1
> |
> | Note a couple things.  The list returned by dir and ls have line
> | breaks, so there is no way to know exactly where line breaks and
> | spaces are in files.  There is no robust way of using dir and ls
> | that I see.
>
> If you write
>
>   x = dir ('./')
>
> then you will get a structure array.  One of the fields is "name", and
> I don't see a problem with spaces if you use this form.

Oh yeah, I didn't notice that.  So guess there is a route.  It's not as nice as a cell array, but it works.

On an older version of Octave both the 0-output return and the 1-output return have the spaces replaced by line breaks.

Dan
Reply | Threaded
Open this post in threaded view
|

Re: dir / ls routines return values

Jaroslav Hajek-2
On Tue, Jan 27, 2009 at 8:33 AM, Daniel J Sebald <[hidden email]> wrote:

> John W. Eaton wrote:
>>
>> On 25-Jan-2009, Daniel J Sebald wrote:
>>
>> | The dir() and ls() routines return strings for file names in a
>> directory, but they place breaks in file names with spaces, which ostensibly
>> makes the routines not useful for programming.  [I'm not a fan of spaces in
>> file names... one of the most confusing and messy conventions in computer
>> programming.]
>> | | For example, try
>> | |   mkdir('dir with spaces and # @ $ % [ ]')
>> |   dir('./')
>> |   ls -1
>> |   d = ls -1
>> | | Note a couple things.  The list returned by dir and ls have line
>> | breaks, so there is no way to know exactly where line breaks and
>> | spaces are in files.  There is no robust way of using dir and ls
>> | that I see.
>>
>> If you write
>>
>>  x = dir ('./')
>>
>> then you will get a structure array.  One of the fields is "name", and
>> I don't see a problem with spaces if you use this form.
>
> Oh yeah, I didn't notice that.  So guess there is a route.  It's not as nice
> as a cell array, but it works.
>

Why it's not as nice? Converting the struct field to a cell array is a
trivial matter.
For instance, you can simply do (note the absence of space):
{dir('./').name}
to get the names.


> On an older version of Octave both the 0-output return and the 1-output
> return have the spaces replaced by line breaks.
>

Personally, I didn't even know ls could return a value, but if you can
create a patch improving backward compatibility, that would probably
be welcome. But anyway I suggest to do the programming with dir, which
has a well-defined output.

cheers

--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz