中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-02 20:21
48,038 topics / 350,123 posts / today 1 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » [Online Help] Problem of Undefined Variable
Printable Version  2,257 / 19
Floor1 HUNRYBECKY Posted 2006-12-18 22:49
银牌会员 Posts 442 Credits 1,179
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 ]
Floor2 ccwan Posted 2006-12-18 23:18
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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:



然后中文问题翻译为:Is it necessary to define as ACCESS?
Floor3 ccwan Posted 2006-12-18 23:27
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
It seems there is a problem. It's okay when ACCESS is present, but it won't work if ACCESS can't be found.
Floor4 HUNRYBECKY Posted 2006-12-18 23:28
银牌会员 Posts 442 Credits 1,179
Yeah, so how to solve this problem? I can't get it to work no matter how I test it.
Floor5 HUNRYBECKY Posted 2006-12-18 23:30
银牌会员 Posts 442 Credits 1,179
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.
Floor6 redtek Posted 2006-12-18 23:44
金牌会员 Posts 1,147 Credits 2,902
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 ]
Floor7 ccwan Posted 2006-12-18 23:47
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
It is okay now
Floor8 NaturalJ0 Posted 2006-12-18 23:52
银牌会员 Posts 533 Credits 1,181
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?
Floor9 HUNRYBECKY Posted 2006-12-19 00:00
银牌会员 Posts 442 Credits 1,179
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
Floor10 HUNRYBECKY Posted 2006-12-19 00:07
银牌会员 Posts 442 Credits 1,179
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.
Floor11 HUNRYBECKY Posted 2006-12-19 00:22
银牌会员 Posts 442 Credits 1,179
Originally posted by ccwan at 2006-12-18 23:47:
It's okay now
Floor12 ccwan Posted 2006-12-19 00:25
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
```@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
```
Floor13 HUNRYBECKY Posted 2006-12-19 00:27
银牌会员 Posts 442 Credits 1,179
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.
Floor14 HUNRYBECKY Posted 2006-12-19 00:29
银牌会员 Posts 442 Credits 1,179
Originally posted by ccwan at 2006-12-19 00:25:
Floor15 redtek Posted 2006-12-19 01:56
金牌会员 Posts 1,147 Credits 2,902
Another method:



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 ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023