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-06-22 11:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问如果在文本中抽取指定字符赋值于变量中 Please ask if you want to extract specified characters from text and assign them to variables View 5,747 Replies 32
Floor 16 Posted 2006-12-12 19:20 ·  美国 北达科他州立大学
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
Wait for you all experts to solve it, and then I'll learn slowly. I'm too bad at CMD batch processing, just started learning.
Floor 17 Posted 2006-12-12 22:00 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by lxmxn at 2006-12-12 00:50:

  There is another problem. If there are blank lines (just pressing Enter) in a.txt, then an error will occur, and the batch file prompts:
  The maximum setlocal recursion level has been reached.
  I don't know how to solve this problem?

  Actually, it is not caused by blank lines, but by the number of lines exceeding the limit of setlocal. According to testing, when setlocal is used nestedly, a single setlocal statement can handle at most 15 lines of content. If it exceeds 15 lines, the maximum recursion level will be exceeded. The solution is to add the statement endlocal to terminate the environment variable localization at an appropriate position.

  Inspired by brother 3742668's code in 12F, based on the code in 11F, the following code is obtained. The following code can be fully compatible with exclamation marks, connection symbols, redirection symbols, semicolons at the beginning of lines, colons at the beginning of lines, solves the problem of setlocal recursion level, and can count blank lines (previous codes all ignored blank lines):

@echo off
set num=0
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a num+=1
set "str=%%i"
setlocal enabledelayedexpansion
echo Line !num!: !str:*:=!
endlocal
)
pause


[ Last edited by namejm on 2006-12-12 at 09:50 AM ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
redtek +7 2006-12-12 22:55
ccwan +8 2006-12-13 04:32
lxmxn +5 2006-12-13 11:20
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 18 Posted 2006-12-12 22:37 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Brother namejm, after I tried it, I found that the first 30 lines are okay, and errors start from line 31:
Line!num!:!str:*:=!
Maximum setlocal recursion level reached.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 19 Posted 2006-12-12 22:47 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
After removing the redundant setlocal statements, the problem of the maximum recursion level no longer occurs. The 17F code has been corrected, please test again.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 20 Posted 2006-12-12 22:53 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe! Indeed beautiful!
I have benefited a lot from brother namejm!
I will give you points when my cooling-off period is over! ^_^
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 21 Posted 2006-12-13 12:50 ·  中国 北京 鹏博士BGP
初级用户
Credits 114
Posts 47
Joined 2006-11-20 04:17
19-year member
UID 71105
Gender Male
Status Offline
Great, learning.
Hehe
Always gain something every time I come
Floor 22 Posted 2006-12-13 22:45 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Still can't handle Unicode text. Adding more before findstr can solve it.
A little modification was made in order to redirect to a file.
Still has problems: The original file is Unicode, and the target is still ANSI, so fc will definitely not pass.


@echo off
(for /f "delims=" %%i in ('more ^< "%~f1" ^| findstr /n .*') do (
set "str=%%i"
setlocal enabledelayedexpansion
echo;!str:*:=!
endlocal
))>test.out
fc "%~f1" test.out
pause


[ Last edited by qzwqzw on 2006-12-13 at 10:28 AM ]
Floor 23 Posted 2006-12-14 00:32 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  I don't know if the person upstairs tested your code? It seems there is a problem.
Floor 24 Posted 2006-12-14 01:00 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Just a test code, it's normal to have issues.

But in the future, when reporting issues, please state what the issue is and under what circumstances it occurs.

Naturally, I didn't encounter any unexpected issues before I sent out the code.

I made a slight change to the above code to make it convenient for myself and others to test.

By the way, it solved the issue with fc and unicode.


@echo off
if "%1"=="" %0 "%~f0"
(for /f "delims=" %%i in ('more ^< %1 ^| findstr /n .*') do (
set "str=%%i"
setlocal enabledelayedexpansion
echo;!str:*:=!
endlocal
))>test.tmp
type test.tmp
echo ---------
more < %1 > %1.tmp
fc %1.tmp test.tmp
pause


[ Last edited by qzwqzw on 2006-12-13 at 12:06 PM ]
Floor 25 Posted 2006-12-14 02:43 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

Whether running directly by double-clicking, or dragging and dropping files to run, or running in the command line, all report errors, prompting that "and" should not be here at this time.
Floor 26 Posted 2006-12-14 03:31 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
This is obviously related to the test text, please post it and have a look.
Floor 27 Posted 2006-12-14 06:37 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Tested for a long time and found the problem.

  At the beginning, double-clicking directly will show the error prompt "There should be no and at this time". After carefully analyzing the brother's code, I found that there is a section of "if "%1"=="" %0 "%~f0"" at the beginning of the brother's code. The extended function of this %~f0 is that the first parameter becomes the full path, but my batch files are all tested in "C:\Documents and Settings\Administrator\Desktop\Files\Temp", so the first parameter becomes "C:\Documents" instead of "C:\Documents and Settings\Administrator\Desktop\Files\Temp\test.bat", which eventually leads to the error message. And dragging and dropping files directly will also show similar error prompts.

  So when testing the above code in a path with spaces, it is easy to report an error. It is recommended to add a statement to handle the path with spaces in front of the code. I changed %~f0 to %~s0, and the problem was solved.

  There is also a small problem, that is, it does not support file dragging and dropping well. I don't know if it's my problem or the code's problem.
Floor 28 Posted 2006-12-14 07:26
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
The errors during drag-and-drop are the same. Also, there are issues during double-clicking. Only when specifying a relative path with parameters in the command line is it correct, all because of the problem of spaces in the path.

Another problem is that when double-clicking, it calls itself, and after returning, it doesn't exit.

Modified code from post 24:


  1. @echo off
  2. if "%~1"=="" (
  3. %0 "%~f0"
  4. goto :eof
  5. )
  6. (for /f "usebackq delims=" %%i in (`more ^< %1 ^| findstr /n .*`) do (
  7. set "str=%%i"
  8. setlocal enabledelayedexpansion
  9. echo;!str:*:=!
  10. endlocal
  11. ))>"%~dp1test.tmp"
  12. type "%~dp1test.tmp"
  13. echo ------------------
  14. more < "%~1" > "%~1.tmp"
  15. fc "%~1.tmp" "%~dp1test.tmp"
  16. pause
hxuan?表ー: 2006-12-13 18:28


There are no problems with drag-and-drop, double-clicking, and command line.

[ Last edited by hxuan999 on 2006-12-13 at 07:27 PM ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 29 Posted 2006-12-14 07:29
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
Also, it's best to add a line: setlocal disabledelayedexpansion in the code on floor 17. As follows:


@echo off
setlocal disabledelayedexpansion
set num=0
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a num+=1
set "str=%%i"
setlocal enabledelayedexpansion
echo Line !num!: !str:*:=!
endlocal
)
pause


This can prevent the situation where the DelayedExpansion key value has been modified in the registry. (I was like this myself, ^&^, so I couldn't see the exclamation marks all the time, and it took me half a day to test it before I remembered.)

[ Last edited by hxuan999 on 2006-12-13 at 07:33 PM ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 30 Posted 2006-12-14 07:41 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Thank you, brother upstairs!

goto :eof is unnecessary, and there will be no return after %0 "%~f0"

usebackq can be omitted, and it's better to do so because it brings more hidden risks

The problem in the original code belongs to the path parameter handling issue, and the words of the upstairs are taken back

Because I always test under d:\test, and rarely pay attention to these issues, and get a lesson
Forum Jump: