|
amao
中级用户
  
积分 316
发帖 152
注册 2006-6-18
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
等你们各位高手解决好了,我再慢慢学习,CMD的批处理我太菜了,刚开始学习。
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.
|
|
2006-12-12 19:20 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
  『第 17 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2006-12-12 00:50:
还有一个问题,就是如果a.txt里面有空行(直接回车)的话,那么就会出错,批处理提示:
已经达到最大的 setlocal 递归层。
不知道这个问题怎么解决?
实际上,并非是由空行引起的,而是行数超出了 setlocal 的限制。根据测试,setlocal 嵌套使用时,单个 setlocal 语句最多只能处理15行内容,超过15行就会超过最大的递归层,解决办法就是在适当的位置加上终止环境变量本地化的语句 endlocal 。
受 3742668 兄12F代码的启发,在11F代码的基础上,得到如下代码。下面的代码能完全兼容感叹号、连接符号、重定向符号、行首分号、行首冒号,解决了 setlocal 递归层的问题,并且能计算空行(以前的代码都会忽略空行):
@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 第 !num! 行: !str:*:=!
endlocal
)
pause
Last edited by namejm on 2006-12-12 at 09:50 AM ]
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 ]
此帖被 +20 点积分 点击查看详情 评分人:【 redtek 】 | 分数: +7 | 时间:2006-12-12 22:55 | 评分人:【 ccwan 】 | 分数: +8 | 时间:2006-12-13 04:32 | 评分人:【 lxmxn 】 | 分数: +5 | 时间:2006-12-13 11:20 |
|
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-12 22:00 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
namejm兄,我试过之后发现前30行没问题,31行开始报错:
第!num!行:!str:*:=!
已经达到最大的setlacal递归层。
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.
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-12 22:37 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
取消多余的 setlocal 语句之后,不再出现最大递归层的问题,17F代码已经修正,请再次测试。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-12 22:47 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
呵呵!果然漂亮!
从namejm兄这里获益良多啊!
等偶冷却期过了给兄加分!^_^
Hehe! Indeed beautiful!
I have benefited a lot from brother namejm!
I will give you points when my cooling-off period is over! ^_^
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-12 22:53 |
|
|
andred0421
初级用户
 
积分 114
发帖 47
注册 2006-11-20
状态 离线
|
『第 21 楼』:
使用 LLM 解释/回答一下
强啊,学习中,
呵呵
每次来都有收获
Great, learning.
Hehe
Always gain something every time I come
|
|
2006-12-13 12:50 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 22 楼』:
使用 LLM 解释/回答一下
还不能处理unicode文本,在findstr前加个more可以解决
为了能重定向到文件,稍微作了些修改
还是有问题:原文件是unicode的,目标仍然是ansi,fc必然通不过。
@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 ]
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 ]
|
|
2006-12-13 22:45 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 23 楼』:
使用 LLM 解释/回答一下
不知道楼上的测试了你的代码了么?好像有问题。
I don't know if the person upstairs tested your code? It seems there is a problem.
|
|
2006-12-14 00:32 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 24 楼』:
使用 LLM 解释/回答一下
测试代码而已,有问题很正常
不过,以后报问题请报出问题是什么,在什么情况下出现
我发出代码之前自然是没碰到什么未料到的问题
将上面的代码略作改变,方便自己,也方便别人测试
顺便解决了fc与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 ]
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 ]
|
|
2006-12-14 01:00 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 25 楼』:
使用 LLM 解释/回答一下
无论是直接双击运行,还是拖放文件运行,在命令行运行,都报错了,提示此时不应有 and 。
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.
|
|
2006-12-14 02:43 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 26 楼』:
使用 LLM 解释/回答一下
这显然与测试文本有关,请贴上来看看。
This is obviously related to the test text, please post it and have a look.
|
|
2006-12-14 03:31 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 27 楼』:
使用 LLM 解释/回答一下
测试了N久,发现问题了。
开始的时候,直接双击会出现“此时不应有and”的错误提示,仔细分析了一下兄的代码,发现兄的代码开始处有一段“if "%1"=="" %0 "%~f0"”,真是这个%~f0的扩展功能是第一参数成了完整路径,而我的批处理都是在C:\Documents and Settings\Administrator\桌面\文件\Temp中测试的,所以导致第一参数为"C:\Documents",而不是"C:\Documents and Settings\Administrator\桌面\文件\Temp\test.bat",最终导致提示错误的信息出现了。而且直接拖放文件也会出现类似的提示错误。
所以在带有空格的路径下测试以上代码,就容易报错,建议在代码前面加上处理带有空格路径的语句,我讲%~f0改为了%~s0,问题就解决了。
还有一个小问题,就是不能很好的支持文件的拖放,不知道是我的问题还是代码的问题。
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.
|
|
2006-12-14 06:37 |
|
|
hxuan999
中级用户
   DOS之日
积分 337
发帖 161
注册 2006-11-4
状态 离线
|
『第 28 楼』:
使用 LLM 解释/回答一下
拖放时的错误是一样的.还有在双击时也有,只有在命令行是用参数指定相对路径是正确,都是因为路径中有空格的问题.
另外还有一个问题,双击时调用本身,返回后没有退出.
修改24楼后代码:
- @echo off
- if "%~1"=="" (
- %0 "%~f0"
- goto :eof
- )
- (for /f "usebackq delims=" %%i in (`more ^< %1 ^| findstr /n .*`) do (
- set "str=%%i"
- setlocal enabledelayedexpansion
- echo;!str:*:=!
- endlocal
- ))>"%~dp1test.tmp"
- type "%~dp1test.tmp"
- echo ------------------
- more < "%~1" > "%~1.tmp"
- fc "%~1.tmp" "%~dp1test.tmp"
- pause
hxuan?表ー: 2006-12-13 18:28
拖放,双击,命令行都没有问题.
Last edited by hxuan999 on 2006-12-13 at 07:27 PM ]
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:
- @echo off
- if "%~1"=="" (
- %0 "%~f0"
- goto :eof
- )
- (for /f "usebackq delims=" %%i in (`more ^< %1 ^| findstr /n .*`) do (
- set "str=%%i"
- setlocal enabledelayedexpansion
- echo;!str:*:=!
- endlocal
- ))>"%~dp1test.tmp"
- type "%~dp1test.tmp"
- echo ------------------
- more < "%~1" > "%~1.tmp"
- fc "%~1.tmp" "%~dp1test.tmp"
- 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 |
|
2006-12-14 07:26 |
|
|
hxuan999
中级用户
   DOS之日
积分 337
发帖 161
注册 2006-11-4
状态 离线
|
『第 29 楼』:
使用 LLM 解释/回答一下
另外17楼代码最好加一句:setlocal disabledelayedexpansion.如下:
@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 第 !num! 行: !str:*:=!
endlocal
)
pause
这样可以防止在注册表中修改过DelayedExpansion键值的情况.(本人就是,^&^,所以一直都看不到感叹号,测试了半天才想起来.)
Last edited by hxuan999 on 2006-12-13 at 07:33 PM ]
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 |
|
2006-12-14 07:29 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 30 楼』:
使用 LLM 解释/回答一下
谢谢楼上的兄弟!
goto :eof 是没有必要的,%0 "%~f0" 之后是不会返回的
usebackq 可以不用,而且最好不用,因为它所带来的隐患比较多
原代码中的问题属于路径参数处理问题,楼上的话收回
因为我从来都是在d:\test下作测试的,很少去注意这些问题,得到一个教训
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
|
|
2006-12-14 07:41 |
|