How to Change File Attributes – (ATTRIB, Windows Command Line, CMD, DOS)

What We'll Learn:

Welcome!
This guide is all about File Attributes and how to change them from the command prompt using the Attrib command.

  • We’ll start by learning, exactly what file attributes are and what function they serve.
  • We’ll then learn how to view or modify them using the attrib command directly from the command line.
  • How to use them to selectively hide files or entire directories and make them undetectable.
  • And finally, we’ll learn how to take advantage of wildcards and change file attributes on multiples files and folders all at once,

Lets get started!

What Are File Attributes?​

File attributes in windows are certain metadata associated with files and folders that define the way a user or the system can interact with that file or folder, as well as how it behaves.

By using file attributes the system and its users, grant or deny certain rights to every file and folder.

These rights dictate how the file or folder can be interacted with and what purpose it serves.

For example, files denoted with the “System” attribute are critical for the proper operation of the system and can only be used by the system itself, only users with the appropriate privileges can be allowed to read or write to them.

Here is a list of the most common file attributes:

  • Read-only (R) : Allows a file only to be read and not modified.
  • Archive (A) : Enables windows backups.
  • System (S) : Denotes the file as one that’s part of the operating system.
  • Hidden (H) : Hides the file in windows explorer.
  • Compressed (C) : Windows will compresses the file.
  • Encrypted (E) : Windows will encrypt the file.
  • Not Content-Indexed (I) : Windows will not index the file for searches.

How do they work?

While that is well and good, exactly how do file attributes work?

Each file attribute has two states on a file, set and cleared. Only a file that has a given attribute in the set state behaves accordingly to the behavior denoted by that attribute.

Which is a fancy way of saying that every file attribute already “exists” in a file, but to make its properties active, we must “enable” it.

We are going to learn how to do that, and much more in this guide/tutorial.

If you would like to follow along (you should), open your command prompt (not as an administrator), navigate to a directory of your choice, and use the following command to create an empty file and a folder.

echo. > File.txt & mkdir My_Folder

We are going to use the file and folder we just created, to learn how to view, set, edit and delete file attributes (they will basically be our guinea pigs).

Lets get started.

View File Attributes - Using Attrib​:

To view, set or clear file attributes using the windows command line we will make use of the Attrib command.

Attrib stands for attribute.

To view what attributes are currently set on the file we just created simply type the Attrib command followed by the name or location of our file or folder.

Attrib File.txt
ATTRIB Command & File Attributes - Examples, Options, Switches (CMD, DOS)​

The Attrib command will immediately display the first letter of every file attribute that is currently set on the specified file or folder. 

By default new files only have the archive attribute set. 

If the command line did not manage to find your file even tho you are sure its located in your current directory it might be because the name of your file contains a space

If that’s the case you need to surround the name of your file in quotes, so that the command line knows that the spaces separating the words do not constitute an additional parameter and are just a continuation of the name of our file. 

For example instead of My File.txt type “My File.txt” .

Now that we know how to view every set file attribute, lets learn how we can change/modify them.

Change File Attributes - Using Attrib:

To change, add or remove an attribute for a file, use the Attrib command followed by a minus (-) or plus (+) sign, depending on whether you would like to remove or add an attribute, along with the letter of the file attribute and the location or name of your file:

Attrib +R File.txt

In this case i used the +R parameter to set the read only attribute in my file.

Similarly to set a different attribute replace R with the letter of the attribute of your choice.

To unset/clear an attribute replace the plus sign (+) with a minus (-).

Attrib -R File.txt

The attribute of your choice will immediately be cleared.

To verify whether an attribute was indeed set or unset use the Attrib command without any parameters, just like we learned previously.

We can also use the Attrib command to set many attributes at once simply by typing them one after the other, for example, with the following command we can set both the read only (R) and the system (S) file attribute.

Attrib +R +S File.txt

Both the read only and the system attribute will be set at once.

In the same way you can replace the plus signs with minuses to remove/clear multiple attributes at once.

Hide a File/Folder - Using Attrib:​

By setting or clearing the hidden attribute from a file or folder we can either hide or unhide it accordingly.

For example we can use he following command to make our file hidden:

Attrib +H File.txt

The file will be hidden at once.

