Jump to content

Retrieving Special Folder Paths


_Kurt
 Share

Recommended Posts

Hello,

I find this function very useful to retrieve the path of a special folder, kind of like the macros (i.e. @ProgramFilesDir). I've translated a script from MSDN and declared most of the constants. It also has several other functions, see below for details.

_FileFindSpecialFolder($Folder,[ $ListToArray = 0[, $Filter = 0]])

Examples:

#include <Array.au3> ;For _ArrayDisplay

$ListFiles = _FileFindSpecialFolder($MY_COMPUTER, 1, 0)
_ArrayDisplay($ListFiles)
;Used the 1 parameter to report files within My Computer
;Used the 0 parameter to filter NO files (default)

$ListFiles = _FileFindSpecialFolder($MY_DOCUMENTS, 1, 2)
_ArrayDisplay($ListFiles)
;Used the 1 parameter to report files within My Documents
;Used the 2 parameter to create an array containing all the folder paths

MsgBox(0,"",_FileFindSpecialFolder($START_MENU))
;Reports the CLASSID corresponding to the Start Menu special folder

MsgBox(0,"",_FileFindSpecialFolder($MY_COMPUTER))
;Reports the path corresponding to the My Computer special folderoÝ÷ Ù.ëmèy¨^­íý²Ø^~éܶ*'jëh×6Global Const $INTERNET_EXPLORER = 0x1
Global Const $NETHOOD = 0x13
Global Const $ADMINISTRATIVE_TOOLS = 0x2f
Global Const $ALL_USERS_APPLICATION_DATA = 0x23
Global Const $ALL_USERS_DESKTOP = 0x19
Global Const $ALL_USERS_PROGRAMS = 0x17
Global Const $ALL_USERS_START_MENU = 0x16
Global Const $ALL_USERS_STARTUP = 0x18
Global Const $APPLICATION_DATA = 0x1A
Global Const $COMMON_FILES = 0x2B
Global Const $CONTROL_PANEL = 0x3
Global Const $DESKTOP = 0x10
Global Const $FONTS = 0x14
Global Const $COOKIES = 0x21
Global Const $FAVORITES = 0x6
Global Const $LOCAL_APPLICATION_DATA = 0x1C
Global Const $MY_COMPUTER = 0x11
Global Const $MY_DOCUMENTS = 0x5
Global Const $MY_MUSIC = 0xD
Global Const $MY_NETWORK_PLACES = 0x12
Global Const $MY_VIDEOS = 0xE
Global Const $NETWORK_CONNECTIONS = 0x31
Global Const $PRINTHOOD = 0x1B
Global Const $PRINTERS_AND_FAXES = 0x4
Global Const $PROGRAM_FILES = 0x26
Global Const $PROGRAMS = 0x2
Global Const $RECYCLE_BIN = 0xA
Global Const $SENDTO = 0x9
Global Const $START_MENU = 0xB
Global Const $STARTUP = 0x7
Global Const $SYSTEM32 = 0x25
Global Const $TEMPLATES = 0x15
Global Const $USER_PROFILE = 0x28
Global Const $WINDOWS = 0x24
Global Const $LOCAL_SETTINGS_HISTORY = 0x22
Global Const $MY_PICTURES = 0x27
Global Const $MY_RECENT_DOCUMENTS = 0x8
Global Const $TEMPORARY_INTERNET_FILES = 0x20
Global Const $CD_BURNING_FOLDER = 0x003B

Global $oShell = ObjCreate("Shell.Application")

;===============================================================================
;
; Function Name:    _FileFindSpecialFolder($Folder,[ $ListToArray = 0[, $Filter = 0]])
; Description:      
; Parameter(s):     $Folder      - The Special folder constant (i.e. $MY_COMPUTER)
;                                  You could also use an existing path (i.e. "C:\Program Files")
;                                  Or a ClassID (i.e. ::{20D04FE0-3AEA-1069-A2D8-08002B30309D} )
;                   $ListToArray - (optional) Change the return value:
;                                             0 = Return the ClassID or Path to the special folder
;                                             1 = Return an array containing the files within the folder
;                   $Filter      - (optional) Filter out the array by:
;                                       1 = Files
;                                       2 = Folders
;                                       3 = Links
;                                       4 = ClassId's
;                                       0 = No Filter (default)
; Requirement(s):   None.
; Return Value(s):  Returns either:
;                       1) A string containing the CLASSID or Path corresponding to the Special folder
;                       2) An Array containing the list of files/folder/links within the special folder
;                   Returns -1 if there are no results
; Author(s):        _Kurt
; Note(s):          See http://www.microsoft.com/technet/scriptcenter/scripts/desktop/special/default.mspx?mfr=true for more info
;
;===============================================================================

