Welcome to MSDN Blogs Sign in | Join | Help

cd "My Documents"

John was complaining about this not working so I wrote MyDoc.bat:

@REM This will work if you did not redirect  “My Documents"

@%HOMEDRIVE%

@cd %HOMEPATH%\My Documents

Is there a better way?

Published Friday, April 07, 2006 3:19 PM by yvesdolc

Comments

# re: cd "My Documents"

Friday, April 07, 2006 6:30 PM by BlakeHandler
You should add a %HOMEDRIVE% command to insure that you're on the proper drive.

%HOMEDRIVE%
cd %HOMEPATH%\My Documents

# re: cd "My Documents"

Friday, April 07, 2006 6:55 PM by yvesdolc
Thanks but I already do it. BTW, on Vista, you would need to change this to:
%HOMEDRIVE%
@cd %HOMEPATH%\Documents

# re: cd "My Documents"

Friday, April 07, 2006 11:20 PM by jmanning
1) AFAIK you can use UserProfile instead of the separate HOMEDRIVE and HOMEPATH
2) you can change drive and path at the same time with cd /d

So:
@cd /d "%UserProfile%\My Documents"

Note that either of those approaches are actually busted - the user can redirect their "my documents" to basically any arbitrary path (many in my office point it to D:\My Documents) - the above is just the default location.

Now, that's likely written into HKCU somewhere, so if you wanted to support that, I'd imagine it's a reg.exe call, parse it out (perhaps with "for"), cache it into a env var (so we don't have to do the check each time :) and do the cd.

On a semi-related note, in Monad the $home variable is already set for you, and parsing the registry contents would, of course, be simpler.

# re: cd "My Documents"

Sunday, April 16, 2006 4:51 PM by Yves Dolce
Thanks Jim.
How would you use USERPROFILE?
If you do

D:\>cd %userprofile%

and

USERPROFILE=C:\Documents and Settings\jmanning

this won't work or am I missing your point?

On my machines, I do redirect My DOCUMENTS so this batch file is actually tailored but I shall follow your suggestion about using reg.exe.

Thanks again.

# re: cd "My Documents"

Saturday, June 10, 2006 9:02 AM by jmanning
The key point to notice is the quotes I added.

@cd /d "%UserProfile%\My Documents"

# re: cd "My Documents"

Saturday, June 10, 2006 2:40 PM by Yves Dolce
Thanks James.
Anonymous comments are disabled
 
Page view tracker