Skip to main content

How to Hide or Unhide a Folder in Batch Script

The "Hidden" attribute is a basic file system flag used to keep folders from being displayed in default File Explorer views. This is useful for decluttering a directory by hiding application data or for preventing users from accidentally tampering with important configuration folders. From a batch script, you can easily control this attribute using the built-in ATTRIB command.

This guide will teach you how to use the ATTRIB command to make a folder hidden (+H) and how to make it visible again (-H). You will learn the correct syntax for targeting folders and how to apply these changes robustly in your scripts.

The Core Command: ATTRIB

The ATTRIB command is the standard Windows utility for viewing and changing file and folder attributes. To manage the hidden status, you use a simple plus (+) to add the attribute and a minus (-) to remove it.

  • ATTRIB +H "FolderName": Adds the hidden attribute.
  • ATTRIB -H "FolderName": Removes the hidden attribute.

The letter H specifically represents the Hidden attribute.

Hiding a Folder (+H)

To hide a folder, you apply the +H attribute. Once set, the folder will not appear in a standard DIR command or in File Explorer unless the "Show hidden files, folders, and drives" option is enabled.

Let's hide a folder named C:\ProgramData\MyAppData.

@ECHO OFF
SET "DATA_FOLDER=C:\ProgramData\MyAppData"

ECHO Hiding the application data folder...
ATTRIB +H "%DATA_FOLDER%"

ECHO Verifying the change...
REM We need /A to see the attributes of a hidden folder.
DIR /A "C:\ProgramData" | FIND "MyAppData"

The DIR /A command (which shows all files) will now show that the directory has the h attribute.

...
10/27/2023 08:00 PM <DIR> h MyAppData
...

Unhiding a Folder (-H)

To make a folder visible again, you simply remove the hidden attribute. This is often necessary for troubleshooting or to allow users to access a folder that was previously hidden.

@ECHO OFF
SET "DATA_FOLDER=C:\ProgramData\MyAppData"

ECHO Making the application data folder visible...
ATTRIB -H "%DATA_FOLDER%"

ECHO Verification complete. The folder should now be visible.
note

After running this, the folder MyAppData will reappear in standard directory listings.

Key ATTRIB Parameters Explained (/S and /D)

When you want to hide a folder and everything inside it, you need two additional switches.

  • /S: Subdirectories. This switch makes the ATTRIB command apply the changes recursively to all files and folders inside the target folder.
  • /D: Directories. This switch is crucial. It tells ATTRIB to apply the command to the folder names themselves, not just the files. Without /D, a command like ATTRIB +H "MyFolder\*.*" /S would hide all the files and subfolders, but the top-level MyFolder would remain visible.
note

The Correct Combination: To hide a folder and everything it contains, you should use /S and /D together. ATTRIB +H "FolderName" /S /D

Common Pitfalls and How to Solve Them

Problem: The Command Only Affects Files Inside the Folder

A common mistake is to target the contents of a folder instead of the folder itself, or to forget the /D switch.

Let's see the error in action:

REM This command hides all the files and folders inside C:\Logs,
REM but the C:\Logs folder itself remains visible.
ATTRIB +H "C:\Logs\*.*" /S

Solution: Target the Folder and Use /S and /D

This is the only way to robustly hide an entire directory tree.

REM This correctly hides C:\Logs and everything within it.
ATTRIB +H "C:\Logs" /S /D

Problem: The Path Contains Spaces

If the path to your folder contains spaces, the command will fail unless it is enclosed in double quotes.

Let's see the error in action:

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

Solution: Always Quote Your Paths

This is a universal best practice for reliable batch scripting.

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

Practical Example: A Script to Hide a ".git" Repository Folder

When you initialize a Git repository, it creates a .git folder. This folder should generally not be modified by the user. This script can be run in a project's root to ensure this important folder is hidden.

@ECHO OFF
SETLOCAL
SET "GIT_FOLDER=.git"

ECHO --- Git Folder Hider ---
ECHO.

ECHO Checking for a .git folder in the current directory...
ECHO Current Directory: %CD%

IF NOT EXIST "%GIT_FOLDER%\" (
ECHO [INFO] No .git folder found here. Nothing to do.
GOTO :End
)

ECHO Found .git folder. Applying the Hidden attribute...

REM Hide the .git folder and everything inside it.
ATTRIB +H "%GIT_FOLDER%" /S /D

ECHO.
ECHO [SUCCESS] The .git folder is now hidden.

:End
ENDLOCAL

Conclusion

The ATTRIB command is the simple and direct tool for hiding and unhiding folders in Windows. Its syntax is easy to remember and powerful when combined with its command-line switches.

For effective and reliable scripts:

  • Use ATTRIB +H "FolderName" to hide a folder.
  • Use ATTRIB -H "FolderName" to unhide a folder.
  • To apply the change to a folder and all of its contents recursively, use the /S and /D switches together.
  • Always enclose your folder paths in double quotes ("...") to correctly handle spaces.

By mastering this command, you can easily control the visibility of folders to protect sensitive data and provide a cleaner user experience.