China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-02 19:14
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Online Help] Problem of Undefined Variable View 2,253 Replies 19
Original Poster Posted 2006-12-18 22:49 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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 ]
Floor 2 Posted 2006-12-18 23:18 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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?
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2006-12-18 23:27 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
It seems there is a problem. It's okay when ACCESS is present, but it won't work if ACCESS can't be found.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2006-12-18 23:28 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Yeah, so how to solve this problem? I can't get it to work no matter how I test it.
Floor 5 Posted 2006-12-18 23:30 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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.
Floor 6 Posted 2006-12-18 23:44 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HUNRYBECKY +2 2006-12-18 23:58
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 7 Posted 2006-12-18 23:47 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +5 2006-12-18 23:49
HUNRYBECKY +2 2006-12-18 23:59
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2006-12-18 23:52 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
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?
Floor 9 Posted 2006-12-19 00:00 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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
Floor 10 Posted 2006-12-19 00:07 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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.
Floor 11 Posted 2006-12-19 00:22 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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
Floor 12 Posted 2006-12-19 00:25 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```@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
```
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 13 Posted 2006-12-19 00:27 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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.
Floor 14 Posted 2006-12-19 00:29 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
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
Floor 15 Posted 2006-12-19 01:56 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-19 02:06
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Forum Jump: