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 SolutionCommented Aug 8, 2014 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/…– rdubyabCommented Aug 9, 2014 at 5:42
3 Answers
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
There is a bug in this first script:
dir /ah file.ext >nul 2>nul && (
echo file.ext is hidden
) || (
echo file.ext is NOT hidden
)
I used it to check if a folder is hidden, but this script above gives false positive feedback if the folder is not hidden and contains a hidden file or folder (and the same is true if you look for any other attribute). The second script seems to work fine:
attrib file.ext | findstr "^....H" >nul && (
echo file.ext is hidden
) || (
echo file.ext is NOT hidden
)
Edit: If you want to check only folders, then add d to the attributes: /adh, and the first script gonna work for you, but I want to check folders and files simultaneously, so for me only the second script is good.
Edit 2: I've corrected the first script:
if exist "PATH"\* (
set "attrD="
) else (
set "attrD=-"
)
dir /a:%attrD%dh "PATH" >nul 2>nul && (
echo %1 is hidden
) || (
echo %1 is NOT hidden
)
This script will show folders as NOT hidden if they are not hidden and contain a hidden file or folder. You have to specify attribute d and it has to be after %attrD%, the position of the other attributes does not matter.