Jump to content

My Documents folder through variable


Recommended Posts

The batch works good and it returns the variable I need.

The only problem now is that I need to make it into a system variable.

I already checked out this where there is an explanation of how to make the %CDROM%-variable into a system one.

I thought to be smart and implement that same code into my batch, so now it looks like this:

@echo off

set TAGFILE=my.docs

FOR %%D IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
IF EXIST %%D:\NUL (
 echo Checking %%D:
 pushd "%%D:\"
 FOR /F "USEBACKQ DELIMS==" %%I IN (`DIR /S /B %TAGFILE%`) DO (
   echo Tagfile: %%I
   SET DOCSFOLDER=%%~dI%%~pI
 )
 popd
)
)

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v "DOCSFOLDER" /t REG_EXPAND_SZ /d "%DOCSFOLDER%"

EXIT

After that, it imports a .reg file, which looks like this:

Windows Registry Editor Version 5.00

;--- My Documents Folder ---
; --- Sets the My Documents Folder to the variable set in set_docs.cmd ---
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"Personal"="%DOCSFOLDER%"

The problem now is, is that my code for the system-variable returns the location with a " behind it, which screws up the .reg file and the location.

I've also tried it using "^%DOCSFOLDER%^", but then it gives a "Too many parameters" error.

Does someone have an idea how I can resolve this?

Thanks in advance.

Link to comment
Share on other sites


If you use an INF, you can set it by making DOCSFOLDER equal %16389%, which is the same as %userprofile%\My Documents.

:blink: Ehm... INF files... No clue how to do that. Also seems a bit "overkill" to me. Are they easy to make? How would I implement them?

Is it not possible using the batch? It's just one " that's screws it up...

Any ideas?

Thanks again! :thumbup

Link to comment
Share on other sites

I don't really understand why you need the Environment Variable, or for that matter a reg file, why not just get rid of

SET DOCSFOLDER=%%~dI%%~pI

and replace it with

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dpI" /f

Link to comment
Share on other sites

I don't really understand why you need the Environment Variable, or for that matter a reg file, why not just get rid of
SET DOCSFOLDER=%%~dI%%~pI

and replace it with

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dpI" /f

LOL, never though of that...

I'm not that savvy with batch-files, so please forgive me :whistle:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dpI" /f

Shouldn't that be

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dI%%~pI" /f

though?

Link to comment
Share on other sites

I just tested, still have the problem with the quotes at the end of the location in the registry...

user_shell_folders.jpg

I have used this code:

@echo off

set TAGFILE=my.docs

FOR %%D IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
IF EXIST %%D:\NUL (
 echo Checking %%D:
 pushd "%%D:\"
 FOR /F "USEBACKQ DELIMS==" %%I IN (`DIR /S /B %TAGFILE%`) DO (
   echo Tagfile: %%I
   REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dpI" /f
 )
 popd
)
)

EXIT

For the other locations, I use a different batch file, with a different tagfile.

Could anyone explain to me why the quotes are still there? :wacko:

Link to comment
Share on other sites

By pure luck I found that this works:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /f /v Personal /d ^"%%~dpI

The only thing that it now seems to do that instead of saying "My Music" and "My Pictures" it says "Ben's Music" and "Ben's Pictures" (so: %USERNAME%'s Music).

I'm currently testing to see if it works normally on a "clean" install.

Link to comment
Share on other sites

On a clean install I also get "%USERNAME%'s Music" or "%USERNAME%'s Pictures", it also happen to "My Documents" btw...

I think it's a problem with the desktop.ini still being there when the "new" owner is set, so it thinks there's a different user looking at it...

Is there a way to resolve this?

(yes, I'm almost done nagging :P)

Once more, Thanks in advance :)

Link to comment
Share on other sites

I've done a few more tests, but I really have no clue why the folders show up as "%USERNAME%'s Documents" etc...

Does someone know how this can be resolved? I think it has to do sth with the desktop.ini files, which specify an owner for the documents, but I'm not sure whether that's true...

It could also be that it has sth to do with the UID that Windows adds to the folders? But then I wonder why I don't have the problem currently?

Anyone has any idea???

EDIT: It seems to be the desktop.ini files that names the Personal folders.

Is there a program or sth which can delete the

Owner=<typenamehere>

line?

Or is that possible through a batch as well?

Thanks in advance,

Greetz,

Ben

Link to comment
Share on other sites

Yes it can be done through a batch, you would of course need to change the attributes of the file to not hidden first and return it to hidden later.

I thought that the folder was named My Documents only to the currently logged in owner.

In the mean time, here is a test version using the technique I mentioned near the start of the thread, here. It will not write anything to the registry. All it should do, if it works, is to show you the line it would add to the registry on the screen. It can just be run now, not in an unattended setup.

like I said its only a test, therefore if anyone with a redirected My Documents folder want to try it, please feel free and let me know if it worked!

tryit.rar

Link to comment
Share on other sites

What is the tryit.exe looking for? I only get to see "Press any key to continue" and then it stops.

I think I have found what the problem is with my current method though.

Apparantly, the folders need to be set in the registry with double backslashes in the directory name and without the trailing backslashes.

I have already solved the problem of the double backslashes in the path itself, but I don't know how to get the trailing ones out...

To clarify, this is what I get now:

I:\\My Documents\\

and it should be:

I:\\My Documents

Is it possible to "delete" the trailing backslashes somehow?

Because if I can delete them, I think the problem should be solved, and I can then post a guide how to do it :). (With proper references of course ;)).

So in short, I think that the desktop.ini problem is obsolete, since I even already tried to replace it with one I put on my DVD :whistle:

Link to comment
Share on other sites

It is looking for a folder name ending with Documents, and checking for the current user name inside the desktop.ini file. It will therefore not work if the 'Documents' folder name has been changed, the logged in user name has changed or the desktop.ini file doesn't exist. Also because the desktop.ini file section containing your 'Owner' name is a special file, if you copy it, all that information is removed. It is in a section labelled [DeleteOnCopy], which as you understand removes it on copy.

On your other question, yes you can remove the trailing backslash in the batch. Post your existing working batch and I'll try to guide you. However, in reg.exe format, you do not need to escape a backslash with another, you only need a single one!

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
  • Recently Browsing   0 members

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