Main Content

fileattrib

Set or get attributes of file or folder

Description

example

fileattriblists 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 of0if the attribute is off,1if the attribute is on, andNaNif the attribute does not apply.

fileattribis similar to the DOSattribcommand, or the UNIX®chmodcommand.

Note

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

example

fileattribfilenamelists the attribute values for the named file or folder.

example

fileattribfilenameattribssets the specified attributes for the named file or folder.

example

fileattribfilenameattribsuserssets the file or folder attributes for the specified subset of users.

example

fileattribfilenameattribsusersssets the specified attributes for the specified users for the contents of the named folder.

example

[status,values] = fileattrib(filename)returns the status and the lastsuccessfullyset attribute values for the named file or folder. If the file exists,statusis1. Otherwise,statusis0.

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 isC:\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 filemyfile.mon 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.

fileattribmyfile.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 folderC:\my_MATLAB_files\doc. The attributes indicate that you have read, write, and execute permissions for the folder.

fileattribC:\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/publicon 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

Makemyfile.mwritable.

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

Make the folder/home/work/resultsa read-only folder forall userson 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 folderD:\work\resultsand all its contents read-only and hidden. Because a value for theusersargument is not applicable on Windows systems,usersis 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 folderresultsand return them as a structure. Astatusvalue of 1 indicates that the operation is successful. The structurevaluescontains 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 forresults.

values.Name
ans = D:\work\results

Get the attributes for all files in the current folder with names that begin withnew. The returned1x3structure arrayvaluesindicates 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. Thestatusvalue of1indicates that the set operation was successful. Therefore, no errormsgormsgIDis 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. Thestatusvalue of0indicates that the set operation was unsuccessful. The minus sign incorrectly appears afterw,而不是之前。msgdescribes the error that occurred andmsgIDcontains the message identifier for the error that occurred.

[status,msg,msgID] = fileattrib('C:\my_MATLAB_files\doc',...'+h w-','','s')
状态= 0味精= t非法文件模式字符he current platform. msgID = MATLAB:FILEATTRIB:ModeSyntaxError

Input Arguments

collapse all

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

Example:fileattrib('myfile.m')

Data Types:char|string

文件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 thoughfileattribdisables 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'

当前的user

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

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

Output Arguments

collapse all

Indication of whether attempt to set or get attributes was successful, specified as0or1. If the attempt to set or get attributes was successful,statusis1. Otherwise,statusis0.

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), orNaN(not applicable)
system 0(not set),1(set), orNaN(not applicable)
hidden 0(not set),1(set), orNaN(not applicable)
directory 0(not set),1(set), orNaN(not applicable)
UserRead 0(not set),1(set), orNaN(not applicable)
UserWrite 0(not set),1(set), orNaN(not applicable)
UserExecute 0(not set),1(set), orNaN(not applicable)
GroupRead 0(not set),1(set), orNaN(not applicable)
GroupWrite 0(not set),1(set), orNaN(not applicable)
GroupExecute 0(not set),1(set), orNaN(not applicable)
OtherRead 0(not set),1(set), orNaN(not applicable)
OtherWrite 0(not set),1(set), orNaN(not applicable)
OtherExecute 0(not set),1(set), orNaN(not applicable)

Note

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

Error message, specified as a character vector. Ifstatusis0,msgcontains the message text of the error. Ifstatusis1,msgis empty,''.

Error message identifier, specified as a character vector. Ifstatusis0,msgIDcontains the message id of the error. Ifstatusis1,msgIDis empty,''.

Version History

Introduced before R2006a