记得之前在论坛中看过一篇文章,说把Setlocal enabledelayedexpansion和 endlocal放在复合语句内部最正确,但是按这种方式操作,却接连出现问题。十分迷茫。
现在主要想弄清楚两个问题:
Setlocal enabledelayedexpansion和 endlocal,放在哪个位置得到的结果,“准确”且“效率高”?
(1)复合语句括号之外
(2)复合语句括号之内
-------------------------------
已知文件%tmp1%内容如下:- HKEY_CURRENT_USER\Temp Num\Temp1
- String 1
- String 2
- String 3
- String 4
- Stringabcdefghigklmnopqrstuvwsyz 1
- Stringabcdefghigklmnopqrstuvwsyz 2
- Stringabcdefghigklmnopqrstuvwsyz 3
- Stringabcdefghigklmnopqrstuvwsyz 4
- String abcdefghigklmnopqrstuvwsyz str1
- String abcdefghigklmnopqrstuvwsyz str2
- String abcdefghigklmnopqrstuvwsyz str3
- String abcdefghigklmnopqrstuvwsyz str4
- HKEY_CURRENT_USER\Temp Num\Temp2
- String 1
- String 2
- String 3
- String 4
- Stringabcdefghigklmnopqrstuvwsyz 1
- Stringabcdefghigklmnopqrstuvwsyz 2
- Stringabcdefghigklmnopqrstuvwsyz 3
- Stringabcdefghigklmnopqrstuvwsyz 4
- ……(后面类似,省略不写)……
复制代码 1、把Setlocal enabledelayedexpansion和 endlocal放在复合语句内部,代码和结果如下:- ===========代码:========
- set "tmp1=%temp%\t1.txt"
- for /f "delims=" %%a in ('type %tmp1%') do (
- Setlocal enabledelayedexpansion
- set "str=%%a"
- if /i "!str:~,5!"=="HKEY_" (set "sKey=%%a") else (set "sval=%%a")
- if defined sval (echo,!sKey!---!sval! &set "sval=")
- endlocal
- )
- pause&exit
-
- ======结果==========
- ---String 1
- ---String 2
- ---String 3
- ---String 4
- ---Stringabcdefghigklmnopqrstuvwsyz 1
- ---Stringabcdefghigklmnopqrstuvwsyz 2
- ---Stringabcdefghigklmnopqrstuvwsyz 3
- ---Stringabcdefghigklmnopqrstuvwsyz 4
- ---String abcdefghigklmnopqrstuvwsyz str1
- ---String abcdefghigklmnopqrstuvwsyz str2
- ---String abcdefghigklmnopqrstuvwsyz str3
- ---String abcdefghigklmnopqrstuvwsyz str4
- ---String 1
- ---String 2
-
- ====结论=====
- 部分变量并未得到延迟,结果为空
复制代码 2、把Setlocal enabledelayedexpansion和 endlocal放在复合语句外部,代码和结果如下:- ===========代码:========
- set "tmp1=%temp%\t1.txt"
- Setlocal enabledelayedexpansion
- for /f "delims=" %%a in ('type %tmp1%') do (
- set "str=%%a"
- if /i "!str:~,5!"=="HKEY_" (set "sKey=%%a") else (set "sval=%%a")
- if defined sval (echo,!sKey!---!sval! &set "sval=")
- )
- endlocal
- pause&exit
-
- ======结果==========
- HKEY_CURRENT_USER\Temp Num\Temp1---String 1
- HKEY_CURRENT_USER\Temp Num\Temp1---String 2
- HKEY_CURRENT_USER\Temp Num\Temp1---String 3
- HKEY_CURRENT_USER\Temp Num\Temp1---String 4
- HKEY_CURRENT_USER\Temp Num\Temp1---Stringabcdefghigklmnopqrstuvwsyz 1
- HKEY_CURRENT_USER\Temp Num\Temp1---Stringabcdefghigklmnopqrstuvwsyz 2
- HKEY_CURRENT_USER\Temp Num\Temp1---Stringabcdefghigklmnopqrstuvwsyz 3
- HKEY_CURRENT_USER\Temp Num\Temp1---Stringabcdefghigklmnopqrstuvwsyz 4
- HKEY_CURRENT_USER\Temp Num\Temp1---String abcdefghigklmnopqrstuvwsyz str1
- HKEY_CURRENT_USER\Temp Num\Temp1---String abcdefghigklmnopqrstuvwsyz str2
- HKEY_CURRENT_USER\Temp Num\Temp1---String abcdefghigklmnopqrstuvwsyz str3
- HKEY_CURRENT_USER\Temp Num\Temp1---String abcdefghigklmnopqrstuvwsyz str4
- HKEY_CURRENT_USER\Temp Num\Temp2---String 1
- HKEY_CURRENT_USER\Temp Num\Temp2---String 2
- HKEY_CURRENT_USER\Temp Num\Temp2---String 3
- HKEY_CURRENT_USER\Temp Num\Temp2---String 4
-
- ====结论=====
- 变量得到延迟,结果正确
复制代码 补充疑问:
如果把在复合语句内部再开启变量延迟开关是正确的,那么在for循环中,每执行一句就要开关一次,岂不是更消耗资源吗? |