fileattrib - Set or get attributes of file or directory

Graphical Interface

As an alternative to the fileattrib function, you can view attributes using the Current Directory browser.

Syntax

fileattrib
fileattrib('name')
fileattrib('name','attrib')
fileattrib('name','attrib','users')
fileattrib('name','attrib','users','s')
[status,message,messageid] = fileattrib('name','attrib','users','s')

Description

The fileattrib function is like the DOS attrib command, or the chmod command used on UNIX[1] platforms.

fileattrib displays the attributes for the current directory. Values are as follows.

Value

Description

0

Attribute is off

1

Attribute is set (on)

NaN

Attribute does not apply

fileattrib('name') displays the attributes for name, where name is the absolute or relative path for a directory or file. Use the wildcard * at the end of name to view attributes for all matching files.

fileattrib('name','attrib') sets the attribute for name, where name is the absolute or relative path for a directory or file. Specify the + qualifier before the attribute to set it, and specify the - qualifier before the attribute to clear it. Use the wildcard * at the end of name to set attributes for all matching files. Values for attrib are as follows.

Value for attrib

Description

a

Archive (Microsoft Windows platform only)

h

Hidden file (Windows platform only)

s

System file (Windows platform only)

w

Write access (Windows and UNIX platforms)

x

Executable (UNIX platform only)

For example, fileattrib('myfile.m','+w') makes myfile.m a writable file.

fileattrib('name','attrib','users') sets the attribute for name, where name is the absolute or relative path for a directory or file, and defines which users are affected by attrib, where users is applicable only for UNIX platforms. For more information about these attributes, see reference information for chmod in UNIX operating system documentation. The default value for users is u. Values for users are listed here.

Value for users

Description

a

All users

g

Group of users

o

All other users

u

Current user

fileattrib('name','attrib','users','s') sets the attribute for name, where name is the absolute or relative path for a file or a directory and its contents, and defines which users are affected by attrib. Here the s specifies that attrib be applied to all contents of name, where name is a directory.

[status,message,messageid] = fileattrib('name','attrib','users','s') sets the attribute for name, returning the status, a message, and the MATLAB error message ID (see error and lasterror). Here, status is 1 for success and is 0 for error. If attrib, users, and s are not specified, and status is 1, message is a structure containing the file attributes and messageid is blank. If status is 0, messageid contains the error. If you use a wildcard * at the end of name, mess will be a structure.

Remarks

Not all platforms or applications respond to file attributes that you have set in the same way. For example, even though you disable the "write" privilege for a directory with fileattrib, files in that directory could still be writable on some platforms or by certain applications.

Examples

Get Attributes of File

To view the attributes of myfile.m, type

fileattrib('myfile.m')

MATLAB returns

            Name: 'd:/work/myfile.m'
         archive: 0
          system: 0
          hidden: 0
       directory: 0
        UserRead: 1
       UserWrite: 0
     UserExecute: 1
       GroupRead: NaN
      GroupWrite: NaN
    GroupExecute: NaN
       OtherRead: NaN
      OtherWrite: NaN
    OtherExecute: NaN

UserWrite is 0, meaning myfile.m is read only. The Group and Other values are NaN because they do not apply to the current operating system, Windows.

Set File Attribute

To make myfile.m become writable, type

fileattrib('myfile.m','+w')

Running fileattrib('myfile.m') now shows UserWrite to be 1.

Set Attributes for Specified Users

To make the directory d:/work/results be a read-only directory for all users, type

fileattrib('d:/work/results','-w','a')

The - preceding the write attribute, w, specifies that write status is removed.

Set Multiple Attributes for Directory and Its Contents

To make the directory d:/work/results and all its contents be read only and be hidden, on Windows platforms, type

fileattrib('d:/work/results','+h-w','','s')

Because users is not applicable on Windows systems, its value is empty. Here, s applies the attribute to the contents of the specified directory.

Return Status and Structure of Attributes

To return the attributes for the directory results to a structure, type

[stat,mess]=fileattrib('results')

MATLAB returns

stat =
     1

mess = 
            Name: 'd:\work\results'
         archive: 0
          system: 0
          hidden: 0
       directory: 1
        UserRead: 1
       UserWrite: 1
     UserExecute: 1
       GroupRead: NaN
      GroupWrite: NaN
    GroupExecute: NaN
       OtherRead: NaN
      OtherWrite: NaN
    OtherExecute: NaN

The operation was successful as indicated by the status, stat, being 1. The structure mess contains the file attributes. Access the attribute values in the structure. For example, typing

mess.Name

returns the path for results

ans =
d:\work\results

Return Attributes with Wildcard for Name

Return the attributes for all files in the current directory whose names begin with new.

[stat,mess]=fileattrib('new*')

MATLAB returns

stat =
     1

mess = 
1x3 struct array with fields:
    Name
    archive
    system
    hidden
    directory
    UserRead
    UserWrite
    UserExecute
    GroupRead
    GroupWrite
    GroupExecute
    OtherRead
    OtherWrite
    OtherExecute

The results indicate there are three matching files. To view the file names, type

mess.Name

MATLAB returns

ans =
d:\work\results\newname.m

ans =
d:\work\results\newone.m

ans =
d:\work\results\newtest.m

To view just the first file name, type

mess(1).Name

ans =
d:\work\results\newname.m

See Also

copyfile, cd, dir, filebrowser, fileparts, ls, mfilename, mkdir, movefile, rmdir

Managing Files and Working with the Current Directory


[1] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS