ZetCode

VBScript Explore Method

last modified April 9, 2025

The Explore method in VBScript is part of the Shell.Application object. It opens Windows Explorer at a specified folder location. This method provides programmatic control over file system navigation. It's commonly used to reveal files or directories to users.

Explore launches a new Explorer window with the specified path selected. It differs from simply opening a folder as it highlights the item. This tutorial covers Explore with practical examples to demonstrate its usage in various scenarios.

Explore Method Overview

The Explore method takes one parameter: the path to explore. It doesn't return a value but opens a Windows Explorer window. The method is available through the Shell.Application object in VBScript.

Key features include highlighting the specified path in Explorer. It works with both file and folder paths. The method raises an error if the path doesn't exist. Understanding this method helps create user-friendly file navigation.

Basic Folder Exploration

This example demonstrates the simplest use of Explore to open a folder in Windows Explorer. It shows how to launch Explorer with a specific directory selected. The path must exist for the method to work.

basic_explore.vbs
Set shell = CreateObject("Shell.Application")
shell.Explore "C:\Windows\System32"

Set shell = Nothing

The script creates a Shell.Application object and calls Explore. The System32 folder is opened in Windows Explorer. The folder contents are displayed with the folder highlighted in the navigation pane.

Exploring User Documents

This example shows how to open the current user's Documents folder. It uses environment variables to locate the folder reliably. The path is constructed dynamically for different user accounts.

user_documents.vbs
Set shell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")

documentsPath = wshShell.ExpandEnvironmentStrings("%USERPROFILE%\Documents")
shell.Explore documentsPath

Set shell = Nothing
Set wshShell = Nothing

The script retrieves the Documents folder path using environment variables. ExpandEnvironmentStrings converts %USERPROFILE% to the actual path. This approach works across different Windows installations and user accounts.

Exploring Network Shares

Explore can open network shared folders just like local paths. This example demonstrates accessing a shared network location. The UNC path format is used for network resources.

network_share.vbs
Set shell = CreateObject("Shell.Application")
networkPath = "\\ServerName\SharedFolder"

On Error Resume Next
shell.Explore networkPath

If Err.Number <> 0 Then
    WScript.Echo "Failed to access network share: " & Err.Description
End If

Set shell = Nothing

The script attempts to open a network share in Explorer. Error handling is included as network paths may be unavailable. The UNC path must be accessible with proper permissions for this to work.

Exploring with File Selection

This example shows how to highlight a specific file in Explorer. While Explore primarily works with folders, it can select files within their parent folders. The containing folder is opened with the file selected.

file_selection.vbs
Set shell = CreateObject("Shell.Application")
filePath = "C:\Temp\report.docx"

On Error Resume Next
shell.Explore filePath

If Err.Number <> 0 Then
    WScript.Echo "File not found: " & filePath
End If

Set shell = Nothing

When given a file path, Explore opens the parent folder and selects the file. This is useful for revealing files to users. The file must exist for the selection to work properly.

Exploring Special Folders

Windows has special folders like Desktop or Downloads that can be explored. This example uses Shell.SpecialFolders to get these paths. The method provides reliable access to system folders.

special_folders.vbs
Set shell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")

desktopPath = wshShell.SpecialFolders("Desktop")
shell.Explore desktopPath

Set shell = Nothing
Set wshShell = Nothing

The script retrieves the Desktop folder path using SpecialFolders. This method works across different Windows versions and user configurations. The Desktop folder is then opened in Explorer.

Source

Shell.Application Documentation

In this article, we have explored the Explore method in VBScript, covering its usage and practical applications. From basic folder navigation to network shares and special folders, these examples demonstrate reliable Explorer integration. With this knowledge, you can enhance your scripts with user-friendly file system navigation.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all VBScript tutorials.