1.

Solve : Check Network path exists?

Answer»

Hi everyone,

Can someone advise on checking if a network path (Z:\abc) is already in existance ?

If the answer is no I want to create it...I was just going to use "goto commands for this part"

Cheers
NeilIf the network drive is mapped to drive Z, then this should work:

Code: [Select]if exist Z:\nul (do something) else (do something else)

If the network drive is not mapped to a drive letter, this notation should work:

Code: [Select]if exist \\servername\sharename\ (do something) else (do something else)

Good luck. Thanks sidewinder !!

NeilSidewinder...just a couple quick questions :-

1. What is the nul for :-
Z:\nul

2. I tried to use the same code to check for a exists file :-
if exist C:\cae_prog\pdms\v11.6\CadCentre\mong_evars.bat (goto :evarscopied) else (goto :copyevars)

But it CARRIED out both :-
:evarscopied
:copyevars

So what did I get wrong ?

Cheers
NeilQuote

1. What is the nul for :-
Z:\nul

It's legacy code to indicate a directory and not a file. Don't think it's necessary on the more modern OSes.

Quote
2. I tried to use the same code to check for a exists file :-
if exist C:\cae_prog\pdms\v11.6\CadCentre\mong_evars.bat (goto :evarscopied) else (goto :copyevars)

But it carried out both :-
:evarscopied
:copyevars

I think control was passed to :evarscopied and then FELL into :copyevars. Normally goto is used without the : marker and the call is used with the : marker when calling an internal subroutine.

Code: [Select]if exist C:\cae_prog\pdms\v11.6\CadCentre\mong_evars.bat (call :evarscopied) else (call :copyevars)
.
.
.
goto :eof

:evarscopied
.
.
.
goto :eof

:copyevars
.
.
.
goto :eof

Before call :label came on the scene, I used to use goto for internal subroutines by setting a variable to a label name immediately following the goto. I could then use a goto to the variable (which held the label name); this allowed control to be returned to the next sequential instruction after the goto. Primitive yes, but in those days we learned to make do

Good luck.

Quote from: Sidewinder on April 20, 2010, 06:18:30 AM
It's legacy code to indicate a directory and not a file. Don't think it's necessary on the more modern OSes.

http://blogs.msdn.com/oldnewthing/archive/2003/10/22/55388.aspxHi almost got this working...one glitch is left the server name...

If I was going to path my drive and folder in I would use :-
net use Z: \\SAOSL10005.ALST.NO\TCM /PERSISTENT:NO

I've tried this...to check the existance of the drive :-
if exist \\Saosl10005.Alst.no\Tcm (goto :pathexists) else (goto :newpath)

But it's not working...

So what have I got wrong in my code..

Thanks
NeilCode: [Select]If I was going to path my drive and folder in I would use :-
net use Z: \\SAOSL10005.ALST.NO\TCM /PERSISTENT:NO

What does "I would use" mean? Did you use net use or not?

If the drive is mapped successfully, there is no reason for the exist test. Do you really want to use goto and not call? By using the : marker it makes it difficult to see what your trying to do.

Based on your code, this might be simpler:

Code: [Select]@echo off
if exist Z:\nul goto :pathexists
net use Z: \\SAOSL10005.ALST.NO\TCM /PERSISTENT:NO 1>NUL 2>NUL
if errorlevel 1 goto :error
if errorlevel 0 goto :pathexists

:pathexists
echo pathexists
goto :eof

:error
echo error
goto :eof

Should you decide to use call instead of goto, the code may need to be tweaked.

I wanted to check the for the full path :-
Z: \\SAOSL10005.ALST.NO\TCM

Just INCASE there was already a Z:\, but different path :-
Z: \\SAOSL10006.ALST.NO\Files

Cheers
NeilThe path is either Z:\ or \\SAOSL10005.ALST.NO\TCM, the former being conventional notation, the latter using UNC notation.

You can't use both, but you should be able to parse the net use output which will co-relate the local mapped drive to the remote device name. That should give you enough information as to whether the Z: drive is mapped and if so mapped correctly.

Code: [Select]@echo off
for /f "tokens=2-3" %%i in ('net use ^| find /i "Z:"') do (
if not errorlevel 1 (
set local=%%i
set remote=%%j
goto Mapped
)
)

:UnMapped
.
.
.
goto :eof

:Mapped
echo Local drive: %local%
echo Remote Drive: %remote%
.
.
.
goto :eof

If drive Z: is not mapped, the logic will fall into the :UnMapped label. If drive Z: is mapped, the logic will leave the for LOOP and start executing at the :Mapped label. I'll let you fill in the blanks.

The if statement in the for loop is written *censored*-backward. Unlike REXX for example, the nop instruction does not exist in batch code. NOP (no operation) is a GREAT target for conditional statements as it improves readability.

Dare I say good luck? Quote from: Sidewinder on May 04, 2010, 07:09:51 PM
The if statement in the for loop is written *censored*-backward. Unlike REXX for example, the nop instruction does not exist in batch code. NOP (no operation) is a great target for conditional statements as it improves readability.

REXX is the only language aside assembly that has a NOP, as far as I know...


Discussion

No Comment Found