Board logo

标题: [转贴] 脑洞清奇的批处理多行注释 [打印本页]

作者: CrLf    时间: 2018-7-11 17:57     标题: 脑洞清奇的批处理多行注释

很有意思,但是因为依赖 goto,所以效率比较低,不建议用在 for 里
  1. https://stackoverflow.com/questions/8526946/commenting-multiple-lines-in-dos-batch-file/28522310#28522310
  2. The update in the dbenham's answer gave me some ideas. First - there are two different cases where we can need multi line comments - in a bracket's context where GOTO cannot be used and outside it. Inside brackets context we can use another brackets if there's a condition which prevents the code to be executed.Though the code thede will still be parsed and some syntax errors will be detected (FOR,IF ,improperly closed brackets, wrong parameter expansion ..).So if it is possible it's better to use GOTO.
  3. Though it is not possible to create a macro/variable used as a label - but is possible to use macros for bracket's comments.Still two tricks can be used make the GOTO comments more symetrical and more pleasing (at least for me). For this I'll use two tricks - 1) you can put a single symbol in front of a label and goto will still able to find it (I have no idea why is this.My guues it is searching for a drive). 2) you can put a single : at the end of a variable name and a replacement/subtring feature will be not triggered (even under enabled extensions). Wich combined with the macros for brackets comments can make the both cases to look almost the same.
  4. So here are the examples (in order I like them most):
  5. With rectangular brackets:
  6. @echo off
  7. ::GOTO comment macro
  8. set "[:=goto :]%%"
  9. ::brackets comment macros
  10. set "[=rem/||(" & set "]=)"
  11. ::testing
  12. echo not commented 1
  13. %[:%
  14.   multi
  15.   line
  16.   comment outside of brackets
  17. %:]%
  18. echo not commented 2
  19. %[:%
  20.   second multi
  21.   line
  22.   comment outside of brackets
  23. %:]%
  24. ::GOTO cannot be used inside for
  25. for %%a in (first second) do (
  26.     echo first not commented line of the %%a execution
  27.     %[%
  28.         multi line
  29.         comment
  30.     %]%
  31.     echo second not commented line of the %%a execution
  32. )
  33. With curly brackets:
  34. @echo off
  35. ::GOTO comment macro
  36. set "{:=goto :}%%"
  37. ::brackets comment macros
  38. set "{=rem/||(" & set "}=)"
  39. ::testing
  40. echo not commented 1
  41. %{:%
  42.   multi
  43.   line
  44.   comment outside of brackets
  45. %:}%
  46. echo not commented 2
  47. %{:%
  48.   second multi
  49.   line
  50.   comment outside of brackets
  51. %:}%
  52. for %%a in (first second) do (
  53.     echo first not commented line of the %%a execution
  54.     %{%
  55.         multi line
  56.         comment
  57.     %}%
  58.     echo second not commented line of the %%a execution
  59. )
  60. With parentheses:
  61. @echo off
  62. ::GOTO comment macro
  63. set "(:=goto :)%%"
  64. ::brackets comment macros
  65. set "(=rem/||(" & set ")=)"
  66. ::testing
  67. echo not commented 1
  68. %(:%
  69.   multi
  70.   line
  71.   comment outside of brackets
  72. %:)%
  73. echo not commented 2
  74. %(:%
  75.   second multi
  76.   line
  77.   comment outside of brackets
  78. %:)%
  79. for %%a in (first second) do (
  80.     echo first not commented line of the %%a execution
  81.     %(%
  82.         multi line
  83.         comment
  84.     %)%
  85.     echo second not commented line of the %%a execution
  86. )
  87. Mixture between powershell and C styles (< cannot be used because the redirection is with higher prio.* cannot be used because of the %*) :
  88. @echo off
  89. ::GOTO comment macro
  90. set "/#:=goto :#/%%"
  91. ::brackets comment macros
  92. set "/#=rem/||(" & set "#/=)"
  93. ::testing
  94. echo not commented 1
  95. %/#:%
  96.   multi
  97.   line
  98.   comment outside of brackets
  99. %:#/%
  100. echo not commented 2
  101. %/#:%
  102.   second multi
  103.   line
  104.   comment outside of brackets
  105. %:#/%
  106. for %%a in (first second) do (
  107.     echo first not commented line of the %%a execution
  108.     %/#%
  109.         multi line
  110.         comment
  111.     %#/%
  112.     echo second not commented line of the %%a execution
  113. )
复制代码

作者: CrLf    时间: 2018-7-11 18:57

实际上用在 for 里效果并不好,一方面是效率低,另一方面是虽然没执行但照样需要符合预处理规则
所以对 for 和 if 来说,多行注释还不如用 if 1==2 ( 你的注释 )




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2