Announcement

Collapse
No announcement yet.

Batch file 2 remap network drive 2 another letter

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

  • Batch file 2 remap network drive 2 another letter

    Ok, I am preparing to roll out a crappy 16 bit application that requires a mapped drive G: to upload user log files. My current batch file checks for the use of G: If it is in use, it deletes it and recreates it to the new location. The problem with this is if G: is already in use, the user will lose it.

    I would like to create a batch file that will check for G:, if it is in use, remaps it to the next available drive letter and then creates my new G: location. Also, any other suggestions would be appreciated. Thank you.

  • #2
    Re: Batch file 2 remap network drive 2 another letter

    It would be very difficult doing that by a batch. It is better to use vbscipt.

    If it is possible replace your entire batch with a vbscript, if not then call the vbscript below from within your batch by using the line:
    @cscript.exe //NoLogo "\\server\share\scripts\file.vbs"

    where the path: \\server\share\scripts can best be this path: \\domain.local\sysvol\domain.local\scripts to place the script in.

    Code:
    ' vbscript name: ForceG.vbs
    
    'This script forces a persistent driveletter to a certain UNC-path
    'If this UNC-path is already mapped with another driveletter,
    ' it will be re-mapped.
    'If the driveletter is in use by another mapping, that mapping will
    ' first be re-mapped to the first available drive letter.
    
    appDrive = "g:"
    appUNC = "\\server\share\folder"
    
    Set objNetwork = CreateObject("Wscript.Network")
    
    Set colDrives = objNetwork.EnumNetworkDrives
    
    sPare=GetSpareLetter()
    
    For i = 0 to colDrives.Count-1 Step 2
            sDrive = colDrives.Item(i)
            sUNC = colDrives.Item(i+1)
            If UCase(sDrive) = UCase(appDrive) Then
               If UCase(sUNC) = UCase(appUNC) Then
                 GoMapping=False 'Already correctly mapped
                 exit for
               Else
                 objNetwork.RemoveNetworkDrive sDrive, True, True
                 wscript.sleep(50)
                 objNetwork.MapNetworkDrive sPare, sUNC, True 'persistent
                 GoMapping=True 'driveletter *was* in use by an other mapping 
               End If
            ElseIf UCase(sUNC) = UCase(appUNC) Then
               If Not UCase(sDrive) = UCase(appDrive) Then
                 objNetwork.RemoveNetworkDrive sDrive, True, True
                 wscript.sleep(50)
                 GoMapping=True  'It *was* mapped to an otherdriveletter
                 Exit For
               End If
            Else GoMapping=True   'there is no G initially, and the path is not mapped with another letter.
            End If
    Next
    
    With CreateObject("Scripting.FileSystemObject")',
     If Not (.DriveExists(appDrive) = True) Then
         If GoMapping=True Then _
         objNetwork.MapNetworkDrive appDrive, appUNC, True 'persistent
     Else  
      ' msgbox "DRIVE NOT MAPPED !"&vbNewLine&vbNewLine& appDrive &" is in use by a 'type '"& .GetDrive(appDrive).DriveType &"' drive"
     End If
    End With
    
    
    Function GetSpareLetter()
       'Find the first avaiable driveletter
       With CreateObject("Scripting.FileSystemObject")
        For i = Asc("D") To Asc("Z") 
            If Not .DriveExists(Chr(i)) Then 
               GetSpareLetter = Chr(i) & ":" 
               Exit For 
            End If 
        Next
       End With
    End Function
    Last edited by Rems; 2nd November 2007, 19:44. Reason: added the 'grey-color' text

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Batch file 2 remap network drive 2 another letter

      REMS, this is perfect. You are a GOD!! Thank you so much for your time and effort.

      Comment


      • #4
        Re: Batch file 2 remap network drive 2 another letter

        Note !:
        I just changed the line:
        If GoMapping=True Then _
        EDIT: changed it back

        In the checking For-Next loop I added the line: Else GoMapping=True

        So now it maps also when nothing had to be changed first.

        It would be great if you let us know in this thread whether the script was working for your environment.
        (That will make all solutions in this forum worthful to others looking for answers/knowledge.)

        You can personaly thank contributers in this forum by clicking on the yinyang at the top-right corner of a solution (the yinyang is visible when you are logged on).


        Glad I could help
        cheers,

        \Rems
        Last edited by Rems; 24th October 2007, 10:31.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Batch file 2 remap network drive 2 another letter

          That modification doesn't seem to change anything. The only thing that I could possibly find wrong with this script is that if there is no G: initially, nothing happens. By this I mean no G: is created. However, if G: is currently mapped elsewhere, then it is reassigned to the next available drive letter and then G: is created properly.

          I think this is what you were getting at with the modification from your last post, I just wanted to clarify.

          Comment


          • #6
            Re: Batch file 2 remap network drive 2 another letter

            Thank you CaTBuS for the feedback helping improving the script.
            I re-changed the script, it should be fine now


            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Batch file 2 remap network drive 2 another letter

              Thank you again REMS. I have already tested the new sc and am going to roll out our sms package to our test group today.

              This script has been tested successfully in Windows XP Pro and Vista Ultimate environments. All contingencies of drive states were tested.

              I am pulling my VB book out of the case after today. REMS has inspired me. On another note, I am posting another thread that will hopefully add to this script. I am currently using a GPO edit to then hide the below G: drive. Although the application needs full access to this mapped drive, I don't want the users browsing to the unc path and doing damage.
              Last edited by CaTBuS; 24th October 2007, 13:54.

              Comment

              Working...
              X