Wednesday, August 5, 2009

fileattrib

Set or get attributes of file or directory

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 UNIX chmod command.

fileattrib displays the attributes for the current directory. Values are


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


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. The s argument is not supported on Windows 98 and ME.

[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 no 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

  • 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, 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 filenames, 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 filename, type

  • mess(1).Name

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

No comments:

Post a Comment