中国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-11-24 17:07
47,820 topics / 349,938 posts / today 0 new / 48,298 members
DOS批处理 & 脚本技术(批处理室) » 请问如果在文本中抽取指定字符赋值于变量中 Please ask if you want to extract specified characters from text and assign them to variables
Printable Version  7,312 / 32
Floor16 amao Posted 2006-12-12 19:20
中级用户 Posts 152 Credits 316
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.
Floor17 namejm Posted 2006-12-12 22:00
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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):


[ Last edited by namejm on 2006-12-12 at 09:50 AM ]
Floor18 ccwan Posted 2006-12-12 22:37
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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.
Floor19 namejm Posted 2006-12-12 22:47
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor20 ccwan Posted 2006-12-12 22:53
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Hehe! Indeed beautiful!
I have benefited a lot from brother namejm!
I will give you points when my cooling-off period is over! ^_^
Floor21 andred0421 Posted 2006-12-13 12:50
初级用户 Posts 47 Credits 114
Great, learning.
Hehe
Always gain something every time I come
Floor22 qzwqzw Posted 2006-12-13 22:45
银牌会员 Posts 636 Credits 2,343
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.



[ Last edited by qzwqzw on 2006-12-13 at 10:28 AM ]
Floor23 lxmxn Posted 2006-12-14 00:32
版主 Posts 4,938 Credits 11,386

  I don't know if the person upstairs tested your code? It seems there is a problem.
Floor24 qzwqzw Posted 2006-12-14 01:00
银牌会员 Posts 636 Credits 2,343
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.



[ Last edited by qzwqzw on 2006-12-13 at 12:06 PM ]
Floor25 lxmxn Posted 2006-12-14 02:43
版主 Posts 4,938 Credits 11,386

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.
Floor26 qzwqzw Posted 2006-12-14 03:31
银牌会员 Posts 636 Credits 2,343
This is obviously related to the test text, please post it and have a look.
Floor27 lxmxn Posted 2006-12-14 06:37
版主 Posts 4,938 Credits 11,386

  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.
Floor28 hxuan999 Posted 2006-12-14 07:26
中级用户 Posts 161 Credits 337
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 ]
Floor29 hxuan999 Posted 2006-12-14 07:29
中级用户 Posts 161 Credits 337
Also, it's best to add a line: setlocal disabledelayedexpansion in the code on floor 17. As follows:



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 ]
Floor30 qzwqzw Posted 2006-12-14 07:41
银牌会员 Posts 636 Credits 2,343
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
Prev  1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023