|  | 
 
| hahaaj 新手上路
 
  
 
 
 
 积分 12
 发帖 5
 注册 2008-4-9
 状态 离线
 | 
| 『楼 主』:
 在for语句中修改变量的问题
 
使用 LLM 解释/回答一下 
 
 
set motFn=
 for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
 set a=%%i
 setlocal EnableDelayedExpansion
 if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!   //此处echo得预期结果
 endlocal
 )
 
 echo %motFn% //此处echo则得 %motFn%变量为空??
 
 
 
 
 |  | 
|  2008-4-11 11:28 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 | 
| 『第 2 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
因为在endlocal命令后所有前面的变量赋值都将取消,所以%monFn%会为空。把if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !motFn!就是的啊。
 
 
 
 
 
 |  
                  |  批处理之家新域名:www.bathome.net
 |  | 
|  2008-4-11 11:40 |  | 
|  | 
 
| hahaaj 新手上路
 
  
 
 
 
 积分 12
 发帖 5
 注册 2008-4-9
 状态 离线
 |  | 
|  2008-4-11 12:07 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 |  | 
|  2008-4-11 12:10 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 | 
| 『第 5 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&call :lp !!motFn!!或
 if "!a:~0,4!"=="motF" set motFn=!a:~5!&echo !a:~5!改为if "!a:~0,4!"=="motF" set motFn=!a:~5!&goto lp
 
 
 
 
 
 |  
                  |  批处理之家新域名:www.bathome.net
 |  | 
|  2008-4-11 12:11 |  | 
|  | 
 
| hahaaj 新手上路
 
  
 
 
 
 积分 12
 发帖 5
 注册 2008-4-9
 状态 离线
 | 
| 『第 6 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
我实际的代码如下
 set motFn=
 set gcFn=
 set obFn=
 
 for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
 set a=%%i
 setlocal EnableDelayedExpansion
 if "!a:~0,4!"=="motF" set motFn=!a:~5!
 if "!a:~0,4!"=="gctF" set gcFn=!a:~5!
 if "!a:~0,4!"=="binF" set obFn=!a:~5!
 endlocal
 )
 if "%motFn%"=="" (echo ~FAIL~~,.~ Did NOT specify motF&goto :conf)
 if "%gcFn%"=="" (echo ~FAIL~~,.~ Did NOT specify gctF&goto :conf)
 if "%obFn%"=="" (echo ~FAIL~~,.~ Did NOT specify binF&goto :conf)
 
 就是要在一条for语句中给三个变量赋值,楼上的方法只能给一个变量赋值
 
 
 
 
 |  | 
|  2008-4-11 14:44 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 | 
| 『第 7 楼』:
 试试这样可以不啊:
 
使用 LLM 解释/回答一下 
 
 
set motFn=        set gcFn=
 set obFn=
 
 for /f "eol=[ usebackq" %%i in (`type %confFn%`) do (
 set a=%%i
 setlocal EnableDelayedExpansion
 if "!a:~0,4!"=="motF" set motFn=!a:~5!&call :lp1
 if "!a:~0,4!"=="gctF" set gcFn=!a:~5!&call :lp2
 if "!a:~0,4!"=="binF" set obFn=!a:~5!&call :lp3
 endlocal
 )
 :lp1
 if "%motFn%"=="" (echo ~FAIL~~,.~ Did NOT specify motF&goto :conf)
 goto :eof
 :lp2
 if "%gcFn%"=="" (echo ~FAIL~~,.~ Did NOT specify gctF&goto :conf)
 goto :eof
 :lp3
 if "%obFn%"=="" (echo ~FAIL~~,.~ Did NOT specify binF&goto :conf)
 goto :eof
 
 
 
 
 
 |  
                  |  批处理之家新域名:www.bathome.net
 |  | 
|  2008-4-11 17:36 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 |  | 
|  2008-4-11 17:39 |  | 
|  | 
 
| hahaaj 新手上路
 
  
 
 
 
 积分 12
 发帖 5
 注册 2008-4-9
 状态 离线
 | 
| 『第 9 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
Originally posted by zw19750516 at 2008-4-11 05:36 PM:set motFn=
 set gcFn=
 set obFn=
 
 for /f "eol=
 
 
 
 用call方法一跳出for语句就回不去哒。.
 
 
 最后我是像楼上说的那样去掉了for结尾处的endlocal语句(不过后面引用变量还是用的“%”,没有改成“!”)。这样bat就能按照预期的方式工作了。
 
 后来知道了setlocal EnableDelayedExpansion。。。。endlocal块中设置的变量在endlocal后就失效了。
 
 那么。又有一个疑问:endlocal是多余的一样。。
 
 
 
 
 |  | 
|  2008-4-14 14:03 |  | 
|  | 
 
| bat-zw 金牌会员
 
       永远的学习者
 
 
 积分 3105
 发帖 1276
 注册 2008-3-8
 状态 离线
 | 
| 『第 10 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
endlocal在for语句递归层小于33的时候可以不加,但当递归层达到33以上时就必须加上了,运行下面的代码你就会明白了: @echo offfor /l %%i in (1,1,33) do (
 set a=%%i
 setlocal enabledelayedexpansion
 echo !a!
 )
 pause
 
 
 
 
 |  
                  |  批处理之家新域名:www.bathome.net
 |  | 
|  2008-4-14 15:15 |  | 
|  | 
 
| terse 银牌会员
 
      
 
 
 
 积分 2404
 发帖 946
 注册 2005-9-8
 状态 离线
 | 
| 『第 11 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
我觉得可以CALL出的:@echo off
 for /f "eol=[ usebackq" %%i in (`type %confFn%`) do call:lp %%i
 echo %motFn% %gcFn% %obFn%
 pause
 goto :eof
 :lp
 set a=%1
 if "%a:~0,4%"=="motF" set motFn=%a:~5%
 if "%a:~0,4%"=="gctF" set gcFn=%a:~5%
 if "%a:~0,4%"=="binF" set obFn=%a:~5%
 
 
 
 
 
 |  
                  |  简单!简单!再简单!
 |  | 
|  2008-4-14 15:49 |  |