if exist not working for a folder with spaces

January 6, 2010 at 10:36:36
Specs: Windows XP
I'm trying to determine in a directory exist using a batch file in XP.

Microsoft basically says use NUL after the pathname

The syntax for checking for a directory existance is...
if exist c:\windows\nul then goto exists

I want to test for the existence of a folder with spaces in the name. The obvious solution is to enclose the name inside double quotes. I can't seem to get that to work either.

Here's an example of what I'm trying:

if exist "C:\Documents and Settings\NUL" goto YES
echo The file does not exist
goto END
:YES
echo The file exists
:END


I have tried using spaces and quote marks in various combinations. I cannot find a way to test for the existence of a directory with spaces in it. Can you help?


See More: if exist not working for a folder with spaces

Report •


#1
January 6, 2010 at 11:38:25
@echo off & setLocal EnableDELAYedeXpansion

if exist "C:\Documents and Settings" (
echo YES
) else (
echo no
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 6, 2010 at 13:53:42
I apologize if I was not clear. I am looking for a way to validate that:

1) a file exists
2) that file is a directory.

Your solution only appears to validate that a file exists.


Report •

#3
January 6, 2010 at 14:16:32
@echo off & setLocal EnableDELAYedeXpansion

if exist "C:\Documents and Settings\" (
echo YES
) else (
echo no
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
January 6, 2010 at 17:27:55
IF EXIST c:\progra~1\nul ECHO Directory exists!

You dos shortname for path.


Report •

#5
January 6, 2010 at 17:45:49
Easiest way:
pushd "C:\Documents and Settings\z" 2>NUL && popd
@if not errorlevel 1 echo Dir exists!


Report •

#6
January 7, 2010 at 06:54:12
Thanks Razor2.3,

That's perfect.


Report •


Ask Question