批量使用多个for循环

时间:2019-04-01 10:40:20

标签: loops batch-file token

下面是我正在处理的代码

setlocal enabledelayedexpansion
echo on

IF NOT EXIST "D:\Deployments\Parameter_Build*.txt" (
exit
)
set SCRIPTDIRECTORY=D:\Deployments\
PUSHD %SCRIPTDIRECTORY%
FOR %%A in (Parameter_Build*.txt) DO (
echo %%A

set ANT_HOME=C:\tibco\ant\apache-ant-1.9.13
set string=
set /p string=< D:\Deployments\%%A
echo string is !string!
echo before for loop
call :myInnerloop !string!

 )
POPD
GOTO:EOF

rem *************************************************************

:myInnerloop
FOR /F "tokens=2,4,6,8,10 delims==:" %%G IN ("!string!") DO (
echo inside for loop
set COMPONENTDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\components"
set CONFIGDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\configuration\components"
set ADAPTER=%%K

echo %%G %%H %%I %%J %%K

echo value taken from file is !string! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !COMPONENTDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !CONFIGDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1


echo Starting with deployment with parameters %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
%ANT_HOME%\bin\xanteai deploy %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
echo Deployment completed >> D:\Deployments\logs\Deployment-%%J.log 2>&1
move D:\Deployments\Parameter_Build-%%J.txt D:\Deployments\archive\Parameter_Build-%%J.txt
RENAME D:\Deployments\logs\Deployment.txt Deployment-%%J.log

 )
:next
GOTO:EOF



endlocal

Parameter_Build文件包含以下格式的文本:-

Environment =:Domain =:Component = xyz.zip | abc.zip | jkl.zip | efg.zip:Build = 160:Adapter = xyz | abc | jkl | efg

我在这里尝试为每个组件执行部署

  

Component = xyz.zip | abc.zip | jkl.zip | efg.zip

通过对每个适配器进行SVN更新

  

xyz | abc | jkl | efg

我需要连续分离组件和适配器,并将其逐个传递给部署命令。同样,在触发部署之前,我需要使用每个组件各自的适配器进行SVN更新(例如:如果触发组件xyz.zip进行部署,则适配器XYZ应该首先通过SVN Update实用程序进行更新)

2 个答案:

答案 0 :(得分:4)

这将创建变量a [0],a [1],... a [n]

@echo off

setlocal EnableDelayedExpansion
set "var=xyz|abc|jkl|efg"

set cnt=0
set "a[0]=%var:|="&set /a cnt+=1&set "a[!cnt!]=%"

echo cnt=!cnt!
set a[

输出:

  

cnt = 3
  a [0] = xyz
  a [1] = abc
  a [2] = jkl
  a [3] = efg

说明

该代码仅用"&set /a cnt+=1&set "a[!cnt!]=替换定界符的所有出现。
看起来很奇怪,但举例来说,

set "a[0]=xyz"  & set /a cnt+=1  & set "a[!cnt!]=abc"  & set /a cnt+=1  & set "a[!cnt!]=jkl"  & set /a cnt+=1  & set "a[!cnt!]=efg"

展开到多行时,它看起来像

set "a[0]=xyz"  
set /a cnt+=1  
set "a[!cnt!]=abc"  
set /a cnt+=1  
set "a[!cnt!]=jkl"  
set /a cnt+=1  
set "a[!cnt!]=efg"

换句话说,它为每个定界符创建多个命令。

该技术甚至可以与<->这样的字符串定界符一起使用

set "a[0]=%var:<->=...

感谢@Aacini(介绍了该技术),请参阅split string into substrings based on delimiter

答案 1 :(得分:1)

我仍然不明白您真正想做什么,但这也许可以为您提供帮助:

@echo off
setlocal

set "Component=xyz.zip|abc.zip|jkl.zip|efg.zip"

for %%a in ("%Component:|=" "%") do (
   echo With extension: %%~a
   echo Without extension: %%~Na
)

编辑添加了新方法

请查看以下代码:

@echo off
setlocal EnableDelayedExpansion

rem Parameter_Build file contains text in below format :-
rem Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg

rem Read the file:
set /P "line=" < Parameter_Build.txt
echo Line read:
echo !line!

rem Separate variables:
set "%line::=" & set "%"

rem Separate Component and Adapter in *matching* parts
set cnt=1
set "Component[1]=%Component:|=" & set /A cnt+=1 & set "Component[!cnt!]=%"
set cnt=1
set "Adapter[1]=%Adapter:|=" & set /A cnt+=1 & set "Adapter[!cnt!]=%"

rem Ok:
for /L %%i in (1,1,%cnt%) do echo Component[%%i]=!Component[%%i]!, Adapter[%%i]=!Adapter[%%i]!

输出示例:

Line read:
Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
Component[1]=xyz.zip, Adapter[1]=xyz
Component[2]=abc.zip, Adapter[2]=abc
Component[3]=jkl.zip, Adapter[3]=jkl
Component[4]=efg.zip, Adapter[4]=efg