4

I want to start with a folder e.g. C:\test
instead of any of the predefined CSIDL_* folders. How can I achieve that?

    ''' python 3.6.2 '''

    import os
    import win32gui
    from win32com.shell import shell, shellcon

    myfolder_pidl = shell.SHGetFolderLocation (0, shellcon.CSIDL_DESKTOP, 0, 0)

    pidl, display_name, image_list = shell.SHBrowseForFolder (
      win32gui.GetDesktopWindow (),
      myfolder_pidl,
      "Select a file or folder",
      0x00014050, #shellcon.BIF_BROWSEINCLUDEFILES and some more
      None,
      None
    )

    if (pidl, display_name, image_list) == (None, None, None):
        print ('Nothing selected')
    else:
        my_path = shell.SHGetPathFromIDList (pidl)

    file_to_process = my_path.decode()

    ''' continue processing file_to_process
    '''
AcK
  • 2,063
  • 2
  • 20
  • 27
  • 2
    I wonder why this has been downvoted ... (after 34 months) – AcK Apr 28 '21 at 13:07
  • 2
    Agreed! The [documentation](http://timgolden.me.uk/pywin32-docs/shell__SHBrowseForFolder_meth.html) is extremely unintuitive, the [provided example](https://github.com/mhammond/pywin32/blob/master/com/win32comext/shell/demos/browse_for_folder.py) doesn't show how to do this, and there are no other obvious Google results for this. You have my +1. – Alex Peters Jun 02 '21 at 13:24
  • 2
    @Alex thx ... Jeff's answer was really helpful ... – AcK Jun 03 '21 at 12:01

1 Answers1

3

You need to get a PIDL for the desired folder.

myfolder_pidl = shell.SHParseDisplayName(u"C:\test", 0)[0]

then pass that into SHBrowseForFolder

Jeff R.
  • 1,493
  • 1
  • 10
  • 14