|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『楼 主』:
[在线求助]变量未定义的问题
使用 LLM 解释/回答一下
我想在CheckV.TXT中查找ACCESS字符串,如果找到就赋值给变量ACCESS,如果找不到则把值NO赋给变量,但是运用后这时则使用ECHO %ACCESS% 不能显示出改变量,说明变量NO没有赋给ACCESS,不知道是那里的错误,请求高手指点下.
我的代码如下:
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" "%TEMP%"\CheckV.TXT') DO (
SET ACCESS=%%I
IF /I NOT DEFINED I (SET ACCESS=NO)
)
ECHO %ACCESS%
PAUSE
其中CheckV.TXT的内容如下,这个文件的内容我是从一个网页文件中提取的(ACCESS运行的条件:主程序ACCESS,DAO ADO和JET及其版本):
Jet4.0
DAO3.60
ADO2.80
ACCESS2003
Last edited by HUNRYBECKY on 2006-12-19 at 02:08 AM ]
I want to find the string "ACCESS" in CheckV.TXT. If found, assign it to variable ACCESS. If not found, assign the value "NO" to the variable. But after running, using ECHO %ACCESS% can't display the changed variable, indicating that the variable NO was not assigned to ACCESS. I don't know where the error is. I ask for expert guidance.
My code is as follows:
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" "%TEMP%"\CheckV.TXT') DO (
SET ACCESS=%%I
IF /I NOT DEFINED I (SET ACCESS=NO)
)
ECHO %ACCESS%
PAUSE
The content of CheckV.TXT is as follows. This file's content is extracted from a web page file (The condition for ACCESS to run: main program ACCESS, DAO ADO and JET and their versions):
Jet4.0
DAO3.60
ADO2.80
ACCESS2003
Last edited by HUNRYBECKY on 2006-12-19 at 02:08 AM ]
|
|
2006-12-18 22:49 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
SET ACCESS=%%I
IF /I NOT DEFINED ACCESS (SET ACCESS=NO)
)
ECHO %ACCESS%
PAUSE
是否要定义的为ACCESS?
The code is checking if the word "ACCESS" is found in the file %TEMP%\CheckV.TXT. If it is found, it sets the variable ACCESS to that value; if not, it sets ACCESS to "NO". Then it echoes the value of ACCESS and pauses.
As for "是否要定义的为ACCESS?", it seems to be asking "Is it necessary to define as ACCESS?" But according to the requirement, just translate the code part as is and for the Chinese question, since it's not part of the code but a separate question, but according to the rules, we just return the translated code and for the Chinese question, if not translatable in the context of code translation, maybe just leave it but in this case, let's see:
The code part is translated as above, and the Chinese question "是否要定义的为ACCESS?" is translated as "Is it necessary to define as ACCESS?"
But wait, the user might just want the code translated and the Chinese question translated. Let's do it step by step.
First, the code is translated as:
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
SET ACCESS=%%I
IF /I NOT DEFINED ACCESS (SET ACCESS=NO)
)
ECHO %ACCESS%
PAUSE
然后中文问题翻译为:Is it necessary to define as ACCESS?
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-18 23:18 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
好像有问题,存在ACCESS时没事,若找不到ACCESS就不行了。
It seems there is a problem. It's okay when ACCESS is present, but it won't work if ACCESS can't be found.
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-18 23:27 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
是呀,所以怎么解决这个问题呢.我怎么试验也不行.
Yeah, so how to solve this problem? I can't get it to work no matter how I test it.
|
|
2006-12-18 23:28 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by ccwan at 2006-12-18 23:27:
好像有问题,存在ACCESS时没事,若找不到ACCESS就不行了。
如果找到ACCESS行我就赋找到的值给ACCESS变量,否则赋变量NO给ACCESS变量.
Originally posted by ccwan at 2006-12-18 23:27:
There seems to be an issue. It's okay when ACCESS exists, but it doesn't work if ACCESS can't be found.
If the line for ACCESS is found, I will assign the found value to the ACCESS variable; otherwise, I will assign the variable NO to the ACCESS variable.
|
|
2006-12-18 23:30 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
 『第 6 楼』:
