Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I find if a folder exists?

1,349 views
Skip to first unread message

Timo Salmi

unread,
Mar 12, 2004, 11:35:30 PM3/12/04
to
DRAFT:

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 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

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Mark V

unread,
Mar 13, 2004, 4:38:56 AM3/13/04
to
In alt.msdos.batch.nt Timo Salmi wrote:

> DRAFT:
>
> 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 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


CD /D <"FQP">
IF %errorlevel% ...

W2K. Others unknown.

Matthias Tacke

unread,
Mar 13, 2004, 6:36:50 AM3/13/04
to
Timo Salmi wrote:

>DRAFT:
>
>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 solution to the problem would appear to be:

<snip>

From w2k on, you could examine the attributes, no dir etc, all with
set and if:

::ChkFolder.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
if [%1]==[] goto :Usage
if defined MyChkFolderAttr Set MyChkFolderAttr=
set MyChkFolderAttr=%~a1
if NOT defined MyChkFolderAttr echo %1 doesn't exist & goto :eof
if [%MyChkFolderAttr:~0,1%]==[d] (
echo [%~f1] is a folder Attr=%MyChkFolderAttr%
) else (
echo [%~f1] isn't a folder Attr=%MyChkFolderAttr%
)
goto :EOF
:Usage
echo/Usage: %~n0 Folder
echo/Returns Attributes if Folder exists. Requires w2k+
::ChkFolder.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::


--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm

OOTANI TAKASHI

unread,
Mar 13, 2004, 10:40:39 AM3/13/04
to
t...@UWasa.Fi (Timo Salmi) writes:
> 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.

Spaces are not essential.
if not exist "c:mydir\nul" echo Folder not found
will also always be true whether the folder exists or not.

> if not exist "%dr_%\*.*" (
> echo Folder "%dr_%" not found)
> ::
> if exist "%dr_%\*.*" (
> echo Folder "%dr_%" exists)

Or,
if not exist "%dr_%\" (


echo Folder "%dr_%" not found)
::

if exist "%dr_%\" (
echo Folder "%dr_%" exists)
Or,
dir /b/ad "%dr_%" >NUL 2>NUL || (


echo Folder "%dr_%" not found)
::

dir /b/ad "%dr_%" >NUL 2>NUL && (
echo Folder "%dr_%" exists)
--
tksotn

Timo Salmi

unread,
Mar 13, 2004, 1:48:03 PM3/13/04
to
Matthias Tacke <Matt...@Tacke.de> wrote:

> Timo Salmi wrote:
> >One solution to the problem would appear to be:
> <snip>

> From w2k on, you could examine the attributes, no dir etc, all with
> set and if:

Thanks Matthias. Linked to the future version in the customary
manner.

Timo Salmi

unread,
Mar 13, 2004, 1:52:50 PM3/13/04
to
OOTANI TAKASHI <tks...@anet.ne.jp> wrote:
> t...@UWasa.Fi (Timo Salmi) writes:
> > 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.

> Spaces are not essential.
> if not exist "c:mydir\nul" echo Folder not found
> will also always be true whether the folder exists or not.

Indirectly the spaces are of essence, since it is the spaces require
the quotes, which, in turn, are the real source of the behavior.
Without the need of the spaces the exist /nul trick works. (But that
is not a generic enough a solution.)

Gary Smith

unread,
Mar 13, 2004, 9:25:56 PM3/13/04
to
Timo Salmi <t...@uwasa.fi> wrote:
> Indirectly the spaces are of essence, since it is the spaces require
> the quotes, which, in turn, are the real source of the behavior.
> Without the need of the spaces the exist /nul trick works. (But that
> is not a generic enough a solution.)

In Windows 2000, it appears that "IF EXIST" works on folder names without
the need to append "\nul". There may be situations hwere it doesn't, but
I haven't found one yet.

--
Gary L. Smith gls...@yahoo.com
Columbus, Ohio

Timo Salmi

unread,
Mar 14, 2004, 12:55:47 AM3/14/04
to
Gary Smith <bitb...@example.com> wrote:

> Timo Salmi <t...@uwasa.fi> wrote:
> > Without the need of the spaces the exist /nul trick works. (But that
> > is not a generic enough a solution.)

> In Windows 2000, it appears that "IF EXIST" works on folder names without
> the need to append "\nul". There may be situations hwere it doesn't, but

Good. Phil Robyn kindly told me the same. It seems that the same
syntax goes for XP. The Microsoft command-line documentation would
thus seem to be inadequate and inaccurate in this respect. Well, it
is not the only case. Weve already discussed the "rem" at one stage,
and I seem to recall the existince other indiscrepancies between the
documents and the reality. Although I can't at the moment exactly
recall which.

Al Dunbar

unread,
Mar 14, 2004, 12:24:40 PM3/14/04
to

"Gary Smith" <bitb...@example.com> wrote in message
news:1057glk...@corp.supernews.com...

Quite correct, however, with pre-2000 versions it is possible to use the
\nul trick to detect the existence of a DIRECTORY as opposed to a FILE.

/Al


Timo Salmi

unread,
Mar 14, 2004, 12:47:31 PM3/14/04
to
Al Dunbar <alan-no-...@hotmail.com> wrote:
> "Gary Smith" <bitb...@example.com> wrote in message
> > In Windows 2000, it appears that "IF EXIST" works on folder names without
> > the need to append "\nul". There may be situations hwere it doesn't, but
> > I haven't found one yet.

> Quite correct, however, with pre-2000 versions it is possible to use the
> \nul trick to detect the existence of a DIRECTORY as opposed to a FILE.

I can confirm that for Windows 95. However, then we are not yet
using the CMD.EXE command rpocessor. The \nul trick is best not used
under NT/2000/XP.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Al Dunbar

unread,
Mar 14, 2004, 2:39:30 PM3/14/04
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:c325rj$o...@poiju.uwasa.fi...

> Al Dunbar <alan-no-...@hotmail.com> wrote:
> > "Gary Smith" <bitb...@example.com> wrote in message
> > > In Windows 2000, it appears that "IF EXIST" works on folder names
without
> > > the need to append "\nul". There may be situations hwere it doesn't,
but
> > > I haven't found one yet.
>
> > Quite correct, however, with pre-2000 versions it is possible to use the
> > \nul trick to detect the existence of a DIRECTORY as opposed to a FILE.
>
> I can confirm that for Windows 95. However, then we are not yet
> using the CMD.EXE command rpocessor. The \nul trick is best not used
> under NT/2000/XP.

Agreed - but my point was not that \nul *should* be used under nt/2k/xp. My
point was that the \nul trick under 9x does somewhat *more* than 'if folder
exists' under nt/2k/xp, and, if one wants *that* particular functionality
(i.e. does a FOLDER exist by this name), then somewhat different steps need
to be taken.

/Al


Gary Smith

unread,
Mar 15, 2004, 3:25:31 AM3/15/04
to
Al Dunbar <alan-no-...@hotmail.com> wrote:

> Agreed - but my point was not that \nul *should* be used under nt/2k/xp. My
> point was that the \nul trick under 9x does somewhat *more* than 'if folder
> exists' under nt/2k/xp, and, if one wants *that* particular functionality
> (i.e. does a FOLDER exist by this name), then somewhat different steps need
> to be taken.

I din't understand what you were getting at with your previous comment
until I saw this. It's true that under Win2K "if exist X" will return
true so long as X exists, whether it be a file or a folder. As we've
seen, when the path name is quoted, appending \nul doesn't work -- it
always returns false. You can, however, append "\*.*". That seems to
work as desired even when the directory is empty.

[D:\] md "Test 1.dir"

[D:\] nullfile "Test 2.tmp"

[D:\] dir test*
Volume in drive D is W2K System
Volume Serial Number is 40F7-83CF

Directory of D:\

03/15/2004 03:16 <DIR> Test 1.dir
03/15/2004 03:17 0 Test 2.tmp
1 File(s) 0 bytes
1 Dir(s) 21,366,358,016 bytes free

[D:\] if exist "Test 1.dir" echo Yes!
Yes!

[D:\] if exist "Test 1.dir\*.*" echo Yes!
Yes!

[D:\] if exist "Test 2.tmp" echo Yes!
Yes!

[D:\] if exist "Test 2.tmp\*.*" echo Yes!

[D:\]

Timo Salmi

unread,
Mar 15, 2004, 1:12:42 PM3/15/04
to
Gary Smith <bitb...@example.com> wrote:
> always returns false. You can, however, append "\*.*". That seems to
> work as desired even when the directory is empty.

Yes, Gary. Actually used that trick a few days ago in
http://www.google.com/groups?selm=c2rlr8$n...@poiju.uwasa.fi
when I noticed to my surprise the that the *.* behavior is different
from the pre-NT versions. That's, in fact, the "incidence" that set
me in the first place thinking and posting about the folder
existence problem under NT/2000/XP.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

OOTANI TAKASHI

unread,
Mar 16, 2004, 2:53:40 PM3/16/04
to
Gary Smith <bitb...@example.com> writes:
> I din't understand what you were getting at with your previous comment
> until I saw this. It's true that under Win2K "if exist X" will return
> true so long as X exists, whether it be a file or a folder. As we've
> seen, when the path name is quoted, appending \nul doesn't work -- it
> always returns false. You can, however, append "\*.*". That seems to
> work as desired even when the directory is empty.

Appending "\*.*" is not necessary.
Appending "\" also work well in W2K, WXP. (I don't have NT4.)

Case of testing for-variable or call-argument, the traditional method:
if exist %%~sI\nul or if exist %~s1\nul are also OK,
unless you have set NtfsDisable8dot3NameCreation in the registry to 1.
--
tksotn

Timo Salmi

unread,
Mar 17, 2004, 10:02:27 AM3/17/04
to
OOTANI TAKASHI <tks...@anet.ne.jp> wrote:

> Gary Smith <bitb...@example.com> writes:
> > always returns false. You can, however, append "\*.*". That seems to
> > work as desired even when the directory is empty.

> Appending "\*.*" is not necessary.

Rigth. Not claimed though. "Can, not must."

> Case of testing for-variable or call-argument, the traditional method:
> if exist %%~sI\nul or if exist %~s1\nul are also OK,

That's neat. "Translates" e.g. to

@echo off & setlocal enableextensions

:: Which folder to examine (customize the path!)
set dr_=c:\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))

0 new messages