Let's Talk About Special Folders! VbScript

17. VbScript | Special Folders

Today we are going to talk about special folders in VbScript. Special Folders tend to be hidden or require a specific username to get to them. The Special Folders Command in VbScript are pre-defined directory paths that can be useful when moving a file from one directory to another; or any task that requires you to use common directories on the computer.

Table of Contents

Special Folder Paths

To specify a special folder, you can use this base code:

Option Explicit
Dim obj
Set obj = createobject("wscript.shell")

obj.specialfolders("")

Within the parenthesis-quotation of obj.specialfolder, you can specify the paths of any of these special folders:

----List of Special Folders ----

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

***If you put a file in the AllUsersDesktop special folder, the file will show up for all the users on their desktops.

SpecialFolders are like preset paths or addresses to specific or “special” folders

For example, the Desktop special folder command gives you the preset path to the desktop folder.

Option Explicit
Dim obj
Set obj = createobject("wscript.shell")

msgbox obj.specialfolders("Desktop")

Output:

FilePath

Open Special Folders

Notice that if you want to open the folder on your desktop, in addition to the default code you need to tell the VbScript to add a dash at the end of “C:\Users\rock\Desktop”. Like so:

Option Explicit
Dim obj
Set obj = createobject("wscript.shell")

obj.run obj.specialfolders("Desktop")& "\workshop"

=

Option Explicit
Dim obj, path
Set obj = createobject("wscript.shell")
path = obj.specialfolders("Desktop")& "\workshop"
obj.run path

Output:

VbScript SpecialFolder open folder
The VbScript opens the Workshop folder located on the Desktop

Copying a file to a Special Folder

To Copy a file to the startup folder:

Option Explicit
Dim fso, obj

Set fso = CreateObject("Scripting.FileSystemObject")
Set obj = CreateObject("wscript.shell")

fso.CopyFile "C:\Users\rock\Desktop\BigEyeCat.jpg" , obj.SpecialFolders("Startup")& "\"

=

Option Explicit
Dim fso, obj, nDirS

Set fso = CreateObject("Scripting.FileSystemObject")
Set obj = CreateObject("wscript.shell")
nDirS = obj.SpecialFolders("Startup")& "\"

fso.CopyFile "C:\Users\rock\Desktop\BigEyeCat.jpg" , nDirS

=

Option Explicit
Dim fso, obj, nDirs, nDirD

Set fso = CreateObject("Scripting.FileSystemObject")
Set obj = CreateObject("wscript.shell")
nDirS = obj.SpecialFolders("Startup")
nDirD =obj.SpecialFolders("Desktop")

fso.CopyFile nDirD& "\BigEyeCat.jpg" , nDirS & "\"

Output:

startup Folder
You’ll see that the BigEyeCat.jpg file is added to the Startup folder, resulting in that file being opened whenever the PC is started.

To summarize: instead of trying to find the full path or directory to a “special” Folder, you can use the Special Folder command to automatically specify the path.


What's Your Opinion?