fileattrib - Set or get attributes of file or folder - MATLAB (original) (raw)

Set or get attributes of file or folder

Syntax

Description

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](#btb02ie-1-filename) lists the attribute values for the named file or folder.

example

fileattrib [filename](#btb02ie-1-filename) [attribs](#btb02ie-1-attribs) sets the specified attributes for the named file or folder.

example

fileattrib [filename](#btb02ie-1-filename) [attribs](#btb02ie-1-attribs) [users](#btb02ie-1-users) sets the file or folder attributes for the specified subset of users.

example

fileattrib [filename](#btb02ie-1-filename) [attribs](#btb02ie-1-attribs) [users](#btb02ie-1-users) s sets the specified attributes for the specified users for the contents of the named folder.

example

[[status](#btb02ie-1-status),[values](#btb02ie-1-values)] = fileattrib([filename](#btb02ie-1-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](#btb02ie-1-status),[msg](#btb02ie-1-msg),[msgID](#btb02ie-1-msgID)] = fileattrib([filename](#btb02ie-1-filename),[attribs](#btb02ie-1-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.

example

Examples

collapse all

View Current Folder Attributes on Windows

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.

        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 File Attributes on Windows

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.

        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 Folder Attributes on Windows

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 Folder Attributes on UNIX

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.

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

Set File Attributes on Windows

Make myfile.m writable.

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

Set File Attributes for All Users on UNIX

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

Set Attributes for Folder and Its Contents on Windows

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 Attributes Structure for a Folder on Windows

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.

Get Attributes Structure for Multiple Files on Windows

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.

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

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

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

View just the second file name.

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

Successfully Set Attributes for a File and Get Messages on Windows

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 =

 ''

Unsuccessfully Set Attributes for a File and Get Messages on Windows

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

filename — File or folder name

character vector | string scalar

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

attribs — File or folder attribute values

character vector | string scalar

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

users — Subset of users

'a' | 'g' | 'o' | 'u' | ''

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

status — Indication of whether attempt to set or get attributes was successful

0 | 1

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.

values — Attribute structure

structure array

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

Field name Possible Values
Name Character vector containing name of file or folder
archive 0 (not set), 1 (set), or NaN (not applicable)
system 0 (not set), 1 (set), or NaN (not applicable)
hidden 0 (not set), 1 (set), or NaN (not applicable)
directory 0 (not set), 1 (set), or NaN (not applicable)
UserRead 0 (not set), 1 (set), or NaN (not applicable)
UserWrite 0 (not set), 1 (set), or NaN (not applicable)
UserExecute 0 (not set), 1 (set), or NaN (not applicable)
GroupRead 0 (not set), 1 (set), or NaN (not applicable)
GroupWrite 0 (not set), 1 (set), or NaN (not applicable)
GroupExecute 0 (not set), 1 (set), or NaN (not applicable)
OtherRead 0 (not set), 1 (set), or NaN (not applicable)
OtherWrite 0 (not set), 1 (set), or NaN (not applicable)
OtherExecute 0 (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.

msg — Error message

character vector

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

msgID — Error message identifier

character vector

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