How do I determine the local path to a shared folder?
Ask:
Hello, Scripting Guy! If you have a UNC path (for example, \\server1\test), can you use a script to determine the local path of a shared folder, such as a C:\Scripts\Test folder?
--JVK
For:
Hello, JVK. Yes, you're right.
Oh, yes; 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 there are xianxin to do it during the baseball season). But if this is really what you want to do, it tells you what to do:
Copy Code code as follows:
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 first used a typical UNC path: \\atl-fs-01\public. Incidentally, we store the path in a variable named strpath. With this path name, we have to do two things. First, we must separate the computer name (atl-fs-01) from the share name (public). After that, we must connect to the computer atl-fs-01 and determine the local path to the public folder. How hard is that going to be? It's not difficult at all.
Especially for those of us who have just managed to beat the "invincible" opponents of the script writers. We haven't said that yet, have we? The final score was 4:2, even though we didn't play that well. Must be a very good coach ...
Let's get this over with. First, we have to remove the \ before the UNC path. Although there are several other ways, we simply use the Replace function to replace each of the occurrences of \ \ With an empty content:
strpath = Replace (strpath, "\ \", "")
After this line of code is executed, the variable strpath equals Atl-fs-01\public. This means that we can use the Split function to divide this value into an array:
Arrpath = Split (strpath, "\")
After separating, we end up with an array of two data:
? Atl-fs-01
? Public
Needless to say, this is the two messages we are looking for: computer name and shared folder name. With that in mind, we assign the first item (item 0) in the array to a variable named StrComputer, and assign the second item (item 1) to a variable named Strshare:
StrComputer = Arrpath (0)
Strshare = Arrpath (1)
So, are we in the difficult part now? Whether you believe it or not, this is the more difficult part; After that, we just use standard WMI scripts. We first connect to the WMI service on the remote computer. (which remote computer?) Is atl-fs-01, the name of the computer is stored in a variable named StrComputer. Then, we 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 idea: We didn't get all the shared folders, did we? Instead, we only get those shared folders that have the Name property equal to the value of the strshare variable. (When you take a nap, strshare equals public, which is the name of the shared folder that we want.) The next thing to do is to set up a For Each loop to traverse the collection (because the shared folder name must be unique on one computer, so there will be only one item in the collection), and then echo the value of the Path property. As you may have guessed, the path attribute tells us the local path to the folder on the atl-fs-01:
D:\Scripts\Public
This is all you have to do to get a UNC path and determine what the local folder path needs to do.