Verifying existence of folders containing spaces: NULL device not seen inside quotes

S

Stewart Halyard

I am writing a script in which I must validate the folder
of a file specification entered on the command line. For
example, if my batch file is called myBatch.bat and the user
runs:

myBatch c:\myProgs\MyLogs\*.log

I must validate the folder c:\myProgs\MyLogs\.
I can do this only when the folder name contains no spaces,
because when I query for the NULL device when the folder name
is in quotes, IF NOT EXIST returns true.

Here's some code that works when there are no spaces in the path:

REM *** Works only with no spaces in path ***
SET theFolder=%~dp1
IF NOT EXIST %theFolder%nul (
ECHO Invalid folder: "%theFolder%"
)


If I specifiy a folder with spaces, the above code doesn't work
because it thinks there are more than one command in the IF
statement. So I add quotes around the folder name:

REM *** Never works because IF NOT EXIST always returns true ***
SET theFolder=%~dp1
IF NOT EXIST "%theFolder%nul" (
ECHO Invalid folder: "%theFolder%"
)

The echo statement is always executed. Apparently, Windows 2000
can't tell that I'm querying for the NULL device when there are quotes
around the path.

For reference on batch parameters like %~dp1, see:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx

Any help would be most appreciated!
Jay
 
P

Paul R. Sadowski [MVP]

Hello, Stewart:
On Fri, 25 Mar 2005 12:45:17 -0500: you wrote...

SH> I must validate the folder c:\myProgs\MyLogs\.
SH> I can do this only when the folder name contains no spaces,
SH> because when I query for the NULL device when the folder name

You don't have to in NT+ You can test the folder directly.

@echo off
REM *** Works only with no spaces in path ***
SET theFolder=%~dp1
IF NOT EXIST "%theFolder%" (
ECHO Invalid folder: "%theFolder%"
) else (
ECHO folder "%theFolder%" exists!
)

C:\Users\sadowski [(firecat) 13:30, Fri 03/25/2005] ifexist.cmd "c:\Program
Files\1-2-3 PayPal Website Payments\fpPayPal.dll"
folder "c:\Program Files\1-2-3 PayPal Website Payments\" exists!

Regards, Paul R. Sadowski [MVP].
 
S

Stewart Halyard

Paul R. Sadowski said:
Hello, Stewart:
On Fri, 25 Mar 2005 12:45:17 -0500: you wrote...

SH> I must validate the folder c:\myProgs\MyLogs\.
SH> I can do this only when the folder name contains no spaces,
SH> because when I query for the NULL device when the folder name

You don't have to in NT+ You can test the folder directly.

@echo off
REM *** Works only with no spaces in path ***
SET theFolder=%~dp1
IF NOT EXIST "%theFolder%" (
ECHO Invalid folder: "%theFolder%"
) else (
ECHO folder "%theFolder%" exists!
)

C:\Users\sadowski [(firecat) 13:30, Fri 03/25/2005] ifexist.cmd "c:\Program
Files\1-2-3 PayPal Website Payments\fpPayPal.dll"
folder "c:\Program Files\1-2-3 PayPal Website Payments\" exists!

Regards, Paul R. Sadowski [MVP].

Thank you Paul! Ugh, I was thrown off by the incorrect Windows (XP!)
documentation, in
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/if.mspx

You cannot use the if command to test directly for a directory,
but the null (NUL) device does exist in every directory. As a
result, you can test for the null device to determine whether
a directory exists. The following example tests for the existence
of a directory:

if exist c:\mydir\nul goto process
 
J

Jerold Schulman

One line:

if exist "%~DP1" (@echo "%~DP1" exists.) ELSE (@echo "%~DP1" does not exist.)

I am writing a script in which I must validate the folder
of a file specification entered on the command line. For
example, if my batch file is called myBatch.bat and the user
runs:

myBatch c:\myProgs\MyLogs\*.log

I must validate the folder c:\myProgs\MyLogs\.
I can do this only when the folder name contains no spaces,
because when I query for the NULL device when the folder name
is in quotes, IF NOT EXIST returns true.

Here's some code that works when there are no spaces in the path:

REM *** Works only with no spaces in path ***
SET theFolder=%~dp1
IF NOT EXIST %theFolder%nul (
ECHO Invalid folder: "%theFolder%"
)


If I specifiy a folder with spaces, the above code doesn't work
because it thinks there are more than one command in the IF
statement. So I add quotes around the folder name:

REM *** Never works because IF NOT EXIST always returns true ***
SET theFolder=%~dp1
IF NOT EXIST "%theFolder%nul" (
ECHO Invalid folder: "%theFolder%"
)

The echo statement is always executed. Apparently, Windows 2000
can't tell that I'm querying for the NULL device when there are quotes
around the path.

For reference on batch parameters like %~dp1, see:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx

Any help would be most appreciated!
Jay


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top