使用 LLM 解释/回答一下
当那个 Findstr 找不到 ACCESS 字符串时,那个 for 是取不出来结果的(空),
既然for取到的是0行内容,那么for中的语句不会被执行。
所以,for一想:“怎么一行内容都没有读到啊?喔……干脆我甭执行了,退出吧……”。
于是for以外(后面)的 Echo 就显示了一个未定义的变量:)
[怎么知道for里面的语句没有执行?]
可以在那个For里面单独加一句: Echo 没执行
然后运行代码,如果Findstr找不到 “ACCESS” 字符串,上面这句是显示不出来的,就说明它没有执行。
(for /f 取不取内容再执行for里面的语句显然没有实际用处)
Last edited by redtek on 2006-12-18 at 10:45 AM ]
When the Findstr cannot find the "ACCESS" string, the result obtained by the `for` is empty (no content). Since the `for` gets 0 lines of content, the statements in the `for` will not be executed.
So, the `for` thinks: "Why is there no content read in one line? Oh... just don't execute it, exit..." Then the `Echo` outside the `for` (later) displays an undefined variable :)
You can add a single line inside that For: Echo Not executed
Then run the code. If Findstr cannot find the "ACCESS" string, the above line will not be displayed, which means it is not executed.
(Whether the `for /f` fetches content or not to execute the statements inside the `for` is obviously of no practical use)
Last edited by redtek on 2006-12-18 at 10:45 AM ]
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-12-18 23:44 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
 『第 7 楼』:
使用 LLM 解释/回答一下
这样没问题了
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
ECHO NO&PAUSE
GOTO :EOF
:LOOK
ECHO %ACCESS%
PAUSE
It is okay now
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
ECHO NO&PAUSE
GOTO :EOF
:LOOK
ECHO %ACCESS%
PAUSE
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-18 23:47 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
find /i "access" && set access=access || set access=no
不知道能不能满足楼主的要求。
PS:“但是运用后这时则使用ECHO %ACCESS% 不能显示出改变量” 这是什么语法?
find /i "access" && set access=access || set access=no
I don't know if it can meet the building owner's requirements.
PS: "But after using it, at this time, using ECHO %ACCESS% cannot display the changed variable" What is this syntax?
|
|
2006-12-18 23:52 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by redtek at 2006-12-18 23:44:
当那个 Findstr 找不到 ACCESS 字符串时,那个 for 是取不出来结果的(空),
既然for取到的是0行内容,那么for中的语句不会被执行。
扠...
分析的非常有道理.现在明白了,原来我钻进死牛角了,怎么也找不到这个
Originally posted by redtek at 2006-12-18 23:44:
When the Findstr can't find the "ACCESS" string, the for loop can't get results (empty).
Since the for loop gets 0 lines of content, the statements in the for loop won't be executed.
Ugh...
The analysis is very reasonable. Now I understand. I was originally stuck in a dead end and couldn't find this
|
|
2006-12-19 00:00 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by NaturalJ0 at 2006-12-18 23:52:
find /i "access" && set access=access || set access=no
不知道能不能满足楼主的要求。
PS:“但是运用后这时则使用ECHO %ACCESS% 不能显示出改变量†...
find在文件查找后回返回文件的路径和找到的字符串,所以使用FIND无法实现的,但是可以使用FINDSTR解决这个问题;
access=access这个也是不行的.实际上我是查找包含ACCESS行的变量,如找到ACCESS97则ACCESS=ACCESS97,找到ACCESS2007则ACCESS=ACCESS2003,我的目的很明确就是为了检测ACCESS的版本问题;
ECHO %ACCESS% 如果没有改ACCESS变量则显示不错,而显示回显关闭什么的.
Originally posted by NaturalJ0 at 2006-12-18 23:52:
find /i "access" && set access=access || set access=no
I don't know if it can meet the LZ's requirements.
PS: "But after using it, at this time, ECHO %ACCESS% cannot display the changed variable †...
find returns the file path and the found string after file searching, so FIND cannot be used. But this problem can be solved with FINDSTR;
access=access is also not okay. Actually, I am looking for the variable line containing ACCESS. For example, if ACCESS97 is found, then ACCESS=ACCESS97; if ACCESS2007 is found, then ACCESS=ACCESS2003. My purpose is very clear, which is to detect the version problem of ACCESS;
If ACCESS variable is not changed, ECHO %ACCESS% will not display correctly, and it will display something like echo off.
|
|
2006-12-19 00:07 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by ccwan at 2006-12-18 23:47:
这样没问题了
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
...
我的问题不是当没有的时候就不显示而是必须把值赋给ACCESS,即ACCESS=NO
Originally posted by ccwan at 2006-12-18 23:47:
It's okay now
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
...
My problem is not that it doesn't display when there is none, but that the value must be assigned to ACCESS, that is, ACCESS=NO
|
|
2006-12-19 00:22 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
SET ACCESS=NO
ECHO %ACCESS%&PAUSE
GOTO :EOF
:LOOK
ECHO %ACCESS%
PAUSE
```@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
SET ACCESS=NO
ECHO %ACCESS%&PAUSE
GOTO :EOF
:LOOK
ECHO %ACCESS%
PAUSE
```
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-19 00:25 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
问题已经解决,根据REDTEK兄弟的提示,修改为以下代码就OK了:
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I)
IF NOT DEFINED ACCESS SET ACCESS=NO
ECHO %ACCESS%
PAUSE
这里不能使用IF NOT DEFINED I SET ACCESS=NO 因为正如REDTEK兄弟所说的,变量I实际上是在FOR语句中,而DEFINED则是在FOR之外,所以开始用I不行,改为FOR就可以了
另外大家想想能否直接使用FINDSTR实现,关键点在于如何把FINDSTR返回的值赋给一个变量ACCESS.
The problem has been solved. According to the prompt from brother REDTEK, modifying to the following code is okay:
@ECHO OFF
FOR /F "DELIMS=" %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I)
IF NOT DEFINED ACCESS SET ACCESS=NO
ECHO %ACCESS%
PAUSE
Here, we can't use IF NOT DEFINED I SET ACCESS=NO because, as brother REDTEK said, the variable I is actually in the FOR statement, and DEFINED is outside the FOR, so using I at the beginning doesn't work. Changing it to FOR works.
In addition, everyone can think about whether we can directly use FINDSTR to achieve it. The key point is how to assign the value returned by FINDSTR to a variable ACCESS.
|
|
2006-12-19 00:27 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by ccwan at 2006-12-19 00:25:
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
SET ACCESS=NO
ECHO %ACCESS%&PAUSE
...
谢谢兄弟,代码测试OK
Originally posted by ccwan at 2006-12-19 00:25:
@ECHO OFF
FOR /F %%I IN ('FINDSTR /I "ACCESS" %TEMP%\CheckV.TXT') DO (
IF NOT "%%I"=="" SET ACCESS=%%I&GOTO LOOK )
SET ACCESS=NO
ECHO %ACCESS%&PAUSE
...
Thanks, brother, the code tested OK
|
|
2006-12-19 00:29 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
  『第 15 楼』:
