This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
105} How to detect whether or not a folder contains hidden files?
As a sideline, note the usage of the caret
^ for dividing a command
structure over several lines to avoid wrapping problems in
presenting a solution.
@echo off & setlocal enableextensions
dir C:\MyFolder\*.* /b /a:h
^
|find /v "SomeUnlikelyString">nul
^
&& set hidden_=true
^
||set hidden_=false
echo hidden_=%hidden_%
endlocal & goto :EOF
The output will be either
C:\_D\TEST>cmdfaq
hidden_=true
or
C:\_D\TEST>cmdfaq
File Not Found
hidden_=false
If
you want to get rid of the "File Not Found" use the alternatives
(posted by William Allen)
@echo off & setlocal enableextensions
dir C:\MyFolder\*.* /a:h>nul 2>&1
if errorlevel 1 (set hidden_=false) else (set hidden_=true)
echo hidden_=%hidden_%
endlocal & goto :EOF
or
@echo off & setlocal enableextensions
dir C:\_M\TEMP\*.* /a:h>nul 2>&1
set hidden_=true& dir C:\MyFolder\*.* /a:h>nul 2>&1||set hidden_=false
echo hidden_=%hidden_%
endlocal & goto :EOF
Another option is
@echo off & setlocal enableextensions
attrib c:\_m\temp\*.*|findstr /b "....H">nul
if errorlevel 1 (set hidden_=false) else (set hidden_=true)
echo hidden_=%hidden_%
endlocal & goto :EOF
Note how the method above can also be used e.g. for testing if a
file is read-only:
@echo off & setlocal enableextensions
if [%1]==[] (
echo Usage: %~f0 [FileName]
goto :EOF
)
if not exist "%~1" (
echo File "%~1" not found
goto :EOF
)
attrib "%~1"|findstr /b ".....R">nul
if errorlevel 1 (set readonly_=false) else (set readonly_=true)
echo File "%~1" read-only status: %readonly_%
endlocal & goto :EOF