SoFunction
Updated on 2025-04-08

Summary of some methods for taking special paths under vbs

1. Use FSO's SpecialFolder constant

The function that FSO's SpecialFolder constant depends on is GetSpecialFolder(SpecialFolder). Unfortunately, there are only 3 SpecialFolder constants, 0-2, 0 correspond to WindowsFolder, which is your Windows folder. If your system is installed on a C drive, the path string "C:\Windows" is returned. 1 corresponds to SystemFolder, GetSpecialFolder(1) returns "C:\Windows\system32". 2 represents a temporary folder, so don’t I say too much, right?

The following is an example of taking the temporary folder path string:

Dim fso
Set fso = CreateObject("")           //Bind FSO object
   Dim tempfolder
   Const TemporaryFolder = 2
   Set tempfolder = (TemporaryFolder)
 tempfolder

You can save the above code as a vbs file and try opening it, and the path where your temporary folder is located pops up. This string is stored in the variable tempfolder, understand?

2. Use WshShell to get system environment variables

The object provides the environment. Returns the collection of objects of WshEnvironment. Let's look at an example first:

Set WshShell=("")           /Binding WSH object
Set WshSysEnv=("Process")
 ("SYSTEMROOT")

Save the above code as a vbs file and run it, does your system path pop up? "C:\windows" right?

Some people may ask, what if you want to take other paths? Depend on your system environment variables, the system environment variables are generally the following:

Name Description
The number of processors running on the NUMBER_OF_PROCESSORS computer.
PROCESSOR_ARCHITECTURE The type of processor used by the user's workstation.
PROCESSOR_IDENTIFIER The processor ID of the user workstation.
PROCESSOR_LEVEL The processor level of the user workstation.
PROCESSOR_REVISION processor version of user workstation.
The operating system used by the OS user workstation.
COMSPEC is used to run commands (usually) for the Command Prompt window.
HOMEDRIVE local main drive (usually a C drive).
The default path for the HOMEPATH user (users\default on WindowsNT).
PATH path environment variable.
The extension of the PATHEXT executable (usually .com, .exe, .bat, or .cmd).
PROMPT command prompt (usually $P$G).
The local drive on which the SYSTEMDRIVE system resides (for example, c:\).
SYSTEMROOT system directory (for example, c:\winnt). Same as WINDIR.
WINDIR system directory (for example, c:\winnt). Same as SYSTEMROOT.
TEMP directory where temporary files are stored (for example, c:\temp). User can change.
The directory where TMP stores temporary files (for example, c:\temp). User can change.

You can replace the SYSTEMROOT in ("SYSTEMROOT") by yourself with the variable above.

3. Use the SpecialFolders property of WshShell

The SpecialFolders property provides a WshSpecialFolders object to access Windows shell folders, such as desktop folders, Start Menu folders, and personal document folders.

The following biweilun gives an example:

Set WshShell=("")
 "Yourdesktopis"&("Desktop")

This is a string that pops up the path where your desktop folder is located, and of course you can also choose to save it with a string. So, what are the SpecialFolders attributes in total?

AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates

You can guess what path this is by looking at the above folder? If you think I'm not fully written, you can use the following script code to read it yourself:

Set WshShell=("")
'htp:///biweilun
'Listallspecialfolders
For Each strFolder In 
 strFolder
Next

You will find that all available SpecialFolders pop up and display them one by one.