How Can I Open a Window to a UNC Location?

How Can I Open a Window to a UNC Location?

  • Comments 2
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I open a window to a UNC location? You've given many examples that involve opening a web page. I have been unable to get a similar result trying to get a script to open \\server\share.

-- CY

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CY. You know, during the heyday of the Cold War the U.S. had analysts whose sole duty was to pore over photographs taken in the Soviet Union and try to figure out what was really going on behind the Iron Curtain. Who was standing next to whom? Who had been relegated to the background? Who had been airbrushed out of the picture altogether? Just what was going on over there?

Today those same analysts spend their time poring over Hey, Scripting Guy! columns. Would the Scripting Guys “tell you porkies?” What wasFrank “Home Run” Baker known for besides leading major league baseball in home runs four times? Can you reallymake scrambled eggs just by shaking a chicken really hard right before she lays her eggs? Just what is going on over there?

Needless to say, we can’t reveal all our secrets. But here’s a tip. Sometimes you’ll hear the Scripting Guys say something like, “You know, this is something we get asked quite a bit and, fortunately, the solution is quite simple.” They’ll then proceed to show everyone a two-line script. Does that mean that this is a question that the Scripting Guys really do get asked quite a bit? Well, maybe. What it mostly means, however, is that the Scripting Guys are feeling lazy that day and are trying to get away with writing two lousy lines of code and calling it a column. Don’t let them get away it! As loyal readers of Hey, Scripting Guy! you deserve better than that.

With that out of the way, let’s see if we can tackle today’s problem: opening a window to a UNC location. You know, this is something we get asked quite a bit and, fortunately, the solution is quite simple. In fact, this task can be carried out using just two lines of code:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "\\atl-fs-01\public"

As you can see, there isn’t much to this script. We start out by creating an instance of the Wscript.Shell object; once we have a handle to that object all we have to do is call the Run method, passing our UNC path as the sole method parameter. Will that really open \\atl-fs-01\public in a Windows Explorer window? Give it a try and see for yourself.

The only thing you need to watch out for are UNC paths that include blank spaces. For example, consider the following script, one that purports to open the folder \\atl-fs-01\public\ken myer:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "\\atl-fs-01\public\ken myer"

Is that going to work? Unfortunately, no; that’s because Windows is going to assume that the path ends at the first blank space, making the path \\atl-fs-01\public\ken. All you’re going to back is the following error message:

The system cannot find the file specified.

But that’s OK; this is easy to fix. Any time your UNC path includes a blank space (or blank spaces) you need to enclose the entire path in double quote marks. (Yes, in much the same way you need to use double quote marks when typing a command like cd “c:\documents and settings\ken myer” from the command prompt.) Of course, it looks as though we already have double quote marks. However, those double quotes simply indicate that we are dealing with a string value; they aren’t part of the value itself. This is crazy-looking, but the following syntax will take care of the blank space issue:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run """\\atl-fs-01\public\ken myer"""

Three double quote marks before the path and three double quote marks after the path. That’s the recipe for success, in life as well as in scripting.

Here’s a little bonus script, one that we won’t discuss in much detail. This script returns a collection of all the shared folders on a computer (in this case, a remote computer named atl-fs-01), taking care to eliminate all the administrative shares. (For our purposes, that means any shared folder that, like C$ or printers$, has a $ in the name.) After returning the collection the script constructs a UNC path for each shared folder and then opens that folder in its very own window:

strComputer = "atl-fs-01"

Set objShell = CreateObject("Wscript.Shell")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Share")

For Each objItem in colItems
    If InStr(objItem.Name, "$") = 0 Then
        strPath = "\\" & strComputer & "\" & objItem.Name
        objShell.Run strPath
    End If
Next

Pretty slick, huh?

And before we go, here’s another handy hint for you. Any time the Scripting Guys start handing out bonus scripts that can mean only one thing: they’re feeling a little guilty about not putting much effort into their work. Just something to watch out for.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Thank you very much! &nbsp;I didn&#39;t know we have to use double quotes when there is a space in the path. &nbsp;I looked for days for this information.</p> <p>Thanks again.</p>

  • <p>I am trying &nbsp;to create a linking document in word to various network locations, as I get older &amp; have so many locations I forget where they all are. My problem is with spaces in path names, as below the 1st path works &amp; the second does not, tried with one or 2 quotes also tried quotes only around the manuals &amp; whitepapers section. &nbsp;I would appreciate any ideas?</p> <p>Drivers \\192.168.1.205\public\drivers</p> <p>Manuals &amp; whitepapers “”\\192.168.1.205\public\manuals &amp; whitepapers””</p>