Announcement

Collapse
No announcement yet.

UNC change directory

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • UNC change directory

    I found CHDIR can be used to select an UNC path (this is an undocumented use for that statement). Hence to set the current directory to a path I can do one of the following:

    1 - If the path conatins a drive letter ( like E: ) I use CHDRIVE to set the drive and CHDIR to set the path
    2 - If the path is an UNC, I only use CHDIR

    Is this correct? Or must I use some API call to set the curret drive-path to an UNC?

    Another question. Is it a good practice to use CHDIR to see if a path exists?

    Aldo

    ------------------


    [This message has been edited by Aldo Cavini (edited May 03, 2001).]
    Rgds, Aldo

  • #2
    A question more. Let the current directory is c:\temp. If I execute the statement "CHDIR aldo", and "aldo" does not exist (neither file nor subdirectory), I get an ERR 53 instead of ERR 76. Have I to check for both error codes? What about ERR 75?

    ----
    (added later). I tested it, and found I get ERR 53 when CHDIR to a directory does not exist, and I get ERR 75 if I try to CHDIR to a name that is a file. Hence it is clear about errors 53 and 75 (I hope it really is), but not about errors 53 and 76.

    ------------------


    [This message has been edited by Aldo Cavini (edited May 03, 2001).]
    Rgds, Aldo

    Comment


    • #3
      CHDIR was intended to return an error 76. This is a confirmed problem in the current version of the compiler and will be corrected in the next update to the compiler.

      You can use a UNC name with CHDIR but we cannot guaranteed it will work on all operating system and network combinations.

      I hope this helps!


      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        thanks lance. the following code verifies or creates a directory regardless of the branch nesting. it is written to work both on unc and dos style paths - it is own responsibility if one uses it on uncs...

        remarks:
        1- only powerbasic statements - no apis
        2- only full path admitted (drive + full path, from the root)
        3- the program checks both err 53 and err 76 on chdirs
        4- may be it won't work on uncs (see lance's post)
        5- thanks to james moneypenny (see http://www.powerbasic.com/support/pb...ead.php?t=3686 who gave me some ideas.

        Code:
        ' action   :   0 verify only
        '            <>0 create directory
           
        ' on return:   0 directory exists or is correctly created (create)
        '              0 directory exists                         (verify)
        '             -1 directory does not exist                 (verify)
        '             -2 invalid drive / unc root
        '             -3 error occurred                           (create)
           
        function createverifypath( byval action as long, drivepath as string ) as long
          
        local oldp as string
        local driv as string
        local path as string
        local i    as long
           
          errclear                                      ' reset previous errors
          oldp = curdir$                                ' save the current directory
           
          path = rtrim$( drivepath, "\" ) + "\"         ' add a \ at the end if needed
           
          if left$( path, 2 ) = "\" then               ' extract "drive" for unc
            driv = "\" + parse$( path, "\", 3 ) + "\" + parse$( path, "\", 4 )
            path = mid$( path, len( driv ) + 1 )
            chdir driv                                  ' chdir at root for that drive
            
          elseif mid$( path, 2, 2 ) = ":\" then         ' extract drive for dos
            driv = left$( path, 2 )
            path = mid$( path, 3 )
            chdrive driv                                ' change drive
            if err = 0 then
              chdir "\"                                 ' chdir at root
            end if
           
          else                                          ' not full path: return error
            function = -2
            exit function
          end if
           
          if err <> 0 then                              ' error in switching to drive/root
            function = -2
           
          elseif action = 0 then                        ' verify only
            chdir path
            if err <> 0 then
              function = -1
            end if
           
          else                                          ' branches loop
            for i = 2 to parsecount( path, "\" ) - 1
              chdir parse$( path, "\", i )
              if err = 53 or err = 76 then              ' branch does not exist
                errclear
                mkdir parse$( path, "\", i )            ' creates the branch
                if err = 0 then
                  chdir parse$( path, "\", i )          ' switch to the branch
                end if
              end if
              if err <> 0 then                          ' error occurred
                function = -3
                exit for
              end if
            next i
          end if
           
          if mid$( oldp, 2, 1 ) = ":" then
            chdrive left$( oldp, 2 )                    ' restores the original drive
          end if
          chdir oldp                                    ' restores the original path
           
        end function
        ------------------


        [this message has been edited by aldo cavini (edited may 04, 2001).]
        Rgds, Aldo

        Comment


        • #5
          Also note, and please correct me if I am wrong but if you are changing DRIVES I understand you can still do a CHDIR without having to CHDRIVE?
          Used to in Dos I thot, but I always do a ChangeDrive anyway just to be safe....



          ------------------
          Scott
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            CHDIR can change the default path on the default drive, and it can also change the default path on non-default drives.

            It does not change the current drive though, unless you happen to (sucessfully) use a UNC name. Basically, if you need to change the default drive, use CHDRIVE.

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              Scott,

              if you try to use CHDRIVE on a UNC, you get an ERR 75. CHDRIVE only tests for the first letter of the drive parameter. An UNC starts with "\\" - the first character is a back-slash, hence it is an invalid drive identifier. This is the reason I can't use CHDRIVE on UNCs.

              ------------------
              Rgds, Aldo

              Comment

              Working...
              X