4

I have found some strange behaviour in Matlab's fileattrib function on Windows. With certain file names it wrongly identifies the file as a hidden, system folder.

To test it, download this file (the file is empty; it's only the file name that matters):

https://docs.google.com/file/d/0B9BeckFuQk1bNHY3T0NKaFpxbUU/edit?usp=sharing

Put the file on an empty folder (I'm using "c:\temp") and try this:

fileattrib('c:\temp\*')

If your Matlab is like mine, it will give you this wrong result:

ans = 
            Name: 'c:\temp\?aaa.txt'
         archive: 1
          system: 1
          hidden: 1
       directory: 1
              [...]

Now rename the file name removing the first character and try again. It will correctly say

ans = 
            Name: 'c:\temp\aaa.txt'
         archive: 1
          system: 0
          hidden: 0
       directory: 0
            [...]

I have seen this behaviour in Matlab R2010b and R2007a, on Windows Vista and 7.

The problem clearly has to do with certain "offending" characters (or character sets/encodings?), but I've no idea. Can someone figure out why this happens? And how to work around it?

EDIT:

This seems to have been corrected in R2015a (maybe earlier): it correctly returns

        Name: 'C:\Users\Luis\Desktop\tmp\�aaa.txt'
     archive: 1
      system: 0
      hidden: 0
   directory: 0
        [...]
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 3
    It may be an encoding issue. On my system the `?`, shows up as `�`. For what it's worth, in R2013 and OS X 10.4, the "hidden" attribute is `NaN` (I think that `fileattrib` may not bother on UNIX systems as it doesn't even recognize file prefixed with `'.'` as hidden - or it's using the visibility attribute in OS X - or it's another bug). – horchler Sep 05 '13 at 23:25
  • @horchler Thanks for the added info. Strange behaviour indeed. On Windows the first character is a white question sign inside a black rhombus – Luis Mendo Sep 05 '13 at 23:26
  • However, if I copy and paste the `?` character into the file name, Matlab -on OS X at least- shows it fine (hidden attribute is still `NaN` though). Maybe your file upload/download changed the characters. – horchler Sep 05 '13 at 23:29
  • Yes, this happens to me only with files I've downloaded. Still, I can understand that the character shows wrongly, but that `fileattrib` is fooled into thinking the file is hidden folder...? – Luis Mendo Sep 05 '13 at 23:31
  • You could look into if adjusting your character encoding changes anything: http://stackoverflow.com/questions/4984532/unicode-characters-in-matlab-source-files – horchler Sep 05 '13 at 23:36
  • 2
    Interestingly, the link to MAtlab's doc in that answer says "MATLAB might not properly handle character codes greater than 2 bytes" – Luis Mendo Sep 05 '13 at 23:40
  • @LuisMendo: I think you might be talking about the Unicode replacement character [U+FFFD](http://www.fileformat.info/info/unicode/char/fffd/index.htm) �: http://en.wikipedia.org/wiki/Specials_%28Unicode_block%29 . How did you create the file? – Amro Sep 06 '13 at 01:16
  • @Amro Yes, it's that one. The original file was donwloaded from somewhere, I don't remember. It came with that name. Actually, I just copied that character from that file's name into the test file's name. As soon as that character is in the name, the problem arises – Luis Mendo Sep 06 '13 at 08:46
  • Workaround seems simple enough (if possible): Find the strange filenames and change them. --- How do you actually want to use this? – Dennis Jaheruddin Sep 25 '13 at 12:38
  • @DennisJaheruddin I'm using it in an incremental backup program. When one of those strange filenames is found, the file is wrongly interpreted as a folder, and the backup fails with that file. I have to manually change filename and run the backup program again, which is cumbersome. The problem is that I don't know in advance which files need the name changed – Luis Mendo Sep 25 '13 at 23:40

1 Answers1

1

One way to deal with this is not to depend (solely) on the fileattrib command.

In order to determine whether something is a file or directory, you can check how it registers when using the dir command on the containing folder.

Its a bit of a hassle, but when using dir called on the folder (won't work when called on the file directly) you seem to get the correct output.


A quick and dirty alternative would of course be to put your entire handling in a try / catch construction and if one fails simply try the other.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • Thanks for your time. Actually, I started using with `dir`, and it works fine, but it is much slower than `fileattrib` (and I have tens of thousands of files). The `try/catch` approach is a good idea, thanks. Although trying to copy the file-seen-as-a-folder won't actually throw an exception, the `dos` command I use does return a result and a status, from which I can infer if something went wrong and in that case try the other option. – Luis Mendo Sep 27 '13 at 09:59