How Can I Determine the Local Path to a Shared Folder?


Hey, Scripting Guy! Question

Hey, Scripting Guy! If I have a UNC path like \\server1\test, can I use a script to determine the local path to that shared folder (like C:\Scripts\Test Folder)?


SpacerHey, Scripting Guy! AnswerScript Center

Hey, JVK. Yes, you are correct: the Kirkland Fire did open their season last night, rallying for 4 runs in the last two innings to defeat Risan 4-2. And, yes, everyone in the league seems to think that Risan will be the team to beat this season. Mission accomplished.

Anyway, thanks for writing. Bye.

Oh, right; we almost forgot. Can you use a script to determine the local path to a shared folder? Of course you can (although we don’t know why anyone would bother to do that during baseball season). But if that’s what you want to do, well, here’s how you can do it:

strPath = “\\atl-fs-01\public”

strPath = Replace(strPath, “\\”, “”)

arrPath = Split(strPath, “\”)

strComputer = arrPath(0) strShare = arrPath(1)

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_Share Where Name = ‘” & strShare & “‘”)

For Each objItem in colItems Wscript.Echo objItem.Path Next

As you can see, we start off with a typical UNC path: \\atl-fs-01\public. We store that path, incidentally, in a variable named strPath. With the path name in hand we have to do two things. First, we need to separate the computer name (atl-fs-01) from the share name (public). Once that’s done we then need to connect to the computer atl-fs-01 and determine the local path for the public folder. How hard is that going to be? Not hard at all.

Especially for those of us who just managed to beat their “unbeatable” rivals. Did we mention that yet? The final score was 4-2, even though we didn’t play all that well. Must be really good coaching ….

But back to our regularly-scheduled program. To begin with, we need to get rid of the \\ that prefaces the UNC path. Although there are a couple different ways we could do that, we simply used the Replace function to replace each instance of \\ with nothing:

strPath = Replace(strPath, “\\”, “”)

After that line of code executes, the variable strPath will be equal to atl-fs-01\public. In turn, that means we can use the Split function to split this value into an array:

arrPath = Split(strPath, “\”)

By splitting on the \, we end up with an array consisting of two items:



Needless to say, those are the two pieces of information we’re looking for: the name of the computer and the name of the shared folder. With that in mind, we then assign the first item in the array (item 0) to a variable named strComputer, and the second item in the array (item 1) to a variable named strShare:

strComputer = arrPath(0)
strShare = arrPath(1)

So now do we get to the hard part? Believe it or not, that was the hard part; from here on we just use a standard WMI script. We start out by connecting to the WMI service on the remote computer. (Which remote computer? Atl-fs-01, the computer whose name is stored in the variable strComputer.) We then use the ExecQuery method to retrieve a collection of all the shared folders on that computer:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_Share Where Name = ‘” & strShare & “‘”)

Good point: we aren’t getting all the shared folders, are we? Instead, we’re getting only those shared folders that have a Name equal to the value stored in the variable strShare. (If you dozed off for a moment, strShare is equal to public, the name of the shared folder we’re interested in.) All that’s left now is to set up a For Each loop to walk through the collection (because shared folder names must be unique on a computer, there will be only one item in the collection) and echo back the value of the Path property. As you probably guessed, the Path property tells us the local path to the folder on atl-fs-01:


That’s all you have to do to take a UNC path and determine the local folder path.

And now for the most important part. There we were, runners on first and second, down 2-0, when the third base coach daringly called for a double steal that put us in position to tie the score. Pretty gutsy call, huh? Wonder who that fearless baseball genius could have been ….


Comments are closed. Login to edit/delete your existing comments

Feedback usabilla icon