batch for循环,从搜索列表

时间:2018-02-07 13:16:05

标签: batch-file

我打赌有人可以快速帮助我,没有找到正确的答案/解决方案而不是搜索功能,现在已经花了很多时间用于简单的单行班轮。我需要使用批处理文件在其中创建一个searchlist变量和多个搜索元素。 例如:

@Echo Off
Setlocal EnableDelayedExpansion
set searchlist="_Var1","_Var 2","Var_ 3","File1","varwithspaces = 12","supervar ="
FOR %%R IN (%searchlist%) DO (
echo %%R
)
pause

输出是:

"_Var1"
"_Var 2"
"Var_ 3"
"File1"
"varwithspaces = 12"
"supervar ="

但我需要这个输出:

_Var1
_Var 2
Var_ 3
File1
varwithspaces = 12
supervar =

请帮助我,即时批处理...

2 个答案:

答案 0 :(得分:0)

您实际上可以使用已设置的enabledelayedexpansion。然后将%%r设置为变量,然后替换"

@Echo Off
Setlocal EnableDelayedExpansion
set searchlist="_Var1","_Var 2","Var_ 3","File1","varwithspaces = 12","supervar ="
for %%r in (%searchlist%) DO (
set "var=%%r"
set "var=!var:"=!"
echo !var!
)
endlocal
pause

然后,为了摆脱双引号而不使用子字符串替换,只需使用变量前面的~

@Echo Off
set searchlist="_Var1","_Var 2","Var_ 3","File1","varwithspaces = 12","supervar ="
for %%r in (%searchlist%) DO (
echo %%~r
)
pause

答案 1 :(得分:0)

@elzooilogico 在评论中说:只需使用echo %%~R

@Echo Off
set searchlist="_Var1","_Var 2","Var_ 3","File1","varwithspaces = 12","supervar ="
FOR %%R IN (%searchlist%) DO (
    echo %%~R
)
pause

进一步阅读