Author Topic: Determine Folder Redirection  (Read 5619 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Determine Folder Redirection
« on: February 01, 2012, 08:18:06 AM »
This thread follows an issue reported by member CmdrDuh in my recent Batch Attribute Editor thread. The problem arises from folder redirection in Windows7 (and possibly other OS's).


Introduction:

Using the 'My Documents' folder as an example:

After using vl-directory-files to list directories under the User directory ("C:\\Users\\<User>") in Windows7, there is the 'Documents' folder and the 'My Documents' folder.

However, the 'My Documents' folder is redirected to the 'Documents' folder (when viewed in Explorer).

And so, although:

Code: [Select]
C:\\Users\\<User>\\My Documents
is a valid filepath (both findfile / vl-file-directory-p return non-nil), it apparently contains no files (returned by vl-directory-files), since its contents are redirected to the following directory:

Code: [Select]
C:\\Users\\<User>\\Documents
('My Documents' was used as an example, but this also applies to Music / Pictures etc, and I believe a redirection can be applied to any folder).


What I have tried:

I can get a list of the Documents / Music / Pictures directories from:

Code: [Select]
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
in the registry. But this does not tell me which folders are being redirected to these directories.


The Question:

Is there a way to retrieve a list of which directories are being redirected and to which location?  Or perhaps someone can suggest an alternative way around my problem?



Thanks for any advice, apologies if this was unclear or used incorrect terminology.

Lee
« Last Edit: February 01, 2012, 08:56:10 AM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10649
Re: Determine Folder Redirection
« Reply #1 on: February 01, 2012, 09:30:06 AM »

Recomendation:
Use the KNOWNFOLDERID system
http://msdn.microsoft.com/en-us/library/dd378457(v=vs.85).aspx

How to use:
http://msdn.microsoft.com/en-us/library/bb756896.aspx



:) I'm sorry. I just had to poke a little fun at your use of ?every? post-format-option available. :) Good luck.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10649
Re: Determine Folder Redirection
« Reply #2 on: February 01, 2012, 10:09:55 AM »
I thought I should mention that I have never used the above system, I only know OF it so, please take the above with a grain of salt.


***
Off topic: Not that I'm on a soap box or even ranting with this next part. I am just talking for talking sake because for once in ~6 months+ I am actually thinking about *this* and not the *other stuff* (I'm having a lucid moment).

One of the major arguments for using LaTex is to allow people (both the typest and the reader) to focus on the content and not the format. Ive seen and read about this argument/topic in more than one area; The use of HTML emails, for example, is an area that hits closer to home I suppose. Bold faced red headdings in the architect's email does nothing but distract my attention from the sentence/paragragraph I'm currently reading (this sentence isn't important because the next part of this email is red...I should read that part first...okay, now I'm confused...now I have to go back and read the first part).

Okay, I'm done rambling about rambling now. Thanks for letting me type to "just type".
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #3 on: February 01, 2012, 10:14:08 AM »
Use the KNOWNFOLDERID system
http://msdn.microsoft.com/en-us/library/dd378457(v=vs.85).aspx

How to use:
http://msdn.microsoft.com/en-us/library/bb756896.aspx

Thanks for the recommendation Se7en, I shall have a read and see what I can come up with.

:) I'm sorry. I just had to poke a little fun at your use of ?every? post-format-option available. :) Good luck.

Just trying to include some structure in my question. :-)

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #4 on: February 01, 2012, 11:29:23 AM »
I've had a read of the KNOWNFOLDERID System and have located the relevant keys in the registry, but from the MSDN article on how to use the system, it doesn't look like it would be accessible from LISP, so I may have to follow a different route.

A simpler idea came to mind:

Code - Auto/Visual Lisp: [Select]
  1. (defun _checkredirect ( folder / item pos )
  2.     (if
  3.         (and
  4.             (eq
  5.                 (strcase (getenv "UserProfile"))
  6.                 (strcase (substr folder 1 (setq pos (vl-string-position 92 folder nil t))))
  7.             )
  8.             (setq item
  9.                 (cdr
  10.                     (assoc (substr (strcase folder t) (+ pos 2))
  11.                        '(
  12.                             ("my documents" . "Documents")
  13.                             ("my pictures"  . "Pictures")
  14.                             ("my videos"    . "Videos")
  15.                             ("my music"     . "Music")
  16.                         )
  17.                     )
  18.                 )
  19.             )
  20.         )
  21.         (strcat (substr folder 1 pos) "\\" item)
  22.         folder
  23.     )
  24. )

But I doubt this would cover all cases.  :|

JohnK

  • Administrator
  • Seagull
  • Posts: 10649
Re: Determine Folder Redirection
« Reply #5 on: February 01, 2012, 11:50:47 AM »
The registry is/may be your problem; 32 and 64 bit applications have different access'. I think its called "Registry redirector" or "registry reflection" (one of those two). You will have to do the leg work on that topic.  The point is, your use of the registry is why I suggested the KNOWNFOLDERSID thing/service/api instead.

That said:
From the second link I provided above, I believe you need to access the shell32.dll...You can find the constants from the first link above.

Here is some code from HighflyingBird to access the shell app (I searched the forum for code already written).
http://www.theswamp.org/index.php?topic=38102.msg431144#msg431144

HTH
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Determine Folder Redirection
« Reply #6 on: February 01, 2012, 01:09:09 PM »
I can't replicate the vl-directory-files problem  :?

Works:
(vl-directory-files (strcat (getenv "userprofile") "\\documents"))

Does not work when manually entered but does work when browsed to:
(vl-directory-files (strcat (getenv "userprofile") "\\my documents"))

If I'm way off please ignore  :lmao:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

BlackBox

  • King Gator
  • Posts: 3770
Re: Determine Folder Redirection
« Reply #7 on: February 01, 2012, 01:16:31 PM »
Why not just write a (small?) .NET LispFunction Method to check if the folder is being redirected, and return the redirect path (if True), or return Nil (if False)?

I've never worked with KnownFolderID Constants, but I believe I've done something similar for a small System.IO.File Class task (GetOwner), to enhance LISP.

Just a thought.
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #8 on: February 01, 2012, 01:28:41 PM »
I can't replicate the vl-directory-files problem  :?

Works:
(vl-directory-files (strcat (getenv "userprofile") "\\documents"))

Does not work when manually entered but does work when browsed to:
(vl-directory-files (strcat (getenv "userprofile") "\\my documents"))

You're not way off Ron  :-)

In my application (linked to in the first post), when the user double-clicks on a folder in the directory listing (left-hand panel), the folder name is appended to the end of the directory string and vl-directory-files then operates on this new directory. Hence, when double-clicking on 'My Documents', vl-directory-files returns nil.

When using the BrowseForFolder method (called by the 'Browse' button), if the user selects 'My Documents', BrowseForFolder correctly returns the redirected 'Documents' folder, so the listing is correct.

I need to emulate this for when the user double-clicks on the 'My Documents' folder, hence I need to know when to redirect and what folder to redirect to.

Hope this is clear  :-)

Why not just write a (small?) .NET LispFunction Method to check if the folder is being redirected, and return the redirect path (if True), or return Nil (if False)?

Many thanks for your time and the suggestion Renderman, but since this is for a LISP application, I'd rather keep this purely LISP if possible - especially to fix this relatively small issue with the file browsing side of the program.

Thanks anyway!

BlackBox

  • King Gator
  • Posts: 3770
Re: Determine Folder Redirection
« Reply #9 on: February 01, 2012, 01:30:46 PM »
No worries, mate! :beer:
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Determine Folder Redirection
« Reply #10 on: February 01, 2012, 01:44:59 PM »
Perhaps this will be of use:

Code - Auto/Visual Lisp: [Select]
  1. (vl-registry-read "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\" "personal")

Source linky (top of last post)
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Determine Folder Redirection
« Reply #11 on: February 01, 2012, 01:48:20 PM »
Another option:

Code - Auto/Visual Lisp: [Select]
  1. (vl-registry-read "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\" "personal")

Not really sure which is more reliable?
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #12 on: February 01, 2012, 01:52:17 PM »
Thanks Renderman, I note that location in the first post, but didn't know how to determine which folders are redirected to those locations (but then maybe it is just the 'My Documents' / 'My Music' / etc...) I may just settle for the solution I posted in #4

BlackBox

  • King Gator
  • Posts: 3770
Re: Determine Folder Redirection
« Reply #13 on: February 01, 2012, 01:59:31 PM »
Thanks Renderman, I note that location in the first post...

Dually noted. :oops:  :lol:
"How we think determines what we do, and what we do determines what we get."

demesne

  • Guest
Re: Determine Folder Redirection
« Reply #14 on: February 02, 2012, 05:54:53 PM »
Having very little knowledge of programming (other than Casio calculators) and not really having ever encountered the problem, is this something that could be overcome by using the DOSLib functions?

http://www.en.na.mcneel.com/doslib.htm

It's just an idea from someone who feels he has to repay Lee Mac for his help.  :wink:

Demesne

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #15 on: February 02, 2012, 06:01:15 PM »
Thanks for the suggestion Demesne; I'm sure DosLib has some kind of file browser - but since this is for use in an application that I am sharing as freeware, I'd rather not have the requirement that the user must download DosLib to use the program.

demesne

  • Guest
Re: Determine Folder Redirection
« Reply #16 on: February 02, 2012, 06:06:17 PM »
DOSLib is freeware too.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Determine Folder Redirection
« Reply #17 on: February 02, 2012, 06:13:36 PM »
DOSLib is freeware too.

I know, but its third-party - I'd rather keep my programs standard.  :-)

demesne

  • Guest
Re: Determine Folder Redirection
« Reply #18 on: February 02, 2012, 06:14:55 PM »
You just like a challange.   :wink: