I'm writing a batch file that will unhide and run another batch file or, if the file is already unhidden, will run the batch file and hide it again. How do I check if a file is hidden?

  • I have a code which can unhide files when the hidden files can't be unchecked. Here it is - attrib -s -h K:*.* /s /d But I don't know any method to check how to get a list of hidden files. Learn More – Marks PC Solution Aug 8 '14 at 23:33
  • Found this on another site. Same subject. [windows-batch-script-to-unhide-files-hidden-by-virus][1] [1]: stackoverflow.com/questions/8095002/… – rdubyab Aug 9 '14 at 5:42

You can use this in PowerShell to get a list of hidden files

Get-ChildItem X:\My\Path -Recurse -Force | Where { ($_.Attributes.ToString() -Split ", ") -Contains "Hidden" } | Select FullName

You should be able to use dir/ah for the same result in cmd in the current path.

Lots of ways.

1) DIR /AH (probably the simplest method)

Use the DIR command to look for a file with a particular attribute. Returns success if found, error if not.

dir /ah file.ext >nul 2>nul && (
  echo file.ext is hidden
) || (
  echo file.ext is NOT hidden
)

2) ATTRIB command

The attribute codes are in upper case. The code for hidden is H, and I believe it is always in the fifth position. Use FINDSTR to look for the code.

attrib file.ext | findstr "^....H" >nul && (
  echo file.ext is hidden
) || (
  echo file.ext is NOT hidden
)

3) FOR variable ~a modifier

The attribute codes are in lower case, and they are in a different order than the ATTRIB command. The code for hidden is h in the fourth position. Use FINDSTR to look for the code.

for %%F in (file.ext) do echo %%~aF | findstr "^...h" >nul 2>nul &&
  echo file.ext is hidden
) || (
  echo file.ext is NOT hidden
)

or use a substring operation. Don't forget that the substring is 0 indexed.

for %%F in (file.ext) do set "attr=%%~aF"
if %attr:~3,1% equ h (
  echo file.ext is hidden
) else (
  echo file.ext is NOT hidden
)

4) Parameter ~a modifier

Basically the same as with FOR variable, except now using a script or sub-routine parameter.

call :IsHidden file.ext &&
  echo file.ext is hidden
) else (
  echo file.ext is NOT hidden
)
exit /b

:isHidden
echo %~a1 | findstr "^...h" >nul 2>nul
exit /b

or

call :IsHidden file.ext &&
  echo file.ext is hidden
) else (
  echo file.ext is NOT hidden
)
exit /b

:isHidden
setlocal
set "att=%~a1"
if %att:~3,1% equ h exit /b 0
exit /b 0

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.