The usual test for a folder existence is
if exist "x:\somewhere\" echo FOLDER FOUND
but, in some cases (i know the case of novell netware redirector) the previous condition will always evaluate to true, both when "x:\somewhere" is a file or when it is a folder
One alternative can be
@echo off
setlocal enableextensions
set "target=c:\windows"
set "what=NOT EXIST"
for %%a in ("%target%") do for /f "delims=r-" %%b in ("%%~aa%%~za"
) do if "%%b"=="d" ( set "what=FOLDER" ) else ( set "what=FILE" )
echo %what%
endlocal
Check for the presence of an initial d
in the list of attributes of the file/folder.
Each of the attributes in the list can be a letter (depending on the attribute) or a dash.
The two first are d
for directory and r
for readonly. So, second for
uses r-
as delimiters to separate the possible d
from the rest of attributes.
If the file does not have any attributes set, the tokenizer in for
command will eliminate all the dashes, non assigning data to the replaceable parameter and in consecuence not executing the code inside the do
clause. To avoid it, %%~za
(the size of the element) is appended to the string, so, there will always be data to be processed if the file/folder exists. If it does not exist, the expresion %%~aa%%~za
is evaluated to a empty string and the code in the do clause will not execute.