Main Content

fileattrib

Set or get attributes of file or folder

Description

example

fileattrib lists the attribute values for the current folder. The values are listed using this structure:

Name
archive
system
hidden
directory
UserRead
UserWrite
UserExecute
GroupRead
GroupWrite
GroupExecute
OtherRead
OtherWrite
OtherExecute

The first field, Name, displays the file or folder name. The remaining fields display a value of 0 if the attribute is off, 1 if the attribute is on, and NaN if the attribute does not apply.

fileattrib is similar to the DOS attrib command, or the UNIX® chmod command.

Note

In Windows®, setting the write access attribute ('w') to read-only does not necessarily prevent write access. Therefore, the value for UserWrite may differ from what is expected.

example

fileattrib filename lists the attribute values for the named file or folder.

example

fileattrib filename attribs sets the specified attributes for the named file or folder.

example

fileattrib filename attribs users sets the file or folder attributes for the specified subset of users.

example

fileattrib filename attribs users s sets the specified attributes for the specified users for the contents of the named folder.

example

[status,values] = fileattrib(filename) returns the status and the last successfully set attribute values for the named file or folder. If the file exists, status is 1. Otherwise, status is 0.

example

[status,msg,msgID] = fileattrib(filename,attribs,___) sets the specified file attributes and returns the status of the operation as well as an error message and error message identifier if the set operation is unsuccessful.

Examples

collapse all

View attributes of the current folder on a Windows system, assuming the current folder is C:\my_MATLAB_files. The attributes indicate that you have read, write, and execute permissions for the current folder.

fileattrib
            Name: 'C:\my_MATLAB_files'
         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

View attributes of the file myfile.m on a Windows system. The attributes indicate that the specified item is a file. You can read and execute the file, but cannot update it. The file is archived.

fileattrib myfile.m
            Name: 'C:\my_MATLAB_files\myfile.m'
         archive: 1
          system: 0
          hidden: 0
       directory: 0
        UserRead: 1
       UserWrite: 0
     UserExecute: 1
       GroupRead: NaN
      GroupWrite: NaN
    GroupExecute: NaN
       OtherRead: NaN
      OtherWrite: NaN
    OtherExecute: NaN

View attributes for the folder C:\my_MATLAB_files\doc. The attributes indicate that you have read, write, and execute permissions for the folder.

fileattrib C:\my_MATLAB_files\doc
ans = 

            Name: 'C:\my_MATLAB_files\doc'
         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

View attributes for the folder /public on a UNIX system. The attributes indicate that you have read, write, and execute permissions for the folder. In addition, users in your UNIX group and all others have read and execute permissions for the folder, but not write permissions.

fileattrib /public
ans = 

            Name: '/public'
         archive: NaN
          system: NaN
          hidden: NaN
       directory: 1
        UserRead: 1
       UserWrite: 1
     UserExecute: 1
       GroupRead: 1
      GroupWrite: 0
    GroupExecute: 1
       OtherRead: 1
      OtherWrite: 0
    OtherExecute: 1

Make myfile.m writable.

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

Make the folder /home/work/results a read-only folder for all users on UNIX platforms. The minus (-) preceding the write attribute, w, removes the write access, making the file read-only.

fileattrib('/home/work/results','-w','a')

Make the folder D:\work\results and all its contents read-only and hidden. Because a value for the users argument is not applicable on Windows systems, users is specified as an empty character vector, ''. The 's' argument applies the hidden and write access attributes to the contents of the folder and to the folder itself.

fileattrib('D:\work\results','+h -w','','s')

Get the attributes for the folder results and return them as a structure. A status value of 1 indicates that the operation is successful. The structure values contains the attributes of the folder.

[status,values] = fileattrib('results')
status =
     1

values = 
            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

Access the name attribute value in the structure. MATLAB® returns the path for results.

values.Name
ans =
D:\work\results

Get the attributes for all files in the current folder with names that begin with new. The returned 1x3 structure array values indicates that there are three matching files.

[status,values] = fileattrib('new*')
status =
     1

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

View the file names.

values.Name
ans =
D:\work\results\newname.m

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

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

View just the second file name.

values(2).Name
ans =
D:\work\results\newname.m

Show output that results when an attempt to set file attributes is successful. The status value of 1 indicates that the set operation was successful. Therefore, no error msg or msgID is returned.

[status,msg,msgID] = fileattrib('C:\my_MATLAB_files\doc',...
'+h -w','','s')
status =

     1

msg =

     ''

msgID =

     ''

Show output that results when an attempt to set file attributes is unsuccessful. The status value of 0 indicates that the set operation was unsuccessful. The minus sign incorrectly appears after w, instead of before it. msg describes the error that occurred and msgID contains the message identifier for the error that occurred.

[status,msg,msgID] = fileattrib('C:\my_MATLAB_files\doc',...
'+h w-','','s')
status =

     0


msg =

Illegal file mode characters on the current platform.


msgID =

MATLAB:FILEATTRIB:ModeSyntaxError

Input Arguments

collapse all

File or folder name, specified as a character vector or string scalar. You can specify an absolute or relative path. filename can include wildcards (*).

Example: fileattrib('myfile.m')

Data Types: char | string

File or folder attribute values, specified as a character vector or string scalar consisting of one or more of these values separated by spaces:

Value

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, making it read-only, files in the folder could be writable for some platforms or applications.

'x'

Executable (UNIX platform only).

Use the plus (+) qualifier before an attribute to set it, and the minus (-) qualifier before an attribute to clear it.

Example: fileattrib('myfile.m', '+w -h')

Data Types: char | string

Subset of users (on UNIX platforms only), specified as one of these values:

Value for UNIX Systems

Description

'a'

All users

'g'

Group of users

'o'

All other users

'u'

Current user

Specify an empty value '' for all platforms other than UNIX. This value is not returned by fileattrib get operations.

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

Output Arguments

collapse all

Indication of whether attempt to set or get attributes was successful, specified as 0 or 1. If the attempt to set or get attributes was successful, status is 1. Otherwise, status is 0.

Attribute structure, specified as a structure array containing these fields and possible values:

Field namePossible Values
NameCharacter vector containing name of file or folder
archive0 (not set), 1 (set), or NaN (not applicable)
system0 (not set), 1 (set), or NaN (not applicable)
hidden0 (not set), 1 (set), or NaN (not applicable)
directory0 (not set), 1 (set), or NaN (not applicable)
UserRead0 (not set), 1 (set), or NaN (not applicable)
UserWrite0 (not set), 1 (set), or NaN (not applicable)
UserExecute0 (not set), 1 (set), or NaN (not applicable)
GroupRead0 (not set), 1 (set), or NaN (not applicable)
GroupWrite0 (not set), 1 (set), or NaN (not applicable)
GroupExecute0 (not set), 1 (set), or NaN (not applicable)
OtherRead0 (not set), 1 (set), or NaN (not applicable)
OtherWrite0 (not set), 1 (set), or NaN (not applicable)
OtherExecute0 (not set), 1 (set), or NaN (not applicable)

Note

On Windows systems, setting the write access attribute ('w') to read-only does not necessarily prevent write access. Therefore, the value for UserWrite may differ from what is expected.

Error message, specified as a character vector. If status is 0, msg contains the message text of the error. If status is 1, msg is empty, ''.

Error message identifier, specified as a character vector. If status is 0, msgID contains the message id of the error. If status is 1, msgID is empty, ''.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a

See Also

| | | | | | |