TExcelTAD
Home › Windows CMD & Batch › attrib: File Attributes

attrib: File Attributes

Every file and folder on a Windows drive carries a set of attributes: on/off flags stored by the file system (NTFS) next to the name, size and dates. They mark a file as read-only, hidden, a system file, or "changed since the last backup". The attrib command shows and changes them.

The main attributes

Letter Attribute Meaning
R Read-only the file can be read but not changed or deleted (without force)
H Hidden not shown by dir or File Explorer (by default)
S System belongs to the operating system; usually combined with H
A Archive set whenever the file is changed; backup tools clear it after backing up
I Not content indexed Windows Search skips the file
D Directory marks a folder (shown, but not changeable with attrib)

Linux and macOS have no direct equivalent of most of these: a "hidden" file there is simply one whose name starts with a dot, and read-only is a permission bit (see chmod & chown).

Showing attributes

attrib with a file name or pattern lists the attributes, then the full path. Sample output from a Windows 11 PC:

cmd
C:\Users\ada\demo>attrib *
A                    C:\Users\ada\demo\notes.txt
A    R               C:\Users\ada\demo\setup.log
A   H                C:\Users\ada\demo\secret.txt

Each column is one attribute; a letter means it is set. Newly created and modified files have A (archive) set. Add /s to include subfolders and /d to list folders as well.

Changing attributes: + and −

+X sets an attribute and -X clears it. Several can be changed at once:

cmd
C:\Users\ada\demo>attrib +r setup.log

C:\Users\ada\demo>del setup.log
Access is denied.

C:\Users\ada\demo>attrib -r setup.log

C:\Users\ada\demo>del setup.log

C:\Users\ada\demo>attrib +h +r secret.txt

C:\Users\ada\demo>attrib secret.txt
A   HR               C:\Users\ada\demo\secret.txt

Common recipes:

Task Command
protect a file against changes attrib +r config.ini
make it writable again attrib -r config.ini
hide a file or folder attrib +h private.txt / attrib +h "My Folder" /d
unhide everything in a tree attrib -h -s /s /d D:\*
make every file in a tree writable attrib -r /s *.*
clear the archive flag after a backup attrib -a /s *.*

Note: A file that is both hidden and system must have both cleared together: attrib -s -h file. Trying to clear only one prints Not resetting system file - … or Not resetting hidden file - …. The classic use of attrib -h -s /s /d is recovering files on a USB stick that malware has hidden.

Seeing hidden files with dir /a

dir skips hidden and system files unless you ask for them with /a. The letters after /a: filter by attribute, and - means "not":

Switch Lists
dir /a everything, including hidden and system files
dir /a:h only hidden entries
dir /a:r only read-only files
dir /a:d only folders
dir /a:-d only files
dir /a:h-s hidden but not system

The directory attribute D works everywhere, including the playground, so it is a good way to try the /a filter:

cmd · try it yourself
@echo off
mkdir demo\photos demo\music & cd demo
echo todo> todo.txt
echo data> data.csv
echo --- folders (/a:d):
dir /b /o:n /a:d
echo --- files (/a:-d):
dir /b /o:n /a:-d
--- folders (/a:d):
music
photos
--- files (/a:-d):
data.csv
todo.txt

On Windows, after attrib +h todo.txt, dir /b would no longer list todo.txt, while dir /b /a:h would list only it.

Reading attributes in a script

Inside a for loop, the modifier %%~a expands to the attribute string of a file, and if exist name\ tells a folder from a file. On Windows 11 a normal file looks like this:

cmd
C:\Users\ada\demo>for %f in (todo.txt) do @echo %~af %f
--a-------- todo.txt

The string starts with the flags d r a h s (directory, read-only, archive, hidden, system), each shown as - when it is not set, so a script can test one with a substring: if "%attr:~1,1%"=="r" echo read-only. In practice, the folder test is the one you need most often:

cmd · try it yourself
@echo off
mkdir demo\docs & cd demo
echo hi> readme.txt
for %%n in (docs readme.txt missing) do (
  if exist %%n\ (
    echo %%n is a folder
  ) else if exist %%n (
    echo %%n is a file
  ) else (
    echo %%n does not exist
  )
)
docs is a folder
readme.txt is a file
missing does not exist

Read-only and other commands

Read-only files affect other commands too:

  • del refuses them unless you add /f (del).
  • copy keeps the read-only attribute on the copy, while xcopy clears it unless you add /k.
  • The read-only flag on a folder is ignored by Windows for most purposes; Explorer uses it to mark customized folders.

Real access control (who may read or change a file) is not an attribute: it is the file's security descriptor (ACL), managed with icacls, covered in Running as Administrator.

Command summary

Task CMD PowerShell
show attributes attrib file (Get-Item file).Attributes
set read-only attrib +r file Set-ItemProperty file -Name IsReadOnly -Value $true
hide attrib +h file (Get-Item file).Attributes += 'Hidden'
list hidden files dir /a:h Get-ChildItem -Hidden

Learn more: NTFS · Listing Files

Test yourself

Which command makes notes.txt read-only?

  1. attrib -r notes.txt
  2. attrib +r notes.txt
  3. attrib /r notes.txt
  4. readonly notes.txt

How do you list hidden files with dir?

  1. dir /h
  2. dir /a:h
  3. dir /s
  4. dir /hidden

What does the archive (A) attribute mean?

  1. The file is compressed
  2. The file has changed since it was last backed up
  3. The file is in a ZIP archive
  4. The file is old