Jump to content

My Documents folder through variable


Recommended Posts

Yeah, I saw the DeleteOnCopy section, and I figured that would be screwing it up as well :P.

My current batch 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
   REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /f /v Personal /t REG_EXPAND_SZ /d ^"%%~dpI
   REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /f /v Personal /d ^"%%~dpI
 )
 popd
)
)

EXIT

and that outputs this (on my computer):

I:\My Documents\

Thanks for all your help :)

Link to comment
Share on other sites


First of all, you do not need to add anything to the shell folders key, only the user shell folders key. The shell folders key is updated from the value in the user shell folders key. Also as your location does not contain any variables, there is no benefit in setting your data as REG_EXPAND, because there is nothing to expand. Another thing you can do is to get rid of the echoing to the screen, they are not needed in an unattended install. The following example will show you what you should have at present

@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 (
   pushd "%%D:\"
   FOR /F "USEBACKQ DELIMS==" %%I IN (`DIR /S /B %TAGFILE% ^2^>nul`) DO (
     REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%%~dpI" /f >nul
   )
   popd
 )
)

EXIT

Your only problem now is to get rid of the single trailing backslash, in the %%~dpI variable.

Link to comment
Share on other sites

The code seems to work fine.

Although there is one thing that needs to be changed to have the key being imported correctly:

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

As you can see, the " /f at the end are gone, and I've added a ^. This is because for some reason, if you have this:

"%%~dpI" /f

the " /f are also imported as part of the registry entry.

As you said, the only problem now is to remove the trailing backslash...

The only question is: how?

Link to comment
Share on other sites

No it doesn't , the only thing required is the trailing backslash full stop!

The reason you have been using carets to escape is because the trailing backslash is acting as an escape character for the final quotation mark.

you can get rid of the backslash using variable expansion and substitution, because you already know the tagfile name you could use something like the following

@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 (
   pushd "%%d:\"
   for /f "usebackq delims==" %%i in (`dir /s /b %tagfile% ^2^>nul`) do (
     set docsfolder=%%i
   )
   popd
 )
)
set docsfolder=%docsfolder:\my.docs=%
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%docsfolder%" /f >nul
goto :eof

My example is setting docsfolder to %%i

  • I:\My Documents\my.docs

then later expands it and substitutes

  • \my.docs

with nothing, hence leaving

  • I:\My Documents

Link to comment
Share on other sites

goto :eof is acting in a similar manner to exit!

Another, neater way of doing it would be to remove the last character using a substitution method similar to before

@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 (
   pushd "%%d:\"
   for /f "usebackq delims==" %%i in (`dir /s /b %tagfile% ^2^>nul`) do (
     set docsfolder=%%~dpi
   )
   popd
 )
)
set mydocs=%docsfolder:~0,-1%
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%mydocs%" /f >nul
goto :eof

Link to comment
Share on other sites

Ok, ignore my above post...

I modified the file for another folder, but forgot to change the last "my.docs" to "my.vids"...

So that was causing the error :whistle:

Thanks for your help, going to make an ISO now with the adapted files and I'll report back on the results :thumbup

Link to comment
Share on other sites

If your happy with it then that's great.

Just one warning though, make sure that you use the correct batch with the appropriate installation. Because there in no error trap in the code, if no tagfile is found, you will have something like zp added to the User Shell Folders data.

<Edit>

One easy way to get around this possibility is to create a single batch using

  • set tagfile=%UserName%.docs

Then you can create a tagfile with each specific User Name in the root of their My Documents folders.

  • i.e. Paul.docs, Jon.docs, general user.docs, Administrator.docs etc.

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

That shouldn't be too much of a problem, I think.

I can always change it later might sth go wrong :P.

And my "instruction-set" for a re-installation will contain a "create my.docs" file ;).

Link to comment
Share on other sites

  • 2 weeks later...

This is kind of a related question to the thread that has been posted, but I haven't been able to find any info on this...

I want to stop redirection on the subfolders under My Documents. I know by default that they redirect when you do it to My Documents, and the GPO only gives you the ability to stop redirection on My Pictures. For us, My Music is a bigger problem than My Pictures. For many media based applications, My Music is where they put their files. Some will even cause the folder to be re-created if you delete it. We allow our users to bring in their own musoc to listen to, and by ripping it to their hard drives they do not have to cart along a lot of cds. I just don't want to store, or back up, the files on my file server.

I saw a Microsoft article about preventing subfolders under My Documents from being redirected a couple of months ago, but now I can't find it. We have over 3500 users in our organization, so manual cleanups are not realistic.

Until our quota software is implemented, where I can restrict specific file types from being saved, I need to be able to stop them from being placed on the file server because of space considerations, while still providing the service of redirecting their documents and other work that sits in My Documents.

Any help on this would be appreciated... wacko.gif

Link to comment
Share on other sites

You could try the redirection directly in the registry, here is an example in inf format

[Version]
Signature = $CHICAGO$

[DefaultInstall]
AddReg    = Reg.Settings

[Reg.Settings]
HKCU,"Software\%MWCv%\%EUsf%","My Music",0x20000,"%MUSIC%"
HKCU,"Software\%MWCv%\%EUsf%","My Pictures",0x20000,"%PICS%"
HKCU,"Software\%MWCv%\%EUsf%",Personal,0x20000,"%MYDOC%"

[Strings]
EUsf      = "Explorer\User Shell Folders"
MWCv      = "Microsoft\Windows\CurrentVersion"

;Editable Strings
MUSIC     = "E:\%USERNAME%\My Music"
MYDOC     = "%USERPROFILE%\My Documents"
PICS      = "E:\%USERNAME%\My Pictures"

This example shows the My Documents in its default location with the My Music and My Pictures folders redirected. Just edit the strings appropriately.

In cmd script you could use

@echo off
set USF="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
set MUSIC="E:\%%USERNAME%%\My Music"
set MYDOC="%%USERPROFILE%%\My Documents"
set PICS="E:\%%USERNAME%%\My Pictures"
reg add %USF% /v "My Music" /t REG_EXPAND_SZ /d %MUSIC% /f
reg add %USF% /v "My Pictures" /t REG_EXPAND_SZ /d %PICS% /f
reg add %USF% /v Personal /t REG_EXPAND_SZ /d %MYDOC% /f
goto :eof

Once again just edit the strings appropriately.

<Edit>

Just be careful with the %% sometimes it can be a little playful, whereby ^% may provide an alternative solution

</Edit>

Edited by Yzöwl
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...