Sign in to follow this  
Followers 0
gcue

detect if script is being run from a mapped drive?

8 posts in this topic

is it possible to see if the script is being mapped from a mapped drive?

i get errors if someone runs it from a unc path (\\server\blah\blah.exe)

thanks

Share this post


Link to post
Share on other sites



DriveGetType(@ScriptDir)

If it is being run from a Network Drive it will return NETWORK


[sub]Quantum mechanics: The dreams stuff is made of[/sub]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

actually that just differentiates between types (local/network)..

it doesnt say differentiate between mapped or unmapped.

Edited by gcue

Share this post


Link to post
Share on other sites

how about something like this?

If @ScriptDir = "\\" Then

MsgBox(4096, "UNC")

EndIf

is there a way to say "Contains" instead of "="

i tried using "*" - no luck either

actually that just differentiates between types (local/network)..

it doesnt say differentiate between mapped or unmapped.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You could take the script location and compare it against a list of the mapped drives. Search the necessary functions in the help file for that.

Searching for "\\" in the script dir wont tell you if it's mapped either and you might as well use the first suggestion.

Also look up StringInStr and StringSplit for help parsing the locations.

Edited by Distrophy

Share this post


Link to post
Share on other sites

You could combine the above ideas:

$result = DriveGetType (@ScriptDir)

If $result = "Network" Then
    If StringLeft(@ScriptDir, 2) = "\\" Then
        ;Drive NOT mapped
    Else
        ;Drive mapped
    EndIf
EndIf

Unfortunately, if you happen to have launched the script from the UNC path but the drive is mapped it will still show not mapped.

Share this post


Link to post
Share on other sites

Here you go:

Dim $objWMIService = ObjGet("winmgmts:\\.\root\cimv2")

Dim $colDrives = $objWMIService.ExecQuery ("Select * From Win32_LogicalDisk Where DriveType = 4")

Dim $mapped = false

For $objDrive in $colDrives
    ;ConsoleWrite("Drive letter: " & $objDrive.DeviceID & @CRLF)
    ;ConsoleWrite("Network path: " & $objDrive.ProviderName & @CRLF)
    
    If StringLeft(@ScriptDir, 2) = $objDrive.DeviceID OR StringInStr(@ScriptDir, $objDrive.ProviderName) Then
        $mapped = True
        ExitLoop
    EndIf
Next

If $mapped Then
    MsgBox(0,"","Drive is mapped")
Else
    MsgBox(0,"","Drive is NOT mapped")
EndIf

Share this post


Link to post
Share on other sites

worked great!

thanks man

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0