Skip to main content

How to Set or Remove the Hidden Attribute in Batch Script

The "Hidden" attribute in Windows is used to declutter file listings, hiding system files or application data from casual view in File Explorer. As a scripter, you might need to hide files or folders to prevent user tampering, or you might need to unhide them to ensure that your scripts can access and modify them. The standard, built-in command for this is ATTRIB.

This guide will show you how to use the ATTRIB command to add (+H) and remove (-H) the hidden attribute, apply these changes to multiple files at once, and provide a practical script for hiding an application's data folder.

The Core Command: ATTRIB

The ATTRIB command is the primary tool for managing file attributes from the command line. The syntax for changing the hidden attribute is simple and intuitive, using a plus (+) to add the attribute and a minus (-) to remove it.

  • ATTRIB +H "filename": Adds the hidden attribute, making the file invisible in default File Explorer views.
  • ATTRIB -H "filename": Removes the hidden attribute, making the file visible again.

The letter H specifically represents the Hidden attribute.

Setting the Hidden Attribute (+H)

To hide a file or folder, you add the +H attribute. This is useful for tucking away data files or logs that the end-user doesn't need to see.

For example, let's hide a temporary data file.

@ECHO OFF
SET "DATA_FILE=C:\ProgramData\MyApp\cache.dat"

ECHO Hiding the data file...
ATTRIB +H "%DATA_FILE%"

ECHO Verifying the change...
ATTRIB "%DATA_FILE%"

The output from the second ATTRIB command now includes an H, confirming the file is hidden.

Hiding the data file...
Verifying the change...
A H C:\ProgramData\MyApp\cache.dat

Removing the Hidden Attribute (-H)

To make a file visible and ensure it can be found by standard commands, you must remove the hidden attribute. This is often a necessary step before performing operations on system files.

Now, let's "unhide" the same data file.

@ECHO OFF
SET "DATA_FILE=C:\ProgramData\MyApp\cache.dat"

ECHO Making the data file visible...
ATTRIB -H "%DATA_FILE%"

ECHO Verifying the change...
ATTRIB "%DATA_FILE%"

Now, in the output the H is absent from the attribute list:

Making the data file visible...
Verifying the change...
A C:\ProgramData\MyApp\cache.dat

Working with Multiple Files and Folders

ATTRIB fully supports wildcards (*), which allows you to change attributes on many files at once. You can also target a folder to hide the folder itself.

Script: Hiding All .tmp Files Recursively

This script hides all temporary files within a specific folder and all of its subdirectories.

@ECHO OFF
SET "TEMP_FOLDER=C:\Users\Admin\AppData\Local\Temp"

ECHO Hiding all .tmp files recursively...
REM The /S switch applies the command to files in subdirectories.
ATTRIB +H "%TEMP_FOLDER%\*.tmp" /S

ECHO Process complete.

This single command efficiently hides all matching files in the entire directory tree.

Common Pitfalls and How to Solve Them

Problem: Command Fails on Paths with Spaces

If the path to a file or folder contains spaces, the command will fail unless the path is correctly quoted.

Let's see the error:

REM This will FAIL.
ATTRIB +H C:\My App Data\settings.ini

Solution: Always Quote Your Paths

Enclosing the entire path in double quotes is essential for robust scripting. It ensures Windows treats the path as a single unit.

REM This is the correct, safe syntax.
ATTRIB +H "C:\My App Data\settings.ini"

Problem: Handling "File Not Found" Errors

Attempting to run ATTRIB on a file that doesn't exist will result in an error. For critical scripts, you should handle this case gracefully.

Let's see the error:

C:\> ATTRIB +H non_existent_file.txt
File not found - non_existent_file.txt

Solution: Use IF EXIST First

A simple IF EXIST check prevents errors by ensuring your command only runs if the target file or folder is actually present.

@ECHO OFF
SET "FILENAME=important.dat"

IF NOT EXIST "%FILENAME%" (
ECHO [ERROR] Target file not found. Cannot change attribute.
GOTO :EOF
)

ATTRIB +H "%FILENAME%"
ECHO Successfully hid the file "%FILENAME%".

Practical Example: Hiding an Application Data Folder

This script is designed to be run after an application is installed. It hides the data folder to protect it from accidental user modification or deletion.

@ECHO OFF
SETLOCAL
SET "APP_DATA_FOLDER=C:\Program Files\MyCoolApp\_data"

ECHO --- Post-Installation Cleanup ---
ECHO.

IF NOT EXIST "%APP_DATA_FOLDER%" (
ECHO [WARNING] Application data folder not found. Nothing to do.
GOTO :End
)

ECHO Hiding the application data folder to protect it...

REM The /D switch ensures the command can apply to directory names as well.
ATTRIB +H "%APP_DATA_FOLDER%" /S /D

ECHO Verification:
ATTRIB "%APP_DATA_FOLDER%"

:End
ECHO Script finished.
ENDLOCAL
note

Note the use of /S and /D. /S processes files within the directories, while /D ensures the directories themselves are processed. This combination comprehensively hides the folder and everything inside it.

Conclusion

The ATTRIB command is the direct and effective tool for managing the hidden attribute in Windows Batch. Its simple +H and -H syntax makes it easy to remember and use.

For writing reliable scripts, always follow these best practices:

  • Use ATTRIB +H to hide a file or folder.
  • Use ATTRIB -H to unhide a file or folder.
  • Always enclose file and folder paths in double quotes ("...") to avoid issues with spaces.
  • Use IF EXIST to check for a file's presence before attempting to change its attributes.

By using ATTRIB correctly, you can create scripts that cleanly manage the visibility of files and folders, contributing to a more professional and robust application environment.