Batch file 2 remap network drive 2 another letter

Home Forums Scripting General Scripting Batch file 2 remap network drive 2 another letter

This topic contains 6 replies, has 2 voices, and was last updated by CaTBuS CaTBuS 12 years ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • CaTBuS
    CaTBuS
    Participant
    #128414

    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.

    Rems
    Rems
    Moderator
    #226566

    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 “\serversharescriptsfile.vbs”

    where the path: \serversharescripts can best be this path: \domain.localsysvoldomain.localscripts to place the script in.

    Code:
    ‘ vbscript name: [B]ForceG.vbs[/B]

    ‘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 = “\serversharefolder”

    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

    [COLOR=”SlateGray”]With CreateObject(“Scripting.FileSystemObject”)’,
    If Not (.DriveExists(appDrive) = True) Then[/COLOR]
    If GoMapping=True Then _
    objNetwork.MapNetworkDrive appDrive, appUNC, True ‘persistent
    [COLOR=”slategray”]Else
    ‘ msgbox “DRIVE NOT MAPPED !”&vbNewLine&vbNewLine& appDrive &” is in use by a ‘type ‘”& .GetDrive(appDrive).DriveType &”‘ drive”
    End If
    End With[/COLOR]

    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

    CaTBuS
    CaTBuS
    Participant
    #309100

    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.

    Rems
    Rems
    Moderator
    #226570

    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. :idea: :idea:

    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

    CaTBuS
    CaTBuS
    Participant
    #309101

    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.

    Rems
    Rems
    Moderator
    #226571

    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

    CaTBuS
    CaTBuS
    Participant
    #309102

    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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.