───────────────── Moderation Record ─────────────────
Performed by: Will Sort
Operation: Delete topic: "21098 - If I want to implement the nested transfer of variables between two for statements. As the title says"
Explanation: The original topic and part of the content of the current topic are repeated
Punishment: Deduct 6 points of points awarded for posting this topic, deduct 2 points of points for the topic violation penalty
───────────────── Moderation Record ─────────────────
Re piziliu2004:
!c! is the usage of variable delayed expansion. There have been multiple explanations in this forum. It is recommended that you search first. In addition, you can refer to the command line help information of cmd /?.
The failure of set1~n is indeed due to setlocal. For specific details, please refer to setlocal /? and the relevant information in Windows Help and Support Center.
Usually, we don't need to keep the variable modifications in the batch file after the batch file ends, especially in the case of multiple variables. If you have such a need, it means that there is a certain defect in the program structure design. Of course, it is possible to keep variable modifications outside the batch file. The simplest way is not to use the setlocal command. If you must use it, such as using it to enable the variable delayed expansion feature, you need to use another trick.
I rarely use this trick. It is only used when it is difficult to avoid using setlocal and variables must be saved for external program reference. The method is to use the rarely used endlocal command. For example, if you want to keep the modification of variable envar before the program ends, you can insert the following statement at the end of the program.
endlocal & set envar=%envar%
For the code on floor 8, it is relatively more complicated because multiple variables with dynamic names need to be retained. The following code can be used.
@echo off & setlocal EnableDelayedExpansion
if exist _setvar.bat del _setvar.bat
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
echo set set!c!=%%i>>_setvar.bat
)
endlocal & if exist _setvar.bat (call _setvar.bat & del _setvar.bat)
set set
[
Last edited by willsort on 2006-6-8 at 20:44 ]
※ Batchinger 致 Bat Fans:请访问
批处理编程的异类 ,欢迎交流与共享批处理编程心得!