批处理 for中%%i的赋值问题

2024-12-01 20:12:49
推荐回答(2个)
回答1:

变量延迟
@echo off& setlocal enabledelayedexpansion
. . . .
do (set m=%%i
echo !m!)

或者m赋值后结束for循环,
之后的代码放饭括号外执行,这样不必开延迟

回答2:

@echo off
setlocal enabledelayedexpansion
for /F "tokens=1-3" %%i in ('reg query "HKLM\Software\Microsoft\MediaPlayer" /v "IEInstall"') do (
if "%%i"=="IEInstall" (
echo "%%i %%j %%k"
set m=%%i
echo "!m!"
echo "!m:~0,1!"
set v=!m:~0,1!
echo "!v!"
)
)