Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

fileattrib - Set or get attributes of file or folder

Syntax

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

Description

fileattrib displays the attributes for the current folder. fileattrib is like the DOS attrib command, or the chmod command used on UNIX[1] platforms.

Values are as follows.

Value

Description

0

Attribute is off

1

Attribute is on

NaN

Attribute does not apply

fileattrib('name') displays the attributes for name, where name is the absolute or relative path for a folder or file. Use a 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 folder or file. Specify the + qualifier before the attribute to set it, and specify the - qualifier before the attribute to clear it. Use a wildcard, *, at the end of name to set attributes for all matches. 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). Results differ by platform and application. For example, even though fileattrib disables the "write" privilege for a folder, files in the folder could be writable for some platforms or applications.

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 folder or file. The users argument 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. The following table lists values for users.

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 folder and its contents. Here, s applies attrib to all contents of name, where name is a folder.

[status,message,messageid] = fileattrib(...) sets the attribute for name, returning the status, a message, and the MATLAB error message ID. Here, status is 1 for success and 0 for error. If you do not specify attrib, users, and s, and the status is 1, then message is a structure containing the file attributes and messageid is blank. If status is 0, then messageid contains the error. To return message as a structure, use a wildcard * at the end of name.

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 folder d:/work/results be a read-only folder for all users, type:

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

The - preceding the write attribute, w, removes the write status.

Set Multiple Attributes for Folder and Its Contents

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

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

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

Return Status and Structure of Attributes

To return the attributes for the folder 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 is 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, type:

mess.Name

MATLAB returns the path for results

ans =
d:\work\results

Return Attributes with Wildcard for Name

To return the attributes for all files in the current folder whose names begin with new, type:

[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, ls, mkdir, movefile, rmdir

Managing Files in MATLAB


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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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