MATLAB Function Reference Previous page   Next Page
fileattrib

Set or get attributes of file or directory

Syntax

Description

The fileattrib function is like the DOS attrib command or the UNIX chmod command.

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 pathname 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 pathname 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 (Windows only)
h
Hidden file (Windows only)
s
System file (Windows only)
w
Write access (Windows and UNIX)
x
Executable (UNIX 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 pathname for a directory or file, and defines which users are affected by attrib, where users is applicable only for UNIX systems. For more information about these attributes, see UNIX reference information for chmod. The default value for users is u. Values for users are

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 pathname 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 lasterr). 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.

Examples

Get Attributes of File

To view the attributes of myfile.m, type

MATLAB returns

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

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

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, 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

MATLAB returns

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

returns the path for results

Return Attributes with Wildcard for Name

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

MATLAB returns

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

MATLAB returns

To view just the first filename, type

See Also

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


Previous page  figurepalette filebrowser Next page

© 1994-2005 The MathWorks, Inc.