【问题标题】:Batch loop through a delimited by hash string, which contain spaces and commas通过包含空格和逗号的哈希字符串分隔的批处理循环
【发布时间】:2015-05-06 20:56:01
【问题描述】:

我有一个由# char 分隔的许多部分组成的字符串。
某些部分可能包含空格和逗号。
我尝试了 FOR 循环,但这不起作用。

@echo off

set string_to_separate=11AAA#222 BB,333 CC,444DDD#555ee,77ff f#88g gg

for /f "tokens=* delims=#" %%i in (%string_to_separate%) do (
    echo %%i
)

我需要结果:

11AAA
222 BB,333 CC,444DDD
555ee,77ff f
88g gg

我需要使用批处理 (.bat) 文件,这样循环的选项有哪些?

附: string_to_separate 可以有超过 100 个部分(由 # char 分隔)。

【问题讨论】:

    标签: loops batch-file for-loop command-line scripting


    【解决方案1】:

    只要内容中没有!,以下内容将起作用。它通过为每个 # 替换一个新行来工作。

    @echo off
    setlocal enableDelayedExpansion
    set "string_to_separate=11AAA#'222 BB',333 CC,444DDD#555ee,77ff f#88g gg"
    for /f usebackq^ delims^=^ eol^= %%A in ('!string_to_separate:#^=^
    
    !') do echo %%A
    

    该方法可以在内容中支持!,只需更多代码:

    @echo off
    setlocal disableDelayedExpansion
    set "string_to_separate=11AAA!#'222 BB',333 CC,444DDD#555ee,77ff f#88g gg"
    setlocal enableDelayedExpansion
    for /f usebackq^ delims^=^ eol^= %%A in ('!string_to_separate:#^=^
    
    !') do (
      if "!!" equ "" endlocal
      echo %%A
    )
    

    【讨论】:

      【解决方案2】:

      如果字符串包含通配符,则以下方法将失败:*?

      @echo off
      
      set string_to_separate=11AAA#222 BB,333 CC,444DDD#555ee,77ff f#88g gg
      
      for %%i in ("%string_to_separate:#=" "%") do (
         echo %%~i
      )
      

      以下方法没有任何限制:

      @echo off
      setlocal DisableDelayedExpansion
      
      set string_to_separate=11AAA#222 BB,333 CC,444DDD#555ee,77ff f#88g gg
      
      set "str="
      setlocal EnableDelayedExpansion
      set ^"str=!string_to_separate:#=^
      % New line %
      !^"
      for /F "eol=# delims=" %%i in ("!str!") do (
         if defined str endlocal
         echo %%i
      )
      

      【讨论】:

        【解决方案3】:

        这里有另一个版本供您尝试。第一个 for 循环确定要在 myEcho 函数中提取哪个字符串。一旦字符串的部分数量用尽,myEcho 函数将不会回显。所以 200 只是一个任意数字,以确保报告所有部分。

        @echo off
        set string_to_separate=one#two#three#four#five#six#seven#eight#nine#ten#eleven#twelve#thirteen#fourteen#fifteen#sixteen#seventeen#eighteen#nineteen#twenty#twenty one#twenty two#twenty three#twenty four#twenty five#twenty six#twenty seven#twenty eight#twenty nine#thirty#thirty one#thirty two#thirty three#thirty four#thirty five#thirty six#thirty seven#thirty eight#thirty nine#forty#forty one#forty two#forty three#forty four#forty five#forty six#forty seven#forty eight#forty nine#fifty#fifty one#fifty two#fifty three#fifty four#fifty five#fifty six#fifty seven#fifty eight#fifty nine#sixty#sixty one#sixty two#sixty three#sixty four#sixty five#sixty six#sixty seven#sixty eight#sixty nine#seventy#seventy one#seventy two#seventy three#seventy four#seventy five#
        :loop
        if not defined string_to_separate goto :EOF
        FOR /F "tokens=1,* delims=#" %%i IN ("%string_to_separate%") Do (
          echo.%%i
          set string_to_separate=%%j
        )
        goto loop
        

        注意:string_to_separate 现在不断缩短,当 string_to_separate 为空时脚本结束。如果您需要原始字符串,只需将其复制到另一个变量即可。

        【讨论】:

        • 这一循环仅通过 31 个元素...,我将 string_to_separate 更改为 280 个元素,它在 31 处停止。
        • @BlueMark,似乎我被 for 循环中的 31 个令牌限制所挫败。一些小的修改就可以克服 - 请参阅修改后的答案。
        猜你喜欢
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多