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.
59} How do I find if a folder exists? How about visible files in it?
Also, and perhaps even first, see the item "
75) How do I detect if an object is a
file or a folder?":
@echo off & setlocal enableextensions
if "%~1"=="" (
echo Usage: %~0 [FileOrFolderName]
goto :EOF
)
::
if not exist "%~1" (
echo File or folder "%~1" not found
goto :EOF
)
if exist "%~1\" (
echo "%~1" is a folder
) else (
echo "%~1" is a file
)
endlocal & goto :EOF
While the most frequent CLI script question is how to include the date
into a file name, the current first question seems to be the one that
elicits most often inaccurate or outright incorrect answers.
The most robust test in XP for the folder existence is
if exist "FolderName\" ...
The trick advocated in the Microsoft Command-line reference is
if exist c:mydir\nul goto process
However, the advocated method crumbles with the LFN possibility of
spaces in the folder names. Hence
if not exist "c:my dir\nul" echo Folder not found
will always be true whether the folder exists or not.
One (further) solution to the problem would appear to be:
@echo off & setlocal enableextensions
rem echo on %This is for debugging purposes%
::
:: Which folder to examine (customize the path!)
set dr_=c:\my dir
::
if not exist "%dr_%\*.*" (
echo Folder "%dr_%" not found)
::
if exist "%dr_%\*.*" (
echo Folder "%dr_%" exists)
::
dir "%dr_%" 2>&1 | find "File(s)" | find /v " 0 File(s)" > nul
if %errorlevel% NEQ 0 (
echo No visible files in folder "%dr_%")
::
dir "%dr_%" 2>&1 | find "File(s)" | find /v " 0 File(s)" > nul
if %errorlevel% EQU 0 (
echo Visible files in found in folder "%dr_%")
::
endlocal & goto :EOF
Phil Robyn pointed out in email that leaving out the \nul the
following kind of a formulation works.
if not exist "%dr_%" (
echo Folder "%dr_%" not found
goto :EOF)
Yet another option is to utilize the fact that short file names do
not have a problem with \nul
@echo off & setlocal enableextensions
:: Which folder to examine (customize the path!)
set dr_=m:\my dir
:: Test it
for /f "tokens=1 delims=" %%d in ("%dr_%") do (
if exist %%~sd\nul (
echo Found %%d
) else (
echo Did not find %%d))
endlocal & goto :EOF
To recount an important difference from MS-DOS+Win../95/98/Me batches
if exist "FolderName\*.*" ...
is
not the proper test to see whether or not there are visible
files in a folder. With
*.* it
always is true. Even if
if exist "FolderName\*.txt" ...
works as expected.
To test for the empty or not status of a folder use the errorlevel
solution further up. Also see the information on
explicit-path versus wild-carded references.
Finding a file anywhere on a drive is covered in
WHEREIS.CMD. Likewise, for folders one
can write e.g.
@echo off & setlocal enableextensions
set findDir=TEST
dir /a:d/s/b "C:\%findDir%"
endlocal & goto :EOF
The output could be something like
C:\_M>CMDFAQ.CMD
C:\_D\TUTKIM2\EVA\DATA\TEST
C:\_G\WWW\ELISANET\TEST
C:\_G\WWW\TS\TEST
For more complicated searching one could have e.g.
@echo off & setlocal enableextensions
set startDir=C:\_F
::
:: Folders which have at least one digit in the path
dir /b/s/a:d "%startDir%\"|findstr "[1234567890]"
pause
::
:: Folders which do not have any digits in the path
dir /b/s/a:d "%startDir%\"|findstr /v "[1234567890]"
pause
::
:: Folders which have either 19 or 20 in the path
dir /b/s/a:d "%startDir%\"|findstr "19" > "%temp%\cmdfaq.tmp"
dir /b/s/a:d "%startDir%\"|findstr "20">> "%temp%\cmdfaq.tmp"
for %%c in (type del) do %%c "%temp%\cmdfaq.tmp"
pause
::
:: Folders which have neither 19 nor 20 in the path
dir /b/s/a:d "%startDir%\"|findstr /v "19"|findstr /v "20"
pause
::
:: Folders which have at least four consequtive digits in the path
dir /b/s/a:d "%startDir%\"|findstr "[0123456789][0123456789][0123456789][0123456789]"
pause
::
:: Folders which have from 2000 to 2010 in the path
dir /b/s/a:d "%startDir%\"|findstr "20[01][0123456789]"
endlocal & goto :EOF