Func _FileFindSpecialFolder($Folder, $ListToArray = 0, $Filter = 0)
    $nPath = $oShell.NameSpace($Folder).Self.Path
    If StringLeft($nPath,1) = ":" OR StringLeft($nPath,1) = ";" Then
        SetError(2)
    Else
        SetError(1)
    EndIf
    If $nPath = "" Then $nPath = -1
    If $ListToArray = 0 Then Return $nPath
    $nNum = $oShell.NameSpace($Folder).Items.Count
    Local $List[$nNum], $a = 0
    For $i = 0 To $nNum-1
        Select
            Case $Filter = 0
                $List[$i] = $oShell.NameSpace($Folder).Items.Item($i).Path
                $a += 1
            Case $Filter = 1
                If $oShell.NameSpace($Folder).Items.Item($i).IsFileSystem = True Then
                    If NOT $oShell.NameSpace($Folder).Items.Item($i).IsFolder = True Then
                        $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                        $a += 1
                    EndIf
                EndIf
            Case $Filter = 2
                If $oShell.NameSpace($Folder).Items.Item($i).IsFolder = True Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
            Case $Filter = 3
                If $oShell.NameSpace($Folder).Items.Item($i).IsLink = True Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
            Case $Filter = 4
                $b = $oShell.NameSpace($Folder).Items.Item($i).Path
                If StringLeft($b, 1) = ":" OR StringLeft($b, 1) = ";" Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
        EndSelect
    Next
    If $a = 0 Then
        $List = -1
    Else
        ReDim $List[$a]
    EndIf
    Return $List
EndFunc

Will set @error to 1 if its a path file, and sets @error to 2 if its a classid.

For more information, visit:

http://www.microsoft.com/technet/scriptcen...t.mspx?mfr=true

or

http://msdn2.microsoft.com/en-us/library/aa969392.aspx

All Problems Solved :)

Enjoy,

Kurt

EDIT: Updated function:

1) Can now retrieve all files/folders/links within the special folder

2) (optional) Will filter out either files, folders, or links depending on parameter

3) Added $CD_BURNING_FOLDER constant, thanks GEOSoft

Edited by _Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

You might be better off to use the file system object for this script.

The VBS code is in this file under storage.

VBS Code

Edit: Also if you search this forum there is also another list of the special folder constants that contains items like the Burn folder. I THINK it was by SmokeN but I could be wrong about that.

OK I was wrong. It was by ptrex. here's the thread

Special folder constants

Also you can enumerate the registry at

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

for the paths

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Updated, see first post

You might be better off to use the file system object for this script.

The VBS code is in this file under storage.

VBS Code

Edit: Also if you search this forum there is also another list of the special folder constants that contains items like the Burn folder. I THINK it was by SmokeN but I could be wrong about that.

OK I was wrong. It was by ptrex. here's the thread

Special folder constants

Also you can enumerate the registry at

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

for the paths

Thanks for adding the $CD_BURNING_FOLDER. And using the registry could be an alternate method, however it doesn't contain the ClassIDs and I've added a couple of other uses to the function, check it out.

Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

How might I be better off using the file system object?

I said that BEFORE you solved your problems. But as a rule, when working with files or folders the File System object will be easier to work with. Just take a look at the examples in that file I linked to.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 1 year later...

great script buddy, this covers a part of what i have been doing (1. go to a location based on CSLID info, which i have already, and 2. if and when needed, create a listing of the contents of the CSLID folder path.- covered in your script )

i used the shell.application to access the CSLID for testing purposes, this carries it on a bit in required direction..

i am posting the include file in examples, check it out, those constants can be used with your function to retrieve the listing

hmm just noticed, Wraithdu has posted someinformation elsewhere which is wonderful exactly what i have been doing (of course, i was to use dllcall as next step. initially i was gonna use Shell.application now i am trying to integrate both...

Edited by rajeshontheweb
Link to comment
Share on other sites

yeah, but i have plans to post your function as a proper UDF, pls dont mistake me, i am gonnna post it as i am using it (theres not muchmodification to your script, u r the author! :-) but i have just put it to use the way i would like it and it might be useful for any other noobs to come...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...