.SpecialFolders - VBScript


Overview

.SpecialFolders function in VB Script provides a way to access special folders on the local computer, such as the desktop, My Documents, or the Windows directory. It is particularly useful for manipulating files and paths across different systems.

Syntax

object.SpecialFolders(Folder)

Parameters:

  • object: The object that represents the special folder location.
  • Folder: The specific special folder to access. Use predefined constants (listed in Options/Flags below) to specify the folder.

Options/Flags

| Constant | Description | Default |
|—|—|—|
| MyDesktop | Desktop folder | N/A |
| MyDocuments | My Documents folder | N/A |
| StartMenuPrograms | Start Menu’s Programs folder | N/A |
| Startup | Startup folder | N/A |
| Recent | Recent Documents folder | N/A |
| SendTo | Send To folder | N/A |
| Templates | Templates folder | N/A |
| MyPictures | My Pictures folder | N/A |
| ApplicationData | Application Data folder | N/A |
| CommonAppData | Common Application Data folder | N/A |
| Windows | Windows folder | N/A |
| System | System folder | N/A |
| ProgramFiles | Program Files folder | N/A |
| CommonProgramFiles | Common Program Files folder | N/A |
| CommonProgramFilesX86 | Common Program Files (x86) folder | N/A |
| WindowsFolder | Windows folder | N/A |
| SystemFolder | System folder | N/A |
| ProgramFilesX86 | Program Files (x86) folder | N/A |
| TemporaryInternetFiles | Temporary Internet Files folder | N/A |
| LocalAppData | Local Application Data folder | N/A |
| AppData | Application Data folder | N/A |

Examples

Get the path to the Desktop folder:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
Dim strDesktopPath
strDesktopPath = objShell.SpecialFolders("MyDesktop")

WScript.Echo strDesktopPath

Create a file in the My Documents folder:

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile
Set objFile = objFSO.CreateTextFile(objShell.SpecialFolders("MyDocuments") & "\test.txt")

objFile.WriteLine "Hello, World!"
objFile.Close

Common Issues

  • Ensure you use the correct constant for the desired special folder.
  • Handle errors when accessing special folders, as they may not be available or accessible depending on the system.

Integration

.SpecialFolders can be combined with other VB Script commands, such as CreateTextFile or FileExists, to automate file and folder management tasks.

  • FileSystemObject
  • GetObject
  • CreateObject