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-29 11:12
中国DOS联盟论坛 » 论坛回收站 » [Help] Code Simplification View 1,742 Replies 0
Original Poster Posted 2010-01-28 23:21 ·  中国 山东 联通
新手上路
九影蓝翼
Credits 18
Posts 18
Joined 2009-11-27 07:46
16-year member
UID 155451
Gender Male
Status Offline
The code is as follows:
------------------------------------------------------
@echo off
set "v123=不存在" & set "v456=不存在" & set "v789=不存在" & set "vabc=不存在"
for /f "tokens=3" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" ^| find /i "123"') do if "%%i"=="0x1" set "v123=存在"
for /f "tokens=3" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" ^| find /i "456"') do if "%%i"=="0x1" set "v456=存在"
for /f "tokens=3" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" ^| find /i "789"') do if "%%i"=="0x1" set "v789=存在"
for /f "tokens=3" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" ^| find /i "abc"') do if "%%i"=="0x1" set "vabc=存在"
echo 123 key %v123%
echo 456 key %v456%
echo 789 key %v789%
echo abc key %vabc%
pause
------------------------------------------------------
To simplify it, especially in terms of the size and the number of for loops, you can do the following:
------------------------------------------------------
@echo off
set "keys=123 456 789 abc"
setlocal enabledelayedexpansion
for %%k in (%keys%) do (
set "v%%k=不存在"
for /f "tokens=3" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" ^| find /i "%%k"') do (
if "%%i"=="0x1" set "v%%k=存在"
)
echo %%k key !v%%k!
)
endlocal
pause
------------------------------------------------------
Forum Jump: