Documentation Center

  • Trial Software
  • Product Updates
라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

fileattrib

Set or get attributes of file or folder

Syntax

  • fileattrib(name,attribs) example
  • fileattrib(name,attribs,users) example
  • fileattrib(name,attribs,users,'s') example
  • [status,message,messageid] = fileattrib(name,attribs,___) example
  • [status,message] = fileattrib(name) example

Description

example

fileattrib gets attribute values for the current folder, using the following structure, where Name is always a string containing the current folder name. For the other fields, a value of 0 indicates that the attribute is off, 1 indicates that the attribute is on, and NaN indicates that the attribute does not apply:

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

example

fileattrib(name) gets the attribute values for the named file or folder.

example

fileattrib(name,attribs) sets the specified attributes for the named file or folder.

example

fileattrib(name,attribs,users) sets the file or folder attributes for the specified subset of users.

example

fileattrib(name,attribs,users,'s') sets the specified attributes for the specified users for the contents of the named folder.

example

[status,message,messageid] = fileattrib(name,attribs,___) sets the specified file attributes and gets the function status:

  • If status is 0, then message is the error message, and messageid is the error message identifier.

  • If status is 1, then message is a structure containing the attributes of the named file or folder, and messageid is an empty string.

example

[status,message] = fileattrib(name) gets status and the last successfully set attribute structure values for the named file or folder and returns the structure to message. (status is always 1.)

Examples

expand all

View Current Folder Attributes

View attributes of the current folder, assuming the current folder is C:\my_MATLAB_files.

fileattrib
ans = 

            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

The attributes indicate that you have read, write, and execute permissions for the current folder.

View File Attributes

View attributes of file collatz.m.

fileattrib('collatz.m')
ans = 

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

The attributes indicate that the specfieid item is a file. You can read and execute the file, but cannot update it. The file is archived.

View Folder Attributes on a Windows System

View attributes for the folder C:\my_MATLAB_files\doc.

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

The attributes indicate that you have read, write, and execute permissions for the specified folder.

View Folder Attributes on a UNIX System

View attributes for the folder /public on a UNIX® system.

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

The attributes indicate that you have read, write, and execute permissions for the specfied folder. In addition, users in your UNIX group and all others have read and execute permissions for the specified folder, but not write permissions.

Set File Attributes

Make myfile.m writeable.

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

Set File Attributes for Specific Users on UNIX

Make the folder /home/work/results a read-only folder for all users on UNIX platforms.

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

The minus (-) preceding the write attribute, w, removes the write status.

Set Attributes for Folder and Its Contents

On Windows® platforms, make the folder D:\work\results and all its contents read only and hidden.

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

Because a value for the users argument is not applicable on Windows systems, the argument is an empty string. The s argument applies the hidden and read-only attributes to the contents of the folder.

Get Attributes Structure for a Folder

Get the attributes for the folder results and return them to a structure:

[stat,struc]=fileattrib('results')
stat =
     1

struc = 
            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, value of 1. The structure, struc, contains the file attributes.

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

struc.Name
ans =
D:\work\results

Get Attributes Structure for Multiple Files

Get the attributes for all files in the current folder with names that begin with new.

[stat,struc]=fileattrib('new*')
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.

View the file names.

struc.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.

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

Successfully Set Attributes for a File and Get Messages

Show output that results when an attempt to set file attributes is successful.

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

     1


message =

     ''



messageid =

     ''

The status value of 1 indicates the set operation was successful; therefore, no error message or messageid is returned.

Unsuccessfully Set Attributes for a File and Get Messages

Show output that results when an attempt to set file attributes is unsuccessful.

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

     0


message =

Illegal file mode characters on the current platform.


messageid =

MATLAB:FILEATTRIB:ModeSyntaxError

The status value of 0 indicates the set operation was unsuccessful. The minus sign incorrectly appears after w, instead of before it.

Input Arguments

expand all

name — File or folder namestring

The absolute or relative path for a folder or file, specified as a string. To specify all names beginning with certain characters, add the wildcard character, *.

Example: fileattrib('myfile.m')

attribs — File or folder attribute values'a' | 'h' | 's' | 'w' | 'x'

File or folder attribute values, specified as a space delimited string. Use the plus (+) qualifier before an attribute to set it, and the minus (-) qualifier before an attribute to clear it.

attribvalue

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

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

users — Subset of users'a' | 'g' | 'o' | 'u' | ''

Subset of users (on UNIX platforms only), specified as a string. For all platforms other than UNIX, specify the users argument as an empty string, ''. This value is not returned by fileattrib get operations.

Value for UNIX Users

Description

'a'

All users on UNIX platforms

'g'

Group of users

'o'

All other users

'u'

Current user

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

Output Arguments

expand all

status — Indication of whether attempt to set attribute was successful0 | 1

If attempt to set attribute was unsuccessful, status is 0. Otherwise, status is 1.

message — Attribute structure or error messagestring | structure array

Attribute structure or error message, depending on whether you are setting or getting attributes and status.

Getting or Setting AttributesStatusMessage contents
Setting0Error message
Setting1Empty string
Getting1Structure containing file attributes and values

When you are getting file attributes, the structure contains these fields and possible values.

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

messageid — Error message identifierstring

Error message identifier when the attempt to set attribute is unsuccessful (status is 0), returned as a string. If status is 1, then messageid is an empty string.

More About

expand all

Tips

  • fileattrib is like the DOS attrib command, or the UNIX chmod command. [1]

See Also

| | | | | | |


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

Was this topic helpful?