Refuse to run if in a UNC path?

  • Thread starter Michael A. Covington
  • Start date
M

Michael A. Covington

Rather than just getting an error message such as:


'\\ais1\install\test-ignore'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.


is there a way to make CMD.EXE flatly refuse to run in this situation? Does
it set an errorlevel when it gets this message?
 
R

Ray at

Note that I am no command prompt expert.

In just a quick test that I did, it does not seem that errorlevel is set.
But, while this wouldn't work if the thing is supposed to start in %windir%,
it would work otherwise, I suppose. I believe that if you do "start in" of
a UNC path, it'll divert you to %windir%. So,

for /f "tokens=1" %%q in ('cd') do (if {%%q}=={%WINDIR%} ECHO Hey, WTF)
 
M

Michael A. Covington

Thanks. More simply, I found that


if "%cd%"=="%windir%" exit


will do the job.
 
A

Al Dunbar [MS-MVP]

Are we guaranteed that the default directory will always be windir in this
context? If not, you could check to see whether or not the first two
characters of the full path of the batch file happen to be "\\".

/Al
 
D

David Trimboli

Good idea!

setlocal enableextensions
set scriptpath=%~f0
if %scriptpath:~0,2% equ \\ goto :EOF

David
Stardate 4146.8
 
C

Clay Calvert

Good idea!

setlocal enableextensions
set scriptpath=%~f0
if %scriptpath:~0,2% equ \\ goto :EOF

David
Stardate 4146.8

Try this:

if %~d0==\\ goto:EOF

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
C

Clay Calvert

Oh! Heh. I didn't realize ~d did that with UNC paths . . . .

David
Stardate 4147.4

It can come in handy, converselyI use %~pX to trim the backslashes
from the front of a computername. I use this when processing the
output of the "net view" command, and to ping (or nbtstat) the
%logonserver%.

C:\CMD>unc \\server1

%~d1 = \\
%~n1 =
%~p1 = server1
%~x1 =

Other techniques along this line can be used to extract the first
three octets of an IP address, and the last octet with a preceding
dot.

C:\CMD>unc 192.168.0.1

%~d1 = C:
%~n1 = 192.168.0
%~p1 = \CMD\
%~x1 = .1

Note that the working drive letter and path get 'parsed' in the above
example.

Thanks

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 

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