cmd,程序生成帮助文件 [英] cmd, program generating help-files

查看:216
本文介绍了cmd,程序生成帮助文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@echo off
md helpgen 2>nul
cd helpgen
for /F %%i in ('help') do (
  echo %%i|findstr /R "^[A-Z]*$" >nul
  if "%ERRORLEVEL%"=="0" (
    help %%i>%%i.txt
  ) 
)
cd ..

应该从 help 命令为每个文件创建帮助文件。
但是...它不能正常工作,我不知道为什么。帮助我)

This program is supposed to make help-files for every file from the help command. But... it doesn't work correctly and i have no idea why. help me please)

推荐答案

您的代码不工作,因为%ERRORLEVEL%在解析时扩展,整个括号FOR块代码在一遍中解析。您希望每次迭代的值为ERRORLEVEL,但是您的代码在执行循环之前已经存在的值为ERRORLEVEL。

Your code is not working because %ERRORLEVEL% is expanded at parse time and the entire parenthesized FOR block of code is parsed in one pass. You want the value of ERRORLEVEL for each iteration, but your code is getting the value of ERRORLEVEL that existed before the loop is executed.

一个解决方案是启用延迟扩展顶部使用 setlocal enableDelayedExpansion 。然后在你的循环中使用!ERRORLEVEL!而不是%ERRORLEVEL%

One solution is to enable delayed expansion at the top using setlocal enableDelayedExpansion. Then within your loop use !ERRORLEVEL! instead of %ERRORLEVEL%. The delayed expansion will give the desired value of ERRORLEVEL at execution time of each iteration.

键入帮助设置 set /?从命令行获取有关延迟扩展的更多信息。

Type help set or set /? from the command line to get more information about delayed expansion.

但是有一个更容易的解决方案,变量扩展一起。 command1&&& command2 将仅在command1成功时执行command2。如果前面的命令不成功,还有 || 运算符用于执行命令。

But there is an easier solution that avoids variable expansion all-together. command1 && command2 will execute command2 only if command1 was successful. There is also the || operator to use to execute a command if the prior command was not successful.

但是整个练习是无意义的,因为你的FINDSTR表达式不会给出正确的结果,所以你的最终输出仍然是错误的。

But the whole excercise is kind of pointless because your FINDSTR expression will not give the correct results, so your final output will still be wrong.

Ansgar Wiechers确定了一个搜索模式他的回答。不需要tokens = 1,因为这是默认设置。

Ansgar Wiechers identified a search pattern that works in his answer. The "tokens=1" is not needed because that is the default setting.

最终脚本可以简单:

@echo off
md helpgen 2>nul
cd helpgen
for /f %%i in ('help ^| findstr /rc:"^[A-Z][A-Z]*  "') do help %%i>%%i.txt

这篇关于cmd,程序生成帮助文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