Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 通过由哈希字符串分隔的批处理循环,该字符串包含空格和逗号_Loops_Batch File_For Loop_Command Line_Scripting - Fatal编程技术网

Loops 通过由哈希字符串分隔的批处理循环,该字符串包含空格和逗号

Loops 通过由哈希字符串分隔的批处理循环,该字符串包含空格和逗号,loops,batch-file,for-loop,command-line,scripting,Loops,Batch File,For Loop,Command Line,Scripting,我有一个由许多部分组成的字符串,由#char分隔。 某些部分可能包含空格和逗号。 我试过打圈,但没用 @echo关闭 将字符串_设置为_separate=11AAA#222 BB、333 CC、444DDD#555ee、77ff f#88g gg 对于/f“tokens=*delims=#”%%i in(%string_to_separate%)do( 回声%%i ) 我需要结果: 11AAA 222 BB,333 CC,444DDD 555ee,77ff f 88g gg 我需要使用批处理

我有一个由许多部分组成的字符串,由
#
char分隔。
某些部分可能包含空格和逗号。
我试过打圈,但没用

@echo关闭
将字符串_设置为_separate=11AAA#222 BB、333 CC、444DDD#555ee、77ff f#88g gg
对于/f“tokens=*delims=#”%%i in(%string_to_separate%)do(
回声%%i
)
我需要结果:

11AAA
222 BB,333 CC,444DDD
555ee,77ff f
88g gg
我需要使用批处理(.bat)文件,有哪些选项可以这样循环


另外,
string\u to_separate
可能有100多个部分(由
char分隔)。

如果字符串包含通配符,下面的方法将失败:
*

@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
)

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

@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
)

只要没有
,以下操作就可以工作在内容中。它的工作原理是为每个
#
替换一个新行

该方法可以支持
包含更多代码的内容中:

@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
)

只要没有
,以下操作就可以工作在内容中。它的工作原理是为每个
#
替换一个新行

该方法可以支持
包含更多代码的内容中:

@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
)

这里有另一个版本供您尝试。第一个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

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

这里有另一个版本供您尝试。第一个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

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

这一个循环仅通过31个元素…,我将字符串_改为_分离为280个元素,并在31停止。@BlueMark,似乎我被for循环中的31个标记限制所挫败。一些小的编辑,这就克服了-请参阅修改后的答案。这一个循环仅通过31个元素…,我将字符串_改为_分隔为280个元素,并在31停止。@BlueMark,似乎我被for循环中的31个标记限制所挫败。一些小的编辑,这是克服-见修改后的答案。