Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Check Network path exists  (Read 21545 times)

0 Members and 1 Guest are viewing this topic.

NEILD

    Topic Starter


    Rookie

    Check Network path exists
    « on: April 19, 2010, 03:06:40 AM »
    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
    Neil

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Check Network path exists
    « Reply #1 on: April 19, 2010, 04:56:00 PM »
    If 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.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    NEILD

      Topic Starter


      Rookie

      Re: Check Network path exists
      « Reply #2 on: April 20, 2010, 12:16:35 AM »
      Thanks sidewinder !!

      Neil

      NEILD

        Topic Starter


        Rookie

        Re: Check Network path exists
        « Reply #3 on: April 20, 2010, 12:34:45 AM »
        Sidewinder...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
        Neil
        « Last Edit: April 20, 2010, 12:53:57 AM by NEILD »

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Check Network path exists
        « Reply #4 on: April 20, 2010, 06:18:30 AM »
        Quote
        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 ;D

        Good luck. 8)

        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Check Network path exists
        « Reply #5 on: April 21, 2010, 11:00:29 PM »
        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.aspx
        I was trying to dereference Null Pointers before it was cool.

        NEILD

          Topic Starter


          Rookie

          Re: Check Network path exists
          « Reply #6 on: April 28, 2010, 11:26:00 PM »
          Hi 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
          Neil

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Check Network path exists
          « Reply #7 on: April 29, 2010, 06:29:47 AM »
          Code: [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.

           8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          NEILD

            Topic Starter


            Rookie

            Re: Check Network path exists
            « Reply #8 on: May 04, 2010, 05:59:21 AM »
            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
            Neil

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Check Network path exists
            « Reply #9 on: May 04, 2010, 07:09:51 PM »
            The 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?  8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Check Network path exists
            « Reply #10 on: May 04, 2010, 07:41:11 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...
            I was trying to dereference Null Pointers before it was cool.