Search code examples
vbscriptpathspecial-folders

geting special folders with shell.application


I need in a script to return the path to the current user desktop. Now I know you can do it with WScript.

var WshShell = WScript.CreateObject("WScript.Shell");
         strDesktop = WshShell.SpecialFolders("Desktop");

But for my script this will not work as I cant use WScript. but I can use the shell.application object as below.

 dim objShell
        dim ssfWINDOWS
        dim objFolder

        ssfWINDOWS = 0
        set objShell = CreateObject("shell.application")
            set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
                if (not objFolder is nothing) then
                Set objFolderItem = objFolder.Self
                    g_objIE.Document.All("logdir").Value = objFolderItem.path
                end if
            set objFolder = nothing
        set objShell = nothing

what is the syntax so the rather than "BrowseForFolder" i can simple return the path of the current users desktop?

IE replace the line

set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)

with the equilivent of.

strDesktop = WshShell.SpecialFolders("Desktop");

Cheers

Aaron


Solution

  • You need to use Shell.Namespace(...).Self.Path:

    Const ssfDESKTOPDIRECTORY = &h10
    Set oShell = CreateObject("Shell.Application")
    strDesktop = oShell.NameSpace(ssfDESKTOPDIRECTORY).Self.Path
    
    WScript.Echo strDesktop
    


    But for my script this will not work as I cant use WScript.

    Do you mean you can't use WScript.CreateObject(...) because WScript is undefined? If so, you can simply use CreateObject("WScript.Shell").SpecialFolders("Desktop") instead. See What is the difference between CreateObject and Wscript.CreateObject?.