To hide an entire folder/directory instead simply replace File.txt with the name of your folder.

Hidden files or directories are by default not viewable from the windows explorer/file manager.

To view a file or folder that’s has the hidden attribute set use the dir command along with the /A parameter. 

Dir /a
ATTRIB Command & File Attributes - Examples, Options, Switches (CMD, DOS)​

Every file in your directory will be displayed at once including any files or directories that are hidden.

Change File Attributes On Folders/Directories:​

So far we have been using the attrib command to modify the file attributes of our files. In a similar way we can change the attributes of our folders/directories. For example:

Attrib +R +S My_Folder

However this command will only change the file attributes of the folder itself and not any of its contents.

To modify the attributes of any files (and only files) within our directory we need to use the /s parameter.

Here is what the command would look like:

Attrib /s -R -S My_Folder

Like i said before, with the /s parameter only any files within our folder will be included in the operation, to include any folders/directories as well you need to use /d along with the /s parameter. For example:

Attrib /s /d +R +S My_Folder

The read only and system attributes will be set to for the My_Folder folder as well as any sub directories or files within it.

 

Change File Attributes On Multiple Files/Folders:​

We can use the Attrib command to set attributes to many files or folders at once.

To do so we will need to use a wildcard.

Wildcards are special symbols that take the place of characters or words.

With the use of the asterisk wildcard we can select and remove the system attribute for every file and folder in the current directory.

Attrib /s /d -S "*"

The read only and system attributes will be removed from every file and folder in the current directory as well as any sub directories or files.

Notice how i surrounded the wildcard in quotes, that’s because the names of some of the files in the current directory might contain spaces within their names.

Like i said before this will complete the specified operation on every file and folder that located only in the current directory, to add to remove a file attribute for a file/folder in a different directory, type the location/path of your file/directory followed by a backwards slash and the asterisk wildcard.

For example to remove the system attribute in every file and folder in the desktop directory, use the following syntax:

Attrib /s /d -S "C:\Users\\Desktop\*"

Replace with your computers username.

Useful Parameters:​

  • (+-)a Enables/Disables the archive attribute on a file or folder.
  • (+-)h Enables/Disables the hidden attribute on a file or folder.
  • (+-)r Enables/Disables the read-only attribute on a file or folder.
  • (+-)s Enables/Disables the system attribute on a file or folder.
  • (+-)c Enables/Disables the compressed attribute on a file or folder.
  • (+-)e Enables/Disables the encrypted attribute on a file or folder.
  • (+-)i Enables/Disables the indexed attribute on a file or folder.
  • /s Only applies when a file attribute is being set/cleared on a folder/directory. Sets/clears the specified attribute on every file within that folder (not including any sub directories) .
  • /d Can only be used with /s. Applies the specified operation on any folders or sub directories within the specified folder.

Attrib Examples:​

View the set applied file attributes of the File.txt text file.

Attrib File.txt

Set the hidden attribute on the File.txt text file.  

Attrib +h File.txt

Set the hidden, read only and system attribute on the File.txt text file.  

Attrib h r s File.txt

View the applied file attributes of the My_Folder folder and any of the files within it.

Attrib /s My_Folder

Clear the read only, hidden and system attributes and set the archive attribute on the My_folder folder including any of the files and directories/folders within it. 

Attrib -r -h -s +a /s /d My_Folder

Summary:

  • File attributes define the way a user or the system can interact with a file or folder, as well as how it behaves.
  • Use Attrib along with your attribute and a plus or a minus sign depending on whether you would like to set or clear that attribute.
  • Set the Hidden attribute on a file or folder to hide it from view. Use the Dir command along with the /a parameter to view it.
  • Use the /s and /d parameters to change the file attributes of a directory recursively.
  • Take advantage of wildcards and change the file attributes of many files or folders all at once.

That's It!

You now know what file attributes are and how to change them from the command prompt.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.

Dont Stop Learning!

Windows Command Line Course!

This course has everything you need to start learning about the windows command line along with batch scripting.

Read More:

Any Questions? Ask them Below:

Leave a Reply

Your email address will not be published.

Take Your Skills to the next level!
Become one of our beloved students by enrolling into one of our courses. Get started now!

FREE Command Line Course!

Learn the Windows command line And Become an Expert!