China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-21 05:43
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] [NT] A simple way to expand variables contained inside an expanded variable? View 706 Replies 4
Original Poster Posted 2008-01-05 13:55 ·  中国 广东 深圳 电信
初级用户
蝙蝠侠是写批处理的人
Credits 54
Posts 24
Joined 2008-01-04 20:34
18-year member
UID 107683
Gender Male
Status Offline
  When using BAT, I need to access the My Documents directory. I found that no system environment variable points there, and my My Documents has been customized so it is not at the system default location %USERPROFILE%\My Documents, so I read the value of Personal under HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders from the registry and assign it to a variable for use.

  This method works well on my machine, but on other machines where the My Documents location has not been customized, the value obtained from the registry is %USERPROFILE%\My Documents, and I want to expand it once more so it becomes a value without any unexpanded variables, such as C:\Documents And Settings\UserName\My Documents.

  My current solution is as follows. It feels a bit verbose, and I hope to see a more concise way to achieve the same effect. Also, if there is a simpler way to get the My Documents directory, please do share it. Thanks!

@ECHO OFF
FOR /F "tokens=2*" %%I IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" ^| FIND /i "Personal"') DO SET "Personal=%%J"

CALL :ExpandPathMacro "%Personal%" Personal

ECHO %Personal%
PAUSE
@ECHO ON
@GOTO :EOF

:: Expand path macro, parameter 1 is the string to be expanded, parameter 2 is the return variable name
:ExpandPathMacro
SETLOCAL ENABLEDELAYEDEXPANSION
SET "EPM=%~1"
SET "EPM=!EPM:%%=?!"
SETLOCAL ENABLEEXTENSIONS & ENDLOCAL & SET "EPM=%EPM:?=!%"
ENDLOCAL & SET "%2=%EPM%"
GOTO :EOF


[ Last edited by BatMan on 2008-1-5 at 02:15 PM ]
Floor 2 Posted 2008-01-05 16:50 ·  中国 广东 深圳 电信
初级用户
蝙蝠侠是写批处理的人
Credits 54
Posts 24
Joined 2008-01-04 20:34
18-year member
UID 107683
Gender Male
Status Offline
I played around with it a bit more myself and found a simpler method: modify the ExpandPathMacro function to:

:ExpandPathMacro
SET "%~2=%~1"
GOTO :EOF

It's already much simpler. Is there any way to do it directly at the same statement level without calling a function?
Floor 3 Posted 2008-01-05 21:13 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
You're taking too much of a detour.
There are two more direct methods.

One is to add a call in front of SET "Personal=%%J"
making it CALL SET "Personal=%%J"
Then all the other expansion code can be omitted.

The other is to read the value under Shell Folders
instead of under User Shell Folders.
Please note the difference between the values in the two.
The main differences are the type and whether expansion is involved.

Also, I found a problem with reg query under 03.
Not sure whether it's a bug.
When querying values under reg query user shell folders,
all values of type reg_expand_sz are displayed as reg_sz.
If a value contains expansion symbols such as %,
it reports “ERROR: More data is available.”
and then does not display those reg_expand_sz values containing expansion symbols.
Floor 4 Posted 2008-01-05 21:37 ·  中国 广东 深圳 电信
初级用户
蝙蝠侠是写批处理的人
Credits 54
Posts 24
Joined 2008-01-04 20:34
18-year member
UID 107683
Gender Male
Status Offline
Originally posted by qzwqzw at 2008-1-5 21:13:
You're taking too much of a detour.
There are two more direct methods.

One is to add a call in front of SET "Personal=%%J"
making it CALL SET "Personal=%%J"
Then all the other expansion code can be omitted.

The other is ...


Thanks, post #3! Learned something!

  Also, on the XP Sp2 machine at my company I have seen some values under User Shell Folders appear as REG_SZ and some as REG_EXPAND_SZ. I haven't looked into the cause or impact.
Floor 5 Posted 2008-01-05 21:44 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
It should be that some software or individuals modified them later on,
and did not follow Windows conventions when making the changes.
Forum Jump: