Announcement

Collapse
No announcement yet.

How to detect first available drive for network?

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

  • How to detect first available drive for network?

    Fellows,

    How can a program (written in PBDLL60) detect which drive is the first available free logical drive for a network connection?
    Using GetDriveType, %DRIVE_REMOTE can only be detected when a connection has already been made.
    I remember from my 'good old' DOS age that you could pass a questionmark, in order to let the program find the first free drive letter. Is there a similar thing in Windows?

    Thanks a lot,
    Egbert

    ------------------
    mailto:[email protected][email protected]</A>
    www.basicguru.com/zijlema/

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    Here's some old code, written by me some time ago. It lists all used drive. If you change some things (and optimize it of course, because it's a bad solution), you can get the first free drive letter with it.

    Code:
    FUNCTION GetDrives () AS STRING 'alle vorhanden laufwerke auflisten
      LongVar& = GetLogicalDrives                  '(auch netzlaufwerke)
      FOR x& = 0 TO 25                             '1 f?r vorhanden, 0 f?r nicht vorhanden
        IF LTRIM$(STR$(BIT(LongVar&, x&),1)) = "1" THEN
           SELECT CASE x&
                  CASE 0
                       a$ = "A:\"
                  CASE 1
                       a$ = "B:\"
                  CASE 2
                       a$ = "C:\"
                  CASE 3
                       a$ = "D:\"
                  CASE 4
                       a$ = "E:\"
                  CASE 5
                       a$ = "F:\"
                  CASE 6
                       a$ = "G:\"
                  CASE 7
                       a$ = "H:\"
                  CASE 8
                       a$ = "I:\"
                  CASE 9
                       a$ = "J:\"
                  CASE 10
                       a$ = "K:\"
                  CASE 11
                       a$ = "L:\"
                  CASE 12
                       a$ = "M:\"
                  CASE 13
                       a$ = "N:\"
                  CASE 14
                       a$ = "O:\"
                  CASE 15
                       a$ = "P:\"
                  CASE 16
                       a$ = "Q:\"
                  CASE 17
                       a$ = "R:\"
                  CASE 18
                       a$ = "S:\"
                  CASE 19
                       a$ = "T:\"
                  CASE 20
                       a$ = "U:\"
                  CASE 21
                       a$ = "V:\"
                  CASE 22
                       a$ = "W:\"
                  CASE 23
                       a$ = "X:\"
                  CASE 24
                       a$ = "Y:\"
                  CASE 25
                       a$ = "Z:\"
           END SELECT
    
          'a$ contains the used drive. you can print it, add it to a listbox or whatever
    
         END IF                                                           
      NEXT
    END FUNCTION
    ------------------
    E-Mail (home): mailto:[email protected][email protected]</A>
    E-Mail (work): mailto:[email protected][email protected]</A>

    Comment


    • #3
      An alternative would be to continue using GetDriveType, and use
      the first drive letter that returns %DRIVE_NO_ROOT_DIR. That's
      the standard result for drives not recognized by the OS. I don't
      *think* that the function ever returns %DRIVE_UNKNOWN for an
      unrecognized drive. It shouldn't, however, hurt to test for BOTH
      cases.

      Code:
      FOR A& = 67 TO 90
          B& = GetDriveType(CHR$(A&) + ":\")
          IF B& = %DRIVE_NO_ROOT_DIR THEN NewDrive$ = CHR$(A&) : EXIT FOR
      NEXT A&
      Sven,

      Your code could be "tightened up" a bit by using the following
      instead of the SELECT CASE block:

      'use the following if the test returns TRUE for an invalid drive
      a$ = CHR$(Result& + 65) + ":\" : EXIT FOR

      Result& would be the return value by the test in the
      FOR/NEXT block. I apologize, but I can't remember most of the code
      that Sven posted, such as the variable names he used.

      Hope this helps, Egbert!

      ------------------
      Clay C. Clear
      http://www.v3space.com/a/a39/202/

      [email protected]
      [email protected]

      [This message has been edited by Clay Clear (edited July 05, 2001).]

      Comment


      • #4
        This is an addendum to my first reply:

        Sven's code might be better redone by using:

        'code for getting logical drive information goes here

        Code:
        FOR A& = 0 TO 25
            IF ISTRUE BIT(LongVar&, A&) THEN
               NewDrive$ = CHR$(A& + 65) + ":\" : EXIT : EXIT
            END IF
        NEXT A&
        Enjoy!


        ------------------
        Clay C. Clear

        http://www.v3space.com/a/a39/202/

        [email protected]
        [email protected]

        Comment


        • #5
          This is what I head laying around:

          Code:
          FUNCTION AvailableDrive() AS STRING
            LOCAL mask AS LONG, count AS LONG
            mask = 8 : count = 68                        ' mask and ASCII-code for drive D
            RetVal& = GetLogicalDrives()                 ' get drives in use
            IF RetVal& = 0 THEN                          ' error
              FUNCTION = STR$(RetVal&)
          '    FUNCTION = RetVal&
              EXIT FUNCTION
            END IF
            DO WHILE (RetVal& AND mask) <> 0
              mask = mask * 2                            ' next bit
              INCR count                                 ' next drive letter
              IF count > 90 THEN                         ' > Z? There are no more, folks!
                FUNCTION = "0"
                EXIT FUNCTION
              END IF
            LOOP
            SELECT CASE count
                   CASE 68
                        FUNCTION="D:"
                   CASE 69
                        FUNCTION="E:"
                   CASE 70
                        FUNCTION="F:"
                   CASE 71
                        FUNCTION="G:"
                   CASE 72
                        FUNCTION="H:"
                   CASE 73
                        FUNCTION="I:"
                   CASE 74
                        FUNCTION="J:"
                   CASE 75
                        FUNCTION="K:"
                   CASE 76
                        FUNCTION="L:"
                   CASE 77
                        FUNCTION="M:"
                   CASE 78
                        FUNCTION="N:"
                   CASE 79
                        FUNCTION="O:"
                   CASE 80
                        FUNCTION="P:"
                   CASE 81
                        FUNCTION="Q:"
                   CASE 82
                        FUNCTION="R:"
                   CASE 83
                        FUNCTION="S:"
                   CASE 84
                        FUNCTION="T:"
                   CASE 85
                        FUNCTION="U:"
                   CASE 86
                        FUNCTION="V:"
                   CASE 87
                        FUNCTION="W:"
                   CASE 88
                        FUNCTION="X:"
                   CASE 89
                        FUNCTION="Y:"
                   CASE 90
                        FUNCTION="Z:"
                   CASE ELSE
                        FUNCTION=""
              END SELECT
          '  FUNCTION = count
          END FUNCTION
          Think it needs no further comment.



          ------------------
          Herman
          You gotta walk and don't loop back.
          You gotta run, and don't loop back.

          Comment


          • #6
            Herman, an optimisation - replace this:
            Code:
              SELECT CASE count
                     CASE 68
                          FUNCTION="D:"
                     CASE 69
                          FUNCTION="E:"
                     CASE 70
                          FUNCTION="F:"
                     CASE 71
                          FUNCTION="G:"
                     CASE 72
                          FUNCTION="H:"
                     CASE 73
                          FUNCTION="I:"
                     CASE 74
                          FUNCTION="J:"
                     CASE 75
                          FUNCTION="K:"
                     CASE 76
                          FUNCTION="L:"
                     CASE 77
                          FUNCTION="M:"
                     CASE 78
                          FUNCTION="N:"
                     CASE 79
                          FUNCTION="O:"
                     CASE 80
                          FUNCTION="P:"
                     CASE 81
                          FUNCTION="Q:"
                     CASE 82
                          FUNCTION="R:"
                     CASE 83
                          FUNCTION="S:"
                     CASE 84
                          FUNCTION="T:"
                     CASE 85
                          FUNCTION="U:"
                     CASE 86
                          FUNCTION="V:"
                     CASE 87
                          FUNCTION="W:"
                     CASE 88
                          FUNCTION="X:"
                     CASE 89
                          FUNCTION="Y:"
                     CASE 90
                          FUNCTION="Z:"
                     CASE ELSE
                          FUNCTION=""
                END SELECT
            With this:
            Code:
             SELECT CASE count
              CASE 67 - 90: FUNCTION = CHR$(count) & ":"
              CASE ELSE: FUNCTION = ""
             END SELECT



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

            Comment


            • #7
              Thanks for al your fast help, guys!

              Essentially it is the following: if GetDriveType() returns %DRIVE_NO_ROOT_DIR it is 'bingo!' I simply did not know the meaning of that value..
              Here is my contribution. I prefer a long integer (i.e. the drive letter's ascii code) in return, in order to create more flexibility in my application..

              Code:
              FUNCTION FirstFreeDrive() AS LONG
                LOCAL aski AS LONG, dType AS LONG
                aski = 67
                DO
                  INCR aski                                       ' start with D:\
                  IF aski > 90 THEN
                    MSGBOX "No free drive found", 16, "Error"
                    EXIT FUNCTION                                 ' stop searching
                  END IF
                  dType = GetDriveType(CHR$(aski) + ":\")
                LOOP UNTIL dType = %DRIVE_NO_ROOT_DIR
                FUNCTION = aski
              END FUNCTION
              ------------------
              mailto:[email protected][email protected]</A>
              www.basicguru.com/zijlema/

              Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
              http://zijlema.basicguru.eu
              *** Opinions expressed here are not necessarily untrue ***

              Comment


              • #8
                Glad to help, Egbert!

                I'm posting this final reply to this topic to give you additional
                notes about using GetDriveType. The note is:

                GetDriveType will also return %DRIVE_NO_ROOT_DIR on any floppy
                drive (drives a: or b that doesn't exist. So, if your only
                use of it in a particular program is to check for available
                Network drives, then start your loop with ASC("C"). if you start
                at the bottom with ASC("A"), then it will erroneously report the
                first available drive as one of the floppy drives, if you don't
                have both floppy drive letters already in use by the system.




                ------------------
                Clay C. Clear

                http://www.v3space.com/a/a39/202/

                [email protected]
                [email protected]

                Comment


                • #9
                  The BFF code on my site uses com to enum objects.
                  If you are desperate you could use that one


                  ------------------
                  hellobasic

                  Comment

                  Working...
                  X