ZetCode

VBScript BuildPath Method

last modified April 9, 2025

The BuildPath method in VBScript is part of the FileSystemObject. It combines a base path with a relative path into a single path string. This method automatically handles path separators, making path concatenation more reliable. It's commonly used in file operations and directory management.

BuildPath ensures proper path construction regardless of trailing backslashes. It simplifies script development by handling path separator logic automatically. This tutorial covers BuildPath with practical examples to demonstrate its usage.

BuildPath Method Overview

The BuildPath method takes two parameters: an existing path and a name to append. It returns a string combining these paths with the appropriate separator. The method is available through the FileSystemObject in VBScript scripting.

Key features include automatic backslash handling and platform-appropriate separators. It doesn't verify path existence or validity. BuildPath works with both file and directory paths. Understanding this method helps create robust file handling scripts.

Basic Path Combination

This example demonstrates the simplest use of BuildPath to combine two path components. It shows how the method automatically inserts the correct separator. The base path and additional name are combined into a complete path.

basic_buildpath.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
combinedPath = fso.BuildPath("C:\Documents", "Reports")
WScript.Echo combinedPath ' Output: C:\Documents\Reports

Set fso = Nothing

The script creates a FileSystemObject and calls BuildPath. The base path "C:\Documents" is combined with "Reports". The result is "C:\Documents\Reports". Notice the automatic backslash insertion between components.

Handling Trailing Backslashes

BuildPath correctly handles cases where the base path ends with a backslash. This example shows how the method avoids duplicate separators. It demonstrates consistent behavior regardless of trailing backslashes.

trailing_backslash.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
path1 = fso.BuildPath("C:\Temp\", "file.txt")
path2 = fso.BuildPath("C:\Temp", "file.txt")

WScript.Echo path1 ' Output: C:\Temp\file.txt
WScript.Echo path2 ' Output: C:\Temp\file.txt

Set fso = Nothing

Both path1 and path2 produce identical results despite different base paths. BuildPath normalizes the output by handling the trailing backslash properly. This consistency makes path construction more reliable in scripts.

Building Nested Directory Paths

BuildPath can create complex nested directory paths by chaining calls. This example shows how to construct multi-level directory structures. Each BuildPath call adds another level to the path hierarchy.

nested_paths.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
basePath = "C:\Projects"
level1 = fso.BuildPath(basePath, "Website")
level2 = fso.BuildPath(level1, "Images")
level3 = fso.BuildPath(level2, "Products")

WScript.Echo level3 ' Output: C:\Projects\Website\Images\Products

Set fso = Nothing

The script builds a path to "C:\Projects\Website\Images\Products". Each BuildPath call extends the path further. This approach is cleaner than manual string concatenation with backslashes.

Combining Drive Root with Path

This example demonstrates combining a drive root with subsequent path components. It shows BuildPath working with the minimal base path of just a drive letter. The method correctly handles the drive root case.

drive_root.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
drivePath = fso.BuildPath("D:", "DataFiles")
fullPath = fso.BuildPath(drivePath, "Archive")

WScript.Echo fullPath ' Output: D:\DataFiles\Archive

Set fso = Nothing

The script starts with just "D:" as the base path. BuildPath correctly converts this to "D:\" when combining. The result is a properly formatted full path with all necessary separators included.

Creating File Paths from Components

BuildPath can combine directory paths with filenames to create complete file paths. This example shows constructing a file path from separate directory and filename components. It's useful for file operations.

file_path.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
dirPath = "C:\Users\Public\Documents"
fileName = "report.docx"
filePath = fso.BuildPath(dirPath, fileName)

WScript.Echo filePath ' Output: C:\Users\Public\Documents\report.docx

Set fso = Nothing

The directory path and filename are combined into a complete file path. This approach is safer than manual concatenation. BuildPath ensures proper separator usage regardless of the directory path's ending.

Source

FileSystemObject Documentation

In this article, we have explored the BuildPath method in VBScript, covering its usage and practical applications. From simple path combinations to complex nested structures, these examples demonstrate reliable path construction. With this knowledge, you can enhance your file handling scripts with robust path management.

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.