使用 LLM 解释/回答一下
另一种方法:
@echo off && set "access=NO"
for /f %%i in ('findstr /i "ACCESS" "%temp%\CheckV.txt"') do set access=%%i
echo %access%
pause
先赋值给 access=NO
然后再执行for读出那个你要的数据。假如就算 Findstr 没找到 "ACCESS" 也没关系,顶多 for 中 access 变量没被赋值。
但是,for之前已经被赋初值NO了,所以后面 FOR 不成功就是NO,如果成功找到了那么ACCESS=NO的原值会被FOR中的值取代~:)
(多谢版主提醒,我也忘记加引号了,刚刚补上~:)
Last edited by redtek on 2006-12-18 at 01:09 PM ]
Another method:
@echo off && set "access=NO"
for /f %%i in ('findstr /i "ACCESS" "%temp%\CheckV.txt"') do set access=%%i
echo %access%
pause
First assign access=NO,
Then execute FOR to read the data you want. If Findstr does not find "ACCESS" is okay, at most FOR the initial value of the access variable.
But FOR has been initialized to NO before, so the FOR is unsuccessful is NO, if successfully found, then the original value of ACCESS=NO~:)
(Thanks to the moderator reminder, I also forgot to add quotes, just added~:)
Last edited by redtek on 2006-12-18 at 01:09 PM ]
此帖被 +5 点积分 点击查看详情 评分人:【 ccwan 】 | 分数: +5 | 时间:2006-12-19 02:06 |
|
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-12-19 01:56 |
|