Jump to content

MY Documents Variables


chrischambers

Recommended Posts

Hi

looking through some of the posting I found the problem to one of my problems were I need to find and set a DocFolder Variable for My Documents as my users has there My Docs in different places.

here is the code I found.

@echo off
set tagfile==%UserName%.txt
echo %UserName%.txt
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:\chris.txt=%
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /d "%docsfolder%" /f >nul

echo %docsfolder%
pause:
goto :eof

I have a little script that creates and place a txt file in each users my documents.

this was in readiness for the migrating of Users to a new Computer. so I need to do is copy there profile details and My documents and place on a backup server.

I am able to do find and backup the user’s desktop and Favourites from they profile, but when I use my New Variable %docsfolder% to locate and backup the users data. For some reason it is not finding the variable reference.

here is my backup code.

echo Off
REM - FIRST MAKE SURE THAT THE TAGFILE PROGRAM IS RAN.
REM -
REM - this Batch file is to backup and zip all the users files from the local
REM - machine and to place it onto a share folder on a server
REM -

rem ** Map to a share location

net use I: \\chris_laptop\mirgation

rem - Create a User Folder on the server and local machine
mkdir I:\users\%Username%
mkdir c:\archive

REM copy pkzipc to the local machine
REM copy I:\pkzipc.exe c:\

REM - Zip the Default folders from the user profile and place then into the archive folder

Echo Archiving %username% Profile

pkzipc -add -silent -path=specify c:\archive\profile.zip "c:\%homepath%\desktop\*.*"
pkzipc -add -silent -path=specify c:\archive\profile.zip "c:\%homepath%\favorites\*.*"

Echo Archiving %username% MyDocuments
REM pkzipc -silent -add -path=specify c:\archive\profile.zip "c:\%commydocdir%\My Documents\*.*"
REM pkzipc -silent -add -path=specify c:\archive\profile.zip "c:\%homepath%\My Documents\*.*"

echo My Documents location - %docsfolder%

pkzipc -silent -add -path=specify c:\archive\profile.zip "%docsfolder%\Convenandes\*.*"



REM - check for other application

IF not EXIST "C:\Lotus\Notes\ah7lena.dat" goto ENDNOTES
Echo Archving Lotus Notes
pkzipc -add -silent -path=specify c:\archive\notes.zip "c:\lotus\notes\*.*"
goto end
:ENDNOTES
Echo Not a Lotus Notes Users

REM Put more If statements here
:end




REM - Copy all the files from the C:\archive

echo Moving files to Archive Folder

xcopy c:\archive\*.* I:\users\%Username% /e/i/y/r/k

REM - BACKUP COMPLETE
echo Archving of all user information is complated
REM - clean up hard drive
rmdir c:\archive /s /q
pause

Can anyone see why this is not working. ?

Link to comment
Share on other sites


First thing I see is this:

set docsfolder=%docsfolder:\chris.txt=%

If you are after setting dodsfolder to say c:\chris.txt it should be

set docsfolder=%docsfolder%:\chris.txt

other wise it will stay being just the drive letter.

Second I believe the way you are setting the environment variable, by adding it to the registry requires a reboot before it is available in any applications or command windows other than the original window it was created in.

Link to comment
Share on other sites

If you want to set three variables for the Favorites, Desktop and My Documents locations, they can be taken directly from the registry:

Example.cmd

@Echo off&Setlocal
Set K_="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
For /f "tokens=1,2*" %%a In (
'Reg query %K_%^|Findstr/i "Desktop Personal Favorites"') Do (Set %%a="%%c")
Echo/%%Desktop%%=%Desktop%
Echo/%%Personal%%=%Personal%
Echo/%%Favorites%%=%Favorites%
Pause

Link to comment
Share on other sites

First thing I see is this:

set docsfolder=%docsfolder:\chris.txt=%

If you are after setting dodsfolder to say c:\chris.txt it should be

set docsfolder=%docsfolder%:\chris.txt

other wise it will stay being just the drive letter.

Second I believe the way you are setting the environment variable, by adding it to the registry requires a reboot before it is available in any applications or command windows other than the original window it was created in.

I think that you might have miss read the code, set docsfolder=%docsfolder%:\chris.txt - is setting the docsfolder to the path of the "My documents" as this folders has different locations

regarding setting up the Variables for Desktop and Fav, this I don't want to do, as this code is only backing up and zipping up the folders in readiness for transfer.

Link to comment
Share on other sites

I know you've already got code which finds the Favorites and Desktop directories and they very likely suit your system, however those locations may also be redirected in the same way as you state that some of the My Documents locations are. The code I provided was simply an example for others to locate all three locations at once.

Doing a full directory and subdirectory search for a particular text file under every drive on the PC is not the most efficient system for doing your task and the code you found is not very good at that either!

@Echo off
Setlocal
Set K_="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
For /f %%a in ('Mountvol^|Findstr [c-z]:\\') Do Call :Chkit %%a
Pause&Goto :Eof
:Chkit
For /f "delims=" %%a In (
'Dir/b/s/a-d %1%UserName%.txt 2^>Nul') Do Call :Addit %%~dpa
Goto :Eof
:Addit
Set P_=%*
Reg add %K_% /v Personal /d "%P_:~,-1%" /f>Nul

You backup code is using variables which have not been set or are set incorrectly.

  • %docsfolder% has not been set and therefore has no value
  • %HomePath% already has a (built-in) leading backslash meaning that you've doubled it up. It would also be better implemented with %HomeDrive%, (as %HOMEDRIVE%%HOMEPATH%), however the standard %USERPROFILE% should suffice.